end take cleanly when system cuts mic, hold wake lock while recording

Hunter
Sunday, November 15th, 2026 at 9:59 AM

resources/main.js+31-11

resources/main.js
 34  // A tape's index is its id on the audio thread. A deleted tape leaves a null behind.
 35  const tapes = [];
 36  let recording = null;
 37 +// A promise of the screen wake lock held during a take.
 38 +let wakeLock = null;
 39  let audio = null;
 40  let deck = null;
 41  let setup = null;

132  	});
133  	deck.connect(audio.destination);
134  	deck.port.onmessage = ({data}) => handlers[data.type](data);
135 +	// The system can interrupt the audio, as it does at lock in Safari.
136 +	audio.onstatechange = () => {
137 +		if (audio.state !== 'running') endTake();
138 +	};
139  	setAudioSessionType('playback');
140  	await loadTapes();
141  }

153  		});
154  		const source = audio.createMediaStreamSource(stream);
155  		source.connect(deck);
156 +		const [track] = stream.getAudioTracks();
157 +		// The system mutes or ends the track when it cuts the microphone.
158 +		track.onmute = track.onended = endTake;
159  		// iOS names the built-in microphone after the device, as in "iPhone Microphone".
    -		const builtIn = /iPhone|iPad/.test(stream.getAudioTracks()[0].label);
160 +		const builtIn = /iPhone|iPad/.test(track.label);
161  		deck.port.postMessage({
162  			type: 'microphone',
163  			gain: builtIn ? BUILT_IN_MICROPHONE_GAIN : 1,

189  }
190  
191  // In Safari an open microphone stops audio at lock, so it is released while
    -// hidden unless a take is in progress.
192 +// hidden once no take is in progress.
193 +function releaseHiddenMicrophone() {
194 +	if (!navigator.audioSession || !document.hidden || !microphone || recording) return;
195 +	closeMicrophone();
196 +	setAudioSessionType('playback');
197 +}
198 +
199  document.addEventListener('visibilitychange', async () => {
200  	if (!navigator.audioSession || !deck) return;
201  	if (!document.hidden) {
202  		audio.resume();
203  		if (microphoneUsed && !microphone) await openMicrophone();
204  	}
    -	if (document.hidden && microphone && !recording) {
    -		closeMicrophone();
    -		setAudioSessionType('playback');
    -	}
205 +	releaseHiddenMicrophone();
206  });
207  
208  const handlers = {

220  		tape.redraw = true;
221  		makeGrabbable(tape, tapes.length);
222  		tapes.push(tape);
223 +		releaseHiddenMicrophone();
224  	},
225  
226  	silent() {

230  	discard() {
231  		recording.row.remove();
232  		recording = null;
233 +		releaseHiddenMicrophone();
234  	},
235  
236  	direction({id, direction}) {

274  	return tape;
275  }
276  
277 +function endTake() {
278 +	if (!recording) return;
279 +	deck.port.postMessage({type: 'stop'});
280 +	recordButton.classList.remove('recording');
281 +	wakeLock?.then(lock => lock.release());
282 +	wakeLock = null;
283 +}
284 +
285  async function toggleRecording() {
    -	if (recording) {
    -		deck.port.postMessage({type: 'stop'});
    -		recordButton.classList.remove('recording');
    -		return;
    -	}
286 +	if (recording) return endTake();
287  	// Only a press may start audio, and it stops counting once the microphone
288  	// prompt is answered, so resume before awaiting.
289  	audio.resume();

296  	const {row} = recording;
297  	deck.port.postMessage({type: 'record'});
298  	recordButton.classList.add('recording');
299 +	// Keeps the screen from locking by itself, which would cut the microphone.
300 +	wakeLock = navigator.wakeLock?.request('screen');
301  
302  	row.animate(
303  		[{height: 0, opacity: 0}, {}],