4

I'm creating .wav files using marytts.

The kpbs of .wav files changing according to voice I'm using with the code below. I would like to write every audio file in 128 kpbs.

Due to the program I am planning to use generated .wav files and only supports 128 kpbs, is there a way to write the .wav files always 128kpbs?

This is my code:

AudioInputStream audio = marytts.generateAudio(text); //generate audio from text
AudioSystem.write(audio, AudioFileFormat.Type.WAVE, new File("F:\\temp\\" + filename + ".wav"));//save audio as .wav to the static location with filename
return true;//function completed so return true
Berk Olcay
  • 161
  • 11
  • "WAV file" and "128 kbps" in the same sentence sounds really, really weird... Are you sure you don't actually want some king of lossy compression format (such as MP3) rather than WAV ? – Cyäegha Aug 10 '16 at 12:26
  • I will use the generated audios in Asterisk. I checked the working audio files in Asterisk and, they are all wav files in 128 kpbs. The ones I generated is 256 or 768 kpbs wav files and not working. – Berk Olcay Aug 10 '16 at 12:31
  • 2
    Rhe reason I asked is because uncompressed audio (such as WAV) is typically not specified in terms of bitrate, but in terms of bit depth and sample rate (e.g. 16 bits/44.1 KHz for audio CD quality). Since there is no compression, the bitrate is just the multiplication of the two; e.g. 16 bits x 44.1 KHz = 704 kbps (or 1408 kbps for stereo). Your 768 kbps WAV files are almost certainly 16 bits/48 KHz mono; the 256 kbps, likely 16 bits/16KHz mono. I guess you could always try lowering the sampling rate to 8 KHz (see: http://stackoverflow.com/q/11421874/3801695)? – Cyäegha Aug 10 '16 at 12:51
  • 1
    I've just seen that [this Asterisk-related answer](http://stackoverflow.com/a/2688585/3801695) mentions a distinction between wav (PCM audio - that's what you will get with `AudioFileFormat.Type.WAVE`) and WAV ("GSM encoded" audio, apparently)... Also related, [according to Wikipedia](https://en.wikipedia.org/wiki/Audio_file_format), a `.wav` file doesn't always contain PCM audio, contrary to what I thought. So I think you will also need to clarify what kind of WAV format you need exactly... – Cyäegha Aug 10 '16 at 13:01
  • I have no knowledge of audio and its type. I only know that in the details of the working audio its 128kbps wav. Now I know bitrate is depth and sample rate. Your comments are very useful and helped me :) thanks. – Berk Olcay Aug 11 '16 at 08:27
  • You're welcome! But just for the record, do keep in mind that this simple calculation is only for uncompressed audio. For example, for telephony applications, GSM 06.10 (used in the original GSM standard I think, it's quite old now) uses only 13 kbps (instead of 104 kbps) for 13 bits/8 Khz audio. – Cyäegha Aug 11 '16 at 08:50
  • @Cyäegha Is there a way to increase the sound of the AudioInputStream before writing it? – Berk Olcay Aug 15 '16 at 13:33

1 Answers1

8

I managed to find an answer to my question.

Maybe later someone also ask the same so I'm giving my solution.

Under the class I wrote these global variables as I wanted my wav

static AudioFormat.Encoding defaultEncoding = AudioFormat.Encoding.PCM_SIGNED;
static float fDefaultSampleRate = 8000;
static int nDefaultSampleSizeInBits = 16;
static int nDefaultChannels = 1;
static int frameSize = 2;
static float frameRate= 8000;
static boolean bDefaultBigEndian = false;

And changed my code like this.

I created a format as I wanted, generated audio from text, changed audio in my format and wrote it.

AudioFormat defaultFormat = new AudioFormat(defaultEncoding,fDefaultSampleRate,nDefaultSampleSizeInBits,nDefaultChannels,frameSize,frameRate,bDefaultBigEndian);
AudioInputStream GeneratedAudio = marytts.generateAudio(text); //generate audio from text
AudioInputStream audio = AudioSystem.getAudioInputStream(defaultFormat, GeneratedAudio);
AudioSystem.write(audio, AudioFileFormat.Type.WAVE, new File("F:\\temp\\" + filename + ".wav"));//save audio as .wav to the static location with filename
return true;//function completed so return true
Berk Olcay
  • 161
  • 11