Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow recording over a track #34

Merged
merged 1 commit into from
Dec 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion roadmap.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ A `Track` is a single mono or stereo audio buffer that contains audio data. A `T
- [x] Component can remove itself from scene https://github.com/ericyd/loop-supreme/pull/5
- [x] Component has arm toggle button https://github.com/ericyd/loop-supreme/pull/8
- [x] ~audio data can be cleared from component without deleting it (to preserve track name)~ just mute, and then re-record if desired
- [ ] `regression` allow re-recording audio over a track [regression introduced here](https://github.com/ericyd/loop-supreme/pull/27)
- [x] `regression` allow re-recording audio over a track [regression introduced here](https://github.com/ericyd/loop-supreme/pull/27) https://github.com/ericyd/loop-supreme/pull/34
- [x] deleting a track stops playback https://github.com/ericyd/loop-supreme/pull/13
- [x] Component can record data from user device https://github.com/ericyd/loop-supreme/pull/8
- [x] Component shows waveform of recorded audio https://github.com/ericyd/loop-supreme/pull/20
Expand Down
2 changes: 1 addition & 1 deletion src/Metronome/controls/TempoControl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export function TempoControl({ onChange, defaultValue }: TempoProps) {
} else {
setTaps((taps) => [...taps, now])
}
if (taps.length > 2) {
if (taps.length >= 2) {
const averageMs = averageBetween(taps.slice(-3))
const bpm = 60 / (averageMs / 1000)
debouncedOnChange(bpm)
Expand Down
8 changes: 5 additions & 3 deletions src/Track/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@ export const Track: React.FC<Props> = ({
/**
* Both of these are instantiated on mount
*/
const recorderWorklet = useRef<AudioWorkletNode>()
const bufferSource = useRef<AudioBufferSourceNode>()
const recorderWorklet = useRef<AudioWorkletNode | null>(null)
const bufferSource = useRef<AudioBufferSourceNode | null>(null)

/**
* Builds a callback that handles the messages from the recorder worker.
Expand Down Expand Up @@ -271,7 +271,7 @@ export const Track: React.FC<Props> = ({
if (recorderWorklet.current) {
recorderWorklet.current.disconnect()
recorderWorklet.current.port.onmessage = null
recorderWorklet.current = undefined
recorderWorklet.current = null
}
bufferSource.current?.stop()
mediaSource.disconnect()
Expand All @@ -290,9 +290,11 @@ export const Track: React.FC<Props> = ({
if (armed) {
setRecording(true)
setArmed(false)
bufferSource.current = null
recorderWorklet.current?.port?.postMessage({
message: 'UPDATE_RECORDING_STATE',
recording: true,
reset: true,
})
waveformWorker.postMessage({
message: 'RESET_FRAMES',
Expand Down
8 changes: 8 additions & 0 deletions src/workers/recorder.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@ class RecordingProcessor extends AudioWorkletProcessor {
this.port.onmessage = (event) => {
if (event.data.message === 'UPDATE_RECORDING_STATE') {
this.recording = event.data.recording
if (event.data.reset) {
this.channelsData = new Array(this.numberOfChannels).fill(
new Float32Array(this.maxRecordingSamples)
)
this.recordedSamples = 0
this.samplesSinceLastPublish = 0
this.gainSum = 0
}

// When the recording ends, send the buffer back to the Track
if (this.recording === false) {
Expand Down