ExoPlayer icon indicating copy to clipboard operation
ExoPlayer copied to clipboard

Implement volume controlling AudioProcessor, with independent control of channel volumes

Open platinum7919 opened this issue 9 years ago • 50 comments

I need a way to switch left and right audio volume separately. Similar to https://developer.android.com/reference/android/media/MediaPlayer.html#setVolume(float, float) Currently, I can't find a way to do this in Exoplayer

Using exoplayer2.3.1 Using android 6.0.1+

platinum7919 avatar Apr 08 '17 03:04 platinum7919

The stereo variant for setting volume was deprecated in the platform, at least on AudioTrack. See here. I do wonder what the recommended approach is though. @andrewlewis - Any ideas?

ojw28 avatar Apr 10 '17 08:04 ojw28

@ojw28 Separated volume control for stereo channels is an often requirement of TV operator for its boxes. Based on the old technique for analogue TV. As example English and Spain tracks were broadcasted in the same audio track in different channels for southern states of USA.

Avetri avatar Apr 10 '17 08:04 Avetri

The platform doesn't provide support for this [internal bug 37200638]. We could implement it as an AudioProcessor that allows the application to set a volume multiplier for each channel, but it likely won't happen for a while. Feel free to send a pull request.

We have an existing audio processor called ChannelMappingAudioProcessor, which would address the use case of taking stereo and replacing one channel with the other. It might make sense to add functionality there for setting the volumes of output channels too.

andrewlewis avatar Jul 24 '17 10:07 andrewlewis

Hi, I found a solution, You can edit AudioTrack.java file to use setStereoVolume insteal setVolume. However setStereoVolume have a bug: it not work on some android version.

https://stackoverflow.com/questions/10462364/android-audiotrack-setstereovolume-not-working

mdtuyen avatar Feb 27 '18 02:02 mdtuyen

Hi, I do have the very same problem as @platinum7919, i.e., the need to control left and right audio volumes for ExoPlayer. Everything else going fine, including looping (gapless) mp3 palying. Using exoplayer 2.7.1 (SimpleExoPlayer class).

Any forecast for such method availability? Or any intermediate solution?

roundsmapp avatar Mar 22 '18 13:03 roundsmapp

We don't have any plans to support this at the moment, as it seems quite niche. For the case of different languages in left/right channels, using ChannelMappingAudioProcessor lets you achieve the desired effect. For changing the volumes of channels independently in general, I suggest implementing a custom AudioProcessor that copies each channel and applies the relevant scaling.

andrewlewis avatar Mar 27 '18 09:03 andrewlewis

@andrewlewis , thank you for your response. Is there any place where I can see a custom implementation of AudioProcessor with separate channels? I've been looking for documentation and didn't find where I can make those channels work.

roundsmapp avatar Mar 27 '18 13:03 roundsmapp

I'd start by reading ChannelMappingAudioProcessor to see how it works, then make a copy of it and modify it to multiply each sample being copied by a factor associated with its channel. The input/output data to/from the processor is interleaved 16-bit PCM, so it might be best to access the input buffer as a ShortBuffer rather than a ByteBuffer.

To use your custom AudioProcessor with SimpleExoPlayer you can override DefaultRenderersFactory.buildAudioProcessors in the renderers factory you pass to the constructor.

andrewlewis avatar Mar 27 '18 14:03 andrewlewis

@andrewlewis , I guess I got it. Thank you!

roundsmapp avatar Mar 27 '18 20:03 roundsmapp

@andrewlewis , I was able to effectively separate the channels. But the player has a weird behaviour. First of all, if I configure AudioProcessor (ChannelMappingAudioProcessor) with only 2 channels, I can isolate the left one, but not the right one.

If I configure it with 3 or more channels, then I can isolate both, making the audio come out from just one channel or from both.

Independently of that, I have another problem. From the channel that is OFF, there is a strange noise coming out of it, while the other channel is playing the audio well.

Do you see any explanation?

roundsmapp avatar Mar 29 '18 16:03 roundsmapp

It's hard to say what's going on without seeing the code. Could you post it somewhere?

andrewlewis avatar Mar 29 '18 16:03 andrewlewis

@andrewlewis , Here you have it:

public void queueInput(ByteBuffer inputBuffer) { int position = inputBuffer.position(); int limit = inputBuffer.limit(); int frameCount = (limit - position) / (2 * channelCount); int outputSize = frameCount * outputChannels.length * 2; if (buffer.capacity() < outputSize) { buffer = ByteBuffer.allocateDirect(outputSize).order(ByteOrder.nativeOrder()); } else { buffer.clear(); } int i; while (position < limit) { for (int channelIndex : outputChannels) { i=0; buffer.putShort(inputBuffer.getShort((position + 2 * i) * channelIndex)); i++; } position += channelCount * 2; } inputBuffer.position(limit); buffer.flip(); outputBuffer = buffer; }

outputChannels has the values 1 or 0 for the channels.

roundsmapp avatar Mar 29 '18 17:03 roundsmapp

The index calculation (position + 2 * i) * channelIndex) looks wrong, as it doesn't make much sense to multiply the position in the buffer by the channel index.

Could try taking ChannelMappingAudioProcessor verbatim and replacing the copying loop with the following (warning: untested)?

while (position < limit) {
  for (int i = 0; i < outputChannels.length; i++) {
    short sample = inputBuffer.getShort(position + 2 * outputChannels[i]);
    sample = (short) (sample * outputChannelVolumes[i]);
    buffer.putShort(sample);
  }
  position += channelCount * 2;
}

Some more care probably needs to be taken with scaling the sample but hopefully this is enough to get started. ChannelMappingAudioProcessor also has logic to make it inactive if channels aren't modified, so while experimenting make configure set active = true (rather than check the mapping).

andrewlewis avatar Apr 03 '18 15:04 andrewlewis

@andrewlewis , I made the changes. It is working great! Thanks again for the help.

roundsmapp avatar Apr 03 '18 18:04 roundsmapp

@andrewlewis , good morning! What's the maximum number of outputChannels allowed? 8?

roundsmapp avatar Apr 16 '18 13:04 roundsmapp

Yes. See DefaultAudioSink.

andrewlewis avatar Apr 16 '18 13:04 andrewlewis

OK, Thank you.

roundsmapp avatar Apr 16 '18 13:04 roundsmapp

Hello, @andrewlewis , everything was going fine until I started testing using the 8 channels. I've found a very similar problem as issue #3335 (which is closed, therefore I couldn't place a comment there). For most of the channels, I can clearly control volume and thus have only left or right. But, for some reason, I can't do this for channels 2 and 3 (3rd and 4th).

Is it a bug?

roundsmapp avatar Apr 24 '18 01:04 roundsmapp

Could you elaborate a bit on what you're seeing? Specifically: what is the input channel count, what processing are you doing (e.g., multiplying each channel's PCM samples by 0.5), what is the output channel count and how many channels is the platform downmixing to? What is the expected and observed behavior?

It sounds like the platform downmixer is not working as expected.

andrewlewis avatar Apr 24 '18 08:04 andrewlewis

The audio file was recorded with 8 channels. So, using ChannelMappingAudioProcessor, I have parameters outputChannels = int[8] and outputChannelVolumes = float[8]. I also have outputChannels[1...8] = 1

By changing the value of outputChannelVolumes, I can control left and right speakers. The intention is to have each pair of channels, 0 and 1; 2 and 3; 4 and 5; 6 and 7 work like "4 channels" in a way that inside each of these pairs the sum of outputChannelVolumes is always 1.0f.

For example, outputChannelVolumes[0] = 0.2f and outputChannelVolumes[1] = 0.8f.

So, when I change outputChannelVolumes[0, 4, 6] from 0.0f to 1.0f, I control the volume of the left speaker. The same is true for outputChannelVolumes[1, 5, 7] to control the volume of the right speaker. All this works perfectly.

But, when I change the value of outputChannelVolumes[2 and 3] nothing happens, i.e., the audio keeps coming from left and right speakers in the same proportion, like if both had been set to 1.0f.

roundsmapp avatar Apr 24 '18 13:04 roundsmapp