Click here to Skip to main content
15,507,425 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
A generated sine sound data are stored in a byte array:

double SAMPLE_RATE = 44100.0;
		double AMPLITUDE = 32768.0;
		double timeSeconds= 10;
		int shortBufferSize= (int) (timeSeconds * SAMPLE_RATE);
		short[] shortBuf = new short[shortBufferSize];
		double dT= timeSeconds / shortBufferSize;
		for (int tIx= 0; tIx < shortBufferSize; tIx++)
		  double time= tIx * dT;
		  double sinValue= Math.sin(2.0 * Math.PI * hz * time);
		  shortBuf[tIx]= (short)(AMPLITUDE * sinValue);
	    byte[] byte_array = new byte[2 * shortBufferSize];
	    for (int i = 0; i < shortBuf.length; ++i)
			byte_array[2*i] = (byte)shortBuf[i];
            byte_array[2*i+1] = (byte) (shortBuf[i] >> 8);

and is played this way:

AudioFormat af = new AudioFormat(44100, 16, 1, true, false);
		SourceDataLine sdl = AudioSystem.getSourceDataLine (af); (af);
		sdl.start ();
		sdl.write (byte_array, 0, byte_array.length);
		sdl.drain ();

how to convert it from mono to stereo, the reason is I want to actually give different data for per channel like this:

byte[] byte_array_left = new byte[2 * shortBufferSize];

byte[] byte_array_right = new byte[2 * shortBufferSize];

does this need to be converted or just a change in the code can do it

What I have tried:

there are some codes online that offer converting from mono to stereo and stereo to mono but did not have the option to actually manipulate per channel's data.
Updated 30-Aug-22 19:18pm

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900