Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java how to write 128 kpbs wav file

Tags:

java

audio

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
like image 521
Berk Olcay Avatar asked Mar 11 '23 11:03

Berk Olcay


1 Answers

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
like image 122
Berk Olcay Avatar answered Mar 21 '23 01:03

Berk Olcay