Closed ovistoica closed 1 year ago
Have you tried downsampling to 8khz before switching the encoding to ulaw
?
Apparently this does not work either:
java.lang.IllegalArgumentException: Unsupported conversion: MPEG-1, Layer 3 8000.0 Hz, unknown bits per sample, mono, unknown frame size, 41.666668 frames/second
from MPEG-1, Layer 3 24000.0 Hz, unknown bits per sample, mono, unknown frame size, 41.666668 frames/second
This looks like you tried to downsample mp3 before converting to pcm. I meant, first convert to pcm, then downsample, then switch encodings to ulaw.
I did some digging. This should work:
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.UnsupportedAudioFileException;
import java.io.File;
import java.io.IOException;
import static javax.sound.sampled.AudioFormat.Encoding.PCM_SIGNED;
public class Ulaw {
public static void main(String[] args) throws UnsupportedAudioFileException, IOException {
final AudioInputStream audioInputStream0 = AudioSystem.getAudioInputStream(new File("audio.mp3"));
final AudioInputStream audioInputStream1 = toSignedPCM(audioInputStream0);
final AudioInputStream audioInputStream2 = toMono(audioInputStream1);
final AudioInputStream audioInputStream3 = to8kHz(audioInputStream2);
final AudioInputStream audioInputStream4 = toULAW(audioInputStream3);
System.out.println(audioInputStream4.getFormat());
}
private static AudioInputStream toSignedPCM(final AudioInputStream audioInputStream) {
final AudioFormat format = audioInputStream.getFormat();
final int sampleSizeInBits = format.getSampleSizeInBits() > 0 ? format.getSampleSizeInBits() : 16;
final int channels = Math.min(format.getChannels(), 2);
final int frameSize = format.getFrameSize() > 0 ? format.getFrameSize() : sampleSizeInBits * channels / 8;
final AudioFormat format1 = new AudioFormat(
PCM_SIGNED,
format.getSampleRate(),
sampleSizeInBits,
channels,
frameSize,
format.getSampleRate(),
format.isBigEndian()
);
return AudioSystem.getAudioInputStream(format1, audioInputStream);
}
private static AudioInputStream toMono(final AudioInputStream audioInputStream) {
final AudioFormat format = audioInputStream.getFormat();
final int frameSize = format.getSampleSizeInBits() / 8;
final AudioFormat format1 = new AudioFormat(
PCM_SIGNED,
format.getSampleRate(),
format.getSampleSizeInBits(),
1,
frameSize,
format.getSampleRate(),
format.isBigEndian()
);
return AudioSystem.getAudioInputStream(format1, audioInputStream);
}
private static AudioInputStream to8kHz(final AudioInputStream audioInputStream) {
final AudioFormat format = audioInputStream.getFormat();
float sampleRate = 8000;
final AudioFormat format1 = new AudioFormat(
PCM_SIGNED,
sampleRate,
format.getSampleSizeInBits(),
format.getChannels(),
format.getFrameSize(),
sampleRate,
format.isBigEndian()
);
return AudioSystem.getAudioInputStream(format1, audioInputStream);
}
private static AudioInputStream toULAW(final AudioInputStream audioInputStream) {
final AudioFormat format = audioInputStream.getFormat();
final AudioFormat format1 = new AudioFormat(
AudioFormat.Encoding.ULAW,
format.getSampleRate(),
8,
format.getChannels(),
format.getChannels(),
format.getFrameRate(),
format.isBigEndian()
);
return AudioSystem.getAudioInputStream(format1, audioInputStream);
}
}
You are a life saver, sir! This is exactly what I needed! Thank you so much!
Hello & thank you for this great package!
I'm writing a phone playback service which requires I send base64 encoded MULAW 8000 Hz sample rate audio format as the message to be played.
From the twilio docs
Issue
When I try to convert, I always get unsupported format. Initial mp3 format
"MPEG-1, Layer 3 24000.0 Hz, unknown bits per sample, mono, unknown frame size, 41.666668 frames/second"
Things I tried
Converting using the format directly does not work
If I go from MP3 to my MULAW 8kHz, I get an unsupported conversion error.
Converting to
PCM_SIGNED
, then toULAW 24000 HZ
then toULAW 8000 HZ
Output:
Question
Do you have any idea how I should go about it? I know that this should be supported by
ffmpeg
but it may be that this sample rate is not supported by the libraryCan you point me in the right direction? I have been stuck on this issue for the last 4 days.