How to Convert .Pcm Files to .Wav Files (Scripting)

How to convert .pcm files to .wav files (scripting)

You don't actually need a tool for this. The Python standard library comes with the wave module for writing .wav files, and of course raw .pcm files can be just opened as plain binary files, and if you need to do any simple transformations that aren't trivial with a list comprehension, they're trivial with audioop.

For example, this is a complete program from converting stereo 16-bit little-endian 44.1k PCM files to WAV files:

import sys
import wave

for arg in sys.argv[1:]:
with open(arg, 'rb') as pcmfile:
pcmdata = pcmfile.read()
with wave.open(arg+'.wav', 'wb') as wavfile:
wavfile.setparams((2, 2, 44100, 0, 'NONE', 'NONE'))
wavfile.writeframes(pcmdata)

In older versions of Python, you may have to use with contextlib.closing(wave.open(…)) (or an explicit open and close instead of a with statement).

Convert PCM WAV to normal WAV in python

You've misunderstood the error message. PCM is intrinsic to the wave file format. There is no "PCM" version, and then a "Normal" version - The wave file format always uses Pulse Code Modulation (PCM) - that really just means that the samples that make up your signal are quantized digitally and contiguous. If your speech_recognition function can't parse the wave file, it's not because of anything related to PCM.

I don't know anything about the SpeechRecognition module (I'm assuming that's what you're using?). I also don't know anything about pjsua. My guess is that pjsua is possibly baking in some additional chunks in the header meta-data, which the SpeechRecognition API isn't expecting. Is there any chance you can share the wave file via dropbox, etc?

Also, the reason your audio sounded like "ants talking" is because of the discrepency between the meta-data your wave file contains, and the meta-data you wrote to your new wave file.
Your wave file is mono - that means one channel, you wrote two. Your file also has a samplerate of 16khz, but you wrote 44.1khz.

Can ffmpeg convert audio from raw PCM to WAV?

The wav container just adds a simple header to the raw PCM data. The header includes the format, sample rate, and number of channels. Since the raw PCM data does not include this information, you will need to specify it on the command line. Options are specified before the file they apply to, so options before the input file may be used to specify the format of the input file, and options after the input file and before the output file may be used to specify the desired format of the output file. If you want the same bits/sample, sample rate, and number of channels in the output file then you don't need any output options in this case; the wav container format is already indicated by the file extension.

Example to convert raw PCM to WAV:

ffmpeg -f s16le -ar 44.1k -ac 2 -i file.pcm file.wav
  • -f s16le … signed 16-bit little endian samples
  • -ar 44.1k … sample rate 44.1kHz
  • -ac 2 … 2 channels (stereo)
  • -i file.pcm … input file
  • file.wav … output file

Writing PCM recorded data into a .wav file (java android)

I've been wrestling with this exact same question for hours now, and my issue was mostly that when recording in 16 bits you have to be very careful about what you write to the output. The WAV file expects the data in Little Endian format, but using writeShort writes it to the output as Big Endian. I also got interesting results when using the other functions so I returned to writing bytes in the correct order and that works.

I used a Hex editor extensively while debugging this. I can recommend you do the same. Also, the header in the answer above works, I used it to check versus my own code and this header is rather foolproof.

Convert GSM Audio to WAV PCM

Here is a link to a C library that encodes and decodes GSM files:

http://user.cs.tu-berlin.de/~jutta/gsm/gsm-1.0.13.tar.gz

and a link to more information on the subject:

http://user.cs.tu-berlin.de/~jutta/toast.html

It should be possible to either compile the C code as a DLL and call it from a C# application using PInvoke, or else incorporate the methods directly into your C# app.

Once you have the GSM data decoded into sample data, writing it out to a WAV file is very simple.

How to reconstruct PCM samples from raw bytes?

As stated in the comments, reconstructing the PCM in pure JavaScript is tedious. I used therefore the node-package node-wav (only works if header is included) as follows:

Store the header of the specific wav options:

const header = Buffer.from([82, 73, 70, 70, 248, 167, ... ])

and concatenate the data as follows:

import wav from 'node-wav';

AudioRecord.on('data', data => {
const chunks = [header]
const chunk = Buffer.from(data, 'base64');
chunks.push(chunk)
const pcm = wav.decode(Buffer.concat(chunks)).channelData
});

This approach works only if you can reconstruct the header. The package node-wave on the other hand is stated as high performance WAV decoder and encoder .



Related Topics



Leave a reply



Submit