简体   繁体   中英

Can't Split Audio Into Separate Channels with Tone.js

I've started creating an application with a library called Tone.js that allows me to manipulate Audio on the web in all sorts of ways.

Currently I'd like to create two channels (left and right) for headphone users and play one different frequency in each ear (eg 400Hz in left and 500Hz in right)

With my current code, I've got two frequencies playing both are playing in each ear. Does anyone have any suggestions as to how I could separate them?

Here's my code thus far:

//create a synth and connect it to the master output (your speakers)

//Connect each separate tone to split
var split = new Tone.Split();
var leftEar = new Tone.Oscillator().toMaster();
var rightEar = new Tone.Oscillator().toMaster();

leftEar.frequency.value = 400;
rightEar.frequency.value = 500;

split.left = leftEar;
split.right = rightEar;

leftEar.connect(split);
rightEar.connect(split);

leftEar.start();
rightEar.start();

//Frequency is equivalent to difference between frequency in left and right ear
var frequency = {
  "Gamma" : [30, 50],
  "Beta" : [14, 30],
  "Alpha" : [8, 14],
  "Theta" : [4, 8],
  "Delta" : [0.1, 4]
};

Thanks!

Reference: https://tonejs.github.io/docs/#Split and https://github.com/Tonejs/Tone.js/wiki/Signals

  • You want to use Merge , not Split
  • You are sending both your left and right oscillators directly to the master output, you should only be calling .toMaster() on split
  • You are deleting the GainNodes that Merge produces by doing split.left = leftEar;
  • You are connecting both oscillators to both channels, you should be connecting them to their respective channels by doing leftEar.connect(split.left)

After these changes your code will look like this:

var split = new Tone.Merge().toMaster();
var leftEar = new Tone.Oscillator();
var rightEar = new Tone.Oscillator();

leftEar.frequency.value = 400;
rightEar.frequency.value = 500;

leftEar.connect(split.left);
rightEar.connect(split.right);

leftEar.start();
rightEar.start();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM