From d681c3d237c7284a8a76b3120b90103d1f369fbd Mon Sep 17 00:00:00 2001 From: Gene Mecija Date: Fri, 17 Jan 2020 18:19:52 -0800 Subject: [PATCH] Progress on tone timing synchronization between telegraph keys --- src/config.json | 2 +- src/hooks/useElectronicKey.js | 53 ++++++++++++++++++++++++----------- src/hooks/useMorsePlayer.js | 8 ++++-- src/hooks/useStraightKey.js | 7 +++-- 4 files changed, 49 insertions(+), 21 deletions(-) diff --git a/src/config.json b/src/config.json index ea050cd..f59ed1d 100644 --- a/src/config.json +++ b/src/config.json @@ -2,7 +2,7 @@ "timingUnit": 1, "ditMaxTime": 60, "mainVolume": 0.2, - "frequency": 700.0, + "frequency": 550.0, "practiceSpeed": { "slow": 40, "normal": 24, diff --git a/src/hooks/useElectronicKey.js b/src/hooks/useElectronicKey.js index b85f288..71f2166 100644 --- a/src/hooks/useElectronicKey.js +++ b/src/hooks/useElectronicKey.js @@ -10,8 +10,9 @@ function useElectronicKey() { const timingUnit = config.timingUnit - let ratio = 0.2 - const ditMaxTime = 80 // ditMaxTime * 0.365 to get ms, e.g. 85 * 0.365 ~= 31ms + let ratio = .2 + const ditMaxTime = 85 // ditMaxTime * 0.365 to get ms, e.g. 85 * 0.365 ~= 31ms + const letterGapMinTime = ditMaxTime*ratio*3 //config.practiceSpeed.normal*3 const wordGapMaxTime = ditMaxTime*ratio*7 // config.practiceSpeed.normal*7 const morseHistorySize = config.historySize @@ -57,6 +58,12 @@ function useElectronicKey() { let frequency = config.frequency + let toneTimer = 0 + let toneTime = 0 + let start = 0 + let end = 0 + + // Promisify playing Dits and Dahs function play(ditDah) { let playDuration = ((ditDah === '.') ? ditMaxTime : ditMaxTime*3) @@ -82,19 +89,23 @@ function useElectronicKey() { g.connect(context.destination) o.start(startTime) - - // // for troubleshooting ditDah length in milliseconds - // toneTimer = setInterval(() => { - // toneTime += 1 - // }, 1); - // start = toneTime - // // - - g.gain.setTargetAtTime(0.0001, startTime + playDuration/1000, 0.001) - o.stop(startTime + playDuration/1000 + 0.05) + + setTimeout(() => { + g.gain.setTargetAtTime(0.0001, context.currentTime, 0.001) + o.stop(context.currentTime + 0.05) + }, playDuration) + // g.gain.setTargetAtTime(0.0001, startTime + playDuration/1000, 0.001) + // o.stop(startTime + playDuration/1000 + 0.05) }) } + function stopToneTimer() { + clearInterval(toneTimer) + end = toneTime + console.log('toneTime:', start, end); + toneTime = 0 + } + function playWithSpaces(ditDah) { let delay = (ditDah === '.') ? ditMaxTime + ditMaxTime : ditMaxTime*3 + ditMaxTime @@ -105,14 +116,19 @@ function useElectronicKey() { checkGapBetweenInputs() setMorseCharBuffer(prev => prev + ditDah) + // for troubleshooting ditDah length in milliseconds + toneTimer = setInterval(() => { + toneTime += 1 + }, 1); + start = toneTime + + play(ditDah) .then(setTimeout(() => { - // START GAP TIMER - // gapTimerRunning = true gapTimer = setInterval(() => { gapTime += 1 - + if (gapTime >= wordGapMaxTime) { setMorseCharBuffer(prev => prev + '/') clearInterval(gapTimer) @@ -122,7 +138,8 @@ function useElectronicKey() { }, timingUnit) resolve(); - }, delay)) + }, delay) + ) } else { setTimeout(() => { resolve(); @@ -264,6 +281,10 @@ function useElectronicKey() { depressSyncTime = 0 } function checkGapBetweenInputs() { + console.log('ditMaxTime', ditMaxTime) + console.log('gapTime', gapTime); + console.log('letterGapMinTime', letterGapMinTime); + console.log('wordGapMaxTime', wordGapMaxTime); // Check Gap between letters if (gapTime >= letterGapMinTime && gapTime < wordGapMaxTime) { setMorseCharBuffer(prev => prev + ' ') diff --git a/src/hooks/useMorsePlayer.js b/src/hooks/useMorsePlayer.js index 6b9d696..b43e350 100644 --- a/src/hooks/useMorsePlayer.js +++ b/src/hooks/useMorsePlayer.js @@ -41,8 +41,12 @@ function useMorsePlayer() { g.connect(context.destination) o.start(startTime) - g.gain.setTargetAtTime(0.0001, startTime + length/1000, 0.001) - o.stop(startTime + length/1000 + 0.05) + // g.gain.setTargetAtTime(0.0001, startTime + length/1000, 0.001) + // o.stop(startTime + length/1000 + 0.05) + setTimeout(() => { + g.gain.setTargetAtTime(0.0001, context.currentTime, 0.009) + o.stop(context.currentTime + 0.05) + }, length) }) } diff --git a/src/hooks/useStraightKey.js b/src/hooks/useStraightKey.js index 984307e..8182aa6 100644 --- a/src/hooks/useStraightKey.js +++ b/src/hooks/useStraightKey.js @@ -109,7 +109,7 @@ function useStraightKey() { if (o.context.state === 'running') { g.gain.setTargetAtTime(0.0001, context.currentTime, 0.001) - o.stop(context.currentTime + 0.01) + o.stop(context.currentTime + 0.05) } } else { return } } @@ -142,7 +142,10 @@ function useStraightKey() { function checkGapBetweenInputs() { // Check Gap between letters - + console.log('ditMaxTime', ditMaxTime) + console.log('gapTime', gapTime); + console.log('letterGapMinTime', letterGapMinTime); + console.log('wordGapMaxTime', wordGapMaxTime); if (gapTime >= letterGapMinTime && gapTime < wordGapMaxTime) { // if (mode === 'practice') { setMorseCharBuffer(prev => prev + ' ')