Skip to content

Commit

Permalink
use two audio output channels instead of one
Browse files Browse the repository at this point in the history
- This allows using the correct output sample rate
  • Loading branch information
wysiwys committed Sep 1, 2024
1 parent f91a428 commit 1993ac6
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 22 deletions.
31 changes: 16 additions & 15 deletions frontend/audio.worklet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,8 @@ export default class XmrsProcessor extends AudioWorkletProcessor {
return;
}
try {
// XXX: why need to divide sample rate by 2?
this.player = new Player(
sampleRate / 2,
sampleRate,
e.data.bytes,
e.data.fileType,
);
Expand All @@ -59,27 +58,29 @@ export default class XmrsProcessor extends AudioWorkletProcessor {

this.port.postMessage({ type: 'FETCH_WASM' });
}
process(inputs: any, outputs: any, parameters: any) {
process(inputs: any, outputs: Float32Array[][], parameters: any) {
try {
if (this.player === null) {
return false;
}

const output = outputs[0];
let channel1 = output[0];
let channel2 = output[1];

let player = this.player as Player;

output.forEach((channel: any) => {
try {
player.nextSamples(channel);
} catch (e) {
if (e === PlayerError.SongOver) {
throw new Error('Song over');
} else if (e instanceof Error) {
throw e;
} else {
throw new Error(`${e}`);
}
try {
player.nextSamples(channel1, channel2);
} catch (e) {
if (e === PlayerError.SongOver) {
throw new Error('Song over');
} else if (e instanceof Error) {
throw e;
} else {
throw new Error(`${e}`);
}
});
}
} catch (e) {
if (this.player !== null) {
this.player.free();
Expand Down
15 changes: 8 additions & 7 deletions frontend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,15 +132,16 @@ impl Player {
}

#[wasm_bindgen(js_name = nextSamples)]
pub fn next_samples(&mut self, output: &mut [f32]) -> Result<(), PlayerError> {
// Check if playing has completed
// This is the same check that would be done in `player.next()`
if self.player.max_loop_count > 0 && self.player.loop_count >= self.player.max_loop_count {
return Err(PlayerError::SongOver);
pub fn next_samples(
&mut self,
chan1: &mut [f32],
chan2: &mut [f32],
) -> Result<(), PlayerError> {
for i in 0..chan1.len() {
chan1[i] = self.player.next().ok_or(PlayerError::SongOver)?;
chan2[i] = self.player.next().ok_or(PlayerError::SongOver)?;
}

self.player.generate_samples(output);

Ok(())
}
}
3 changes: 3 additions & 0 deletions frontend/typescript_src/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,9 @@ export const useSharedPlayer = defineStore('main', {
this.isReady = false;

let options = {
numberOfOutputs: 1,
outputChannelCount: [2],
channelCount: 2,
processorOptions: {},
};

Expand Down

0 comments on commit 1993ac6

Please sign in to comment.