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

add convert stereo to mono function #96

Open
trackme518 opened this issue Dec 24, 2023 · 0 comments
Open

add convert stereo to mono function #96

trackme518 opened this issue Dec 24, 2023 · 0 comments
Labels
enhancement New feature or request

Comments

@trackme518
Copy link

trackme518 commented Dec 24, 2023

Hi,
I propose adding function to convert stereo sample to mono, code below. It can be placed inside AudioSample.java I think. It works for me from Processing sketch when called on SoundFile.

To keep only one channel and discard the other:

        //this will stop playback if it is playing already
	public stereoToMono(int whichChannel){ //whichChannel should be 0 for left and 1 for roght
		float[] newBuffer = new float[this.frames()];
		if(whichChannel>1){
			whichChannel = 1;
		}
		if(whichChannel<0){
			whichChannel = 0;
		}
		for (int i = 0; i < newBuffer.length; i++) {
			newBuffer[i] = this.read(i, whichChannel);//read only left channel
		}
		this.resize(newBuffer.length, false); //change to mono
		this.write(newBuffer); //copy data from single channel into buffer
	}

To sum up two channels into one:

        //this will stop playback if it is playing already
	public stereoToMono(int whichChannel){ //whichChannel should be 0 for left and 1 for roght
		float[] newBuffer = new float[this.frames()];
		if(whichChannel>1){
			whichChannel = 1;
		}
		if(whichChannel<0){
			whichChannel = 0;
		}
		for (int i = 0; i < newBuffer.length; i++) {
                        float left = bufferData[i] = sample.read(i, 0);
                        float right = bufferData[i] = sample.read(i, 1);			
                        newBuffer[i] = (left+right)/2;
		}
		this.resize(newBuffer.length, false); //change to mono
		this.write(newBuffer); //copy data from single channel into buffer
	}
@trackme518 trackme518 changed the title convert stereo to mono add convert stereo to mono function Dec 24, 2023
@kevinstadler kevinstadler added the enhancement New feature or request label Dec 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants