Reputation: 15890
I would like to use some features of TarsosDSP on sound data. The incoming data is Stereo, but Tarsos does only support mono, so I tried to transfer it to mono as follows, but the result still sounds like stereo data interpreted as mono, i.e. the conversion via MultichannelToMono
doesn't seem to have any effect, although its implementation looks good upon a quick glance.
@Test
public void testPlayStereoFile() throws IOException, UnsupportedAudioFileException, LineUnavailableException {
AudioDispatcher dispatcher = AudioDispatcherFactory.fromFile(FILE,4096,0);
dispatcher.addAudioProcessor(new MultichannelToMono(dispatcher.getFormat().getChannels(), false));
dispatcher.addAudioProcessor(new AudioPlayer(dispatcher.getFormat()));
dispatcher.run();
}
Is there anything that I do wrong here? Why does the MultichannelToMono
processor not transfer the data to mono?
Upvotes: 1
Views: 422
Reputation: 15890
The only way I found which works is to use the Java Audio System for performing this conversion before sending the data to TarsosDSP, it seems it does not convert the framesize correctly
I found the following snippet at https://www.experts-exchange.com/questions/26925195/java-stereo-to-mono-conversion-unsupported-conversion-error.html which I use to convert to mono before applying more advanced audio transformations with TarsosDSP.
public static AudioInputStream convertToMono(AudioInputStream sourceStream) {
AudioFormat sourceFormat = sourceStream.getFormat();
// is already mono?
if(sourceFormat.getChannels() == 1) {
return sourceStream;
}
AudioFormat targetFormat = new AudioFormat(
sourceFormat.getEncoding(),
sourceFormat.getSampleRate(),
sourceFormat.getSampleSizeInBits(),
1,
// this is the important bit, the framesize needs to change as well,
// for framesize 4, this calculation leads to new framesize 2
(sourceFormat.getSampleSizeInBits() + 7) / 8,
sourceFormat.getFrameRate(),
sourceFormat.isBigEndian());
return AudioSystem.getAudioInputStream(targetFormat, sourceStream);
}
Upvotes: 1