Created
September 12, 2019 12:07
-
-
Save Feroz-Istar/27220b89865a95119616a101077cdcc5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package convert; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.FileNotFoundException; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.OutputStream; | |
public class PCmTowav { | |
public static void main(String[] args) { | |
// TODO Auto-generated method stub | |
File read = new File("left_1.pcm"); | |
File out = new File("left.wav"); | |
try { | |
PCMToWAV(read, out, 1, 8000, 16); | |
} catch (FileNotFoundException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} catch (IOException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
} | |
/* | |
* static public void PCMToWAV(File input, File output, AudioFormat format) | |
* throws IOException { int bitsPerSample; switch (format.getEncoding()) { case | |
* AudioFormat.ENCODING_PCM_8BIT: bitsPerSample = 8; break; case | |
* AudioFormat.ENCODING_PCM_FLOAT: bitsPerSample = 32; break; case | |
* AudioFormat.ENCODING_PCM_16BIT: default: bitsPerSample = 16; | |
* | |
* } } | |
*/ | |
/** | |
* @param input raw PCM data | |
* limit of file size for wave file: < 2^(2*4) - 36 bytes (~4GB) | |
* @param output file to encode to in wav format | |
* @param channelCount number of channels: 1 for mono, 2 for stereo, etc. | |
* @param sampleRate sample rate of PCM audio | |
* @param bitsPerSample bits per sample, i.e. 16 for PCM16 | |
* @throws IOException in event of an error between input/output files | |
* @see <a href="http://soundfile.sapp.org/doc/WaveFormat/">soundfile.sapp.org/doc/WaveFormat</a> | |
*/ | |
static public void PCMToWAV(File input, File output, int channelCount, int sampleRate, int bitsPerSample) throws IOException { | |
final int inputSize = (int) input.length(); | |
try { | |
OutputStream encoded = new FileOutputStream(output); | |
// WAVE RIFF header | |
writeToOutput(encoded, "RIFF"); // chunk id | |
writeToOutput(encoded, 36 + inputSize); // chunk size | |
writeToOutput(encoded, "WAVE"); // format | |
// SUB CHUNK 1 (FORMAT) | |
writeToOutput(encoded, "fmt "); // subchunk 1 id | |
writeToOutput(encoded, 16); // subchunk 1 size | |
writeToOutput(encoded, (short) 1); // audio format (1 = PCM) | |
writeToOutput(encoded, (short) channelCount); // number of channelCount | |
writeToOutput(encoded, sampleRate); // sample rate | |
writeToOutput(encoded, sampleRate * channelCount * bitsPerSample / 8); // byte rate | |
writeToOutput(encoded, (short) (channelCount * bitsPerSample / 8)); // block align | |
writeToOutput(encoded, (short) bitsPerSample); // bits per sample | |
// SUB CHUNK 2 (AUDIO DATA) | |
writeToOutput(encoded, "data"); // subchunk 2 id | |
writeToOutput(encoded, inputSize); // subchunk 2 size | |
copy(new FileInputStream(input), encoded); | |
}catch(Exception e) { | |
} | |
} | |
/** | |
* Size of buffer used for transfer, by default | |
*/ | |
private static final int TRANSFER_BUFFER_SIZE = 10 * 1024; | |
/** | |
* Writes string in big endian form to an output stream | |
* | |
* @param output stream | |
* @param data string | |
* @throws IOException | |
*/ | |
public static void writeToOutput(OutputStream output, String data) throws IOException { | |
for (int i = 0; i < data.length(); i++) | |
output.write(data.charAt(i)); | |
} | |
public static void writeToOutput(OutputStream output, int data) throws IOException { | |
output.write(data >> 0); | |
output.write(data >> 8); | |
output.write(data >> 16); | |
output.write(data >> 24); | |
} | |
public static void writeToOutput(OutputStream output, short data) throws IOException { | |
output.write(data >> 0); | |
output.write(data >> 8); | |
} | |
public static long copy(InputStream source, OutputStream output) | |
throws IOException { | |
return copy(source, output, TRANSFER_BUFFER_SIZE); | |
} | |
public static long copy(InputStream source, OutputStream output, int bufferSize) throws IOException { | |
long read = 0L; | |
byte[] buffer = new byte[bufferSize]; | |
for (int n; (n = source.read(buffer)) != -1; read += n) { | |
output.write(buffer, 0, n); | |
} | |
return read; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment