Tonejs / Tone.js

A Web Audio framework for making interactive music in the browser.
https://tonejs.github.io
MIT License
13.52k stars 983 forks source link

connecting player with pitchshift seems to play audio twice? #905

Closed Earu closed 3 years ago

Earu commented 3 years ago

Hi, so I'm not quite sure if it's a bug or if it's because there's something I'm doing wrong but whenever I connect a PitchShift node to a Player node and then call start on the player node, it plays both the sound non pitched and the sound pitched at once. Any idea as to what's happening?

Here's my code:

// in class1.ts
    processAudio(player: Tone.Player): Tone.Player {
        const pitchShift: Tone.PitchShift = new Tone.PitchShift(100).toDestination();
        pitchShift.pitch = this.value;

        return player.connect(pitchShift);
    }

// in class2.ts
    async process(): Promise<void> {
        const buffer: Tone.ToneAudioBuffer = await this.resolveBuffer();

        let ply: Tone.Player = new Tone.Player(buffer);
        for (const modifier of this.chatsound.modifiers) {
            ply = modifier.processAudio(ply);
        }

        ply = ply.toDestination().start()
        await this.listen(ply);
    }
thely commented 3 years ago

It looks like both your Tone.Player and your Tone.PitchShift are both being sent to toDestination(). Remember that the Destination is your speakers. So instead of the chain you expect, which is Player -> PitchShift -> Speakers, you've got Player -> Speakers and Player -> PitchShift -> Speakers.

Just get rid of the toDestination() on ply.toDestination().

tambien commented 3 years ago

@thely 🙏🏻