Skip to content

Commit

Permalink
tts output now goes through main, added volume controls
Browse files Browse the repository at this point in the history
  • Loading branch information
goldbuick committed Jan 4, 2025
1 parent feeeb0e commit 482feb2
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 53 deletions.
12 changes: 8 additions & 4 deletions zss/device/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,6 @@ export function register_share(sender: string, player: string) {
hub.emit('register:share', sender, undefined, player)
}

export function register_refresh(sender: string, player: string) {
hub.emit('register:refresh', sender, undefined, player)
}

export function register_select(sender: string, book: string, player: string) {
hub.emit('register:select', sender, book, player)
}
Expand Down Expand Up @@ -161,6 +157,14 @@ export function synth_drumvolume(
hub.emit('synth:drumvolume', sender, volume, player)
}

export function synth_ttsvolume(
sender: string,
volume: number,
player: string,
) {
hub.emit('synth:ttsvolume', sender, volume, player)
}

export function synth_voice(
sender: string,
idx: number,
Expand Down
20 changes: 8 additions & 12 deletions zss/device/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
peer_create,
platform_init,
register_ready,
register_refresh,
tape_crash,
tape_info,
tape_terminal_close,
Expand Down Expand Up @@ -78,7 +77,11 @@ function readurlhash(): string {
let shouldreload = true
window.addEventListener('hashchange', () => {
if (shouldreload) {
location.reload()
// location.reload()
// how do we clear the state here ??
// do we tear down platform and re-make it ??
// question, should we then just shove another vm_init ??
// ie: here is your starting point again ??
} else {
// reset after a single pass
shouldreload = true
Expand Down Expand Up @@ -211,15 +214,6 @@ const register = createdevice(
})
}
break
case 'refresh':
if (message.player === sessionid) {
doasync('register:refresh', async function () {
writeheader(register.name(), 'BYE')
await waitfor(100)
location.reload()
})
}
break
case 'nuke':
if (message.player === sessionid) {
doasync('register:nuke', async function () {
Expand All @@ -232,6 +226,7 @@ const register = createdevice(
await waitfor(1000)
writeheader(register.name(), 'BYE')
await waitfor(100)
// nuke is the only valid case for reload
location.hash = ''
location.reload()
})
Expand All @@ -251,7 +246,8 @@ const register = createdevice(
doasync('register:select', async () => {
if (isstring(message.data)) {
await writeselectedid(message.data)
register_refresh(register.name(), sessionid)
// use same solution as a hash change here ...
// re-run the vm_init flow
}
})
}
Expand Down
75 changes: 44 additions & 31 deletions zss/device/synth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ function createsynth() {
const destination = getDestination()
const broadcastdestination = getContext().createMediaStreamDestination()

const mainvolume = new Volume(4)
const mainvolume = new Volume(0)
mainvolume.connect(destination)
mainvolume.connect(broadcastdestination)

Expand All @@ -90,24 +90,27 @@ function createsynth() {
})
maincompressor.connect(mainvolume)

const maingain = new Gain()
maingain.connect(maincompressor)
const playvolume = new Volume(0)
playvolume.connect(maincompressor)

const drumvolume = new Volume(2)
const drumvolume = new Volume(1)
drumvolume.connect(maincompressor)

const ttsvolume = new Volume(3)
ttsvolume.connect(maincompressor)

const SOURCE = [
// for sfx
createsource(maingain),
createsource(playvolume),
// + 8track synths
createsource(maingain),
createsource(maingain),
createsource(maingain),
createsource(maingain),
createsource(maingain),
createsource(maingain),
createsource(maingain),
createsource(maingain),
createsource(playvolume),
createsource(playvolume),
createsource(playvolume),
createsource(playvolume),
createsource(playvolume),
createsource(playvolume),
createsource(playvolume),
createsource(playvolume),
]

// config drums
Expand Down Expand Up @@ -614,12 +617,24 @@ function createsynth() {
drumvolume.volume.value = volume
}

// tts output
function addttsaudiobuffer(audiobuffer: AudioBuffer) {
const player = new Player(audiobuffer).connect(ttsvolume)
player.start(0)
}

function setttsvolume(volume: number) {
ttsvolume.volume.value = volume
}

return {
broadcastdestination,
addplay,
stopplay,
addttsaudiobuffer,
setmainvolume,
setdrumvolume,
setttsvolume,
SOURCE,
}
}
Expand Down Expand Up @@ -696,22 +711,7 @@ function validatesynthtype(
}

// get MicrosoftSpeechTTS instance
const tts = new EdgeSpeechTTS({ locale: 'en-US' })

function playaudiobuffer(audiobuffer: AudioBuffer) {
const player = new Player(audiobuffer).toDestination()
player.start(0)
console.info('playing audio buffer', player)
}

async function handletts(voice: string, phrase: string) {
const audiobuffer = await tts.createAudio({
input: phrase,
options: { voice: voice || 'en-US-GuyNeural' },
})
// play the audio
playaudiobuffer(audiobuffer)
}
let tts: MAYBE<EdgeSpeechTTS>

let synth: MAYBE<ReturnType<typeof createsynth>>

Expand All @@ -737,6 +737,11 @@ const synthdevice = createdevice('synth', [], (message) => {
synth.setdrumvolume(message.data)
}
break
case 'ttsvolume':
if (isnumber(message.data)) {
synth.setttsvolume(message.data)
}
break
case 'play':
if (isarray(message.data)) {
const [mode, buffer] = message.data as [number, string]
Expand Down Expand Up @@ -1180,9 +1185,17 @@ const synthdevice = createdevice('synth', [], (message) => {
break
case 'tts':
doasync('tts', async () => {
if (isarray(message.data)) {
if (!ispresent(tts)) {
tts = new EdgeSpeechTTS({ locale: 'en-US' })
}
if (ispresent(synth) && isarray(message.data)) {
const [voice, phrase] = message.data as [string, string]
await handletts(voice, phrase)
const audiobuffer = await tts.createAudio({
input: phrase,
options: { voice: voice || 'en-US-GuyNeural' },
})
// play the audio
synth.addttsaudiobuffer(audiobuffer)
}
})
break
Expand Down
8 changes: 5 additions & 3 deletions zss/device/vm.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createdevice } from 'zss/device'
import { INPUT, UNOBSERVE_FUNC } from 'zss/gadget/data/types'
import { doasync } from 'zss/mapping/func'
import { waitfor } from 'zss/mapping/tick'
import { MAYBE, isarray, ispresent, isstring } from 'zss/mapping/types'
import { isjoin } from 'zss/mapping/url'
import {
Expand Down Expand Up @@ -30,11 +31,11 @@ import {
gadgetserver_clearplayer,
platform_started,
register_flush,
register_refresh,
tape_debug,
tape_info,
vm_codeaddress,
vm_flush,
vm_login,
} from './api'
import { modemobservevaluestring, modemwriteplayer } from './modem'

Expand Down Expand Up @@ -117,8 +118,9 @@ const vm = createdevice('vm', ['init', 'tick', 'second'], (message) => {
gadgetserver_clearplayer('vm', message.player)
// save state
await savestate()
// reload page
register_refresh('vm', message.player)
// attempt re-login
await waitfor(1000)
vm_login(vm.name(), message.player)
})
}
break
Expand Down
6 changes: 6 additions & 0 deletions zss/firmware/audio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
synth_mainvolume,
synth_play,
synth_tts,
synth_ttsvolume,
synth_voice,
synth_voicefx,
} from 'zss/device/api'
Expand Down Expand Up @@ -63,6 +64,11 @@ export const AUDIO_FIRMWARE = createfirmware()
synth_drumvolume('audio', volume, READ_CONTEXT.player)
return 0
})
.command('ttsvolume', (_, words) => {
const [volume] = readargs(words, 0, [ARG_TYPE.NUMBER])
synth_ttsvolume('audio', volume, READ_CONTEXT.player)
return 0
})
.command('play', (chip, words) => {
handlesynthplay(1, chip, words)
return 0
Expand Down
6 changes: 3 additions & 3 deletions zss/gadget/audio/source.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Gain, Synth } from 'tone'
import { Synth, Volume } from 'tone'

import { createfx } from './fx'

export function createsource(maingain: Gain) {
export function createsource(playvolume: Volume) {
const source = new Synth()
source.set({
envelope: {
Expand All @@ -24,7 +24,7 @@ export function createsource(maingain: Gain) {
fx.distortion,
fx.echo,
fx.reverb,
maingain,
playvolume,
)

return { source, fx }
Expand Down

0 comments on commit 482feb2

Please sign in to comment.