Raw to wav python

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.

Читайте также:  What are jar files in java

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
Читайте также:  How To Create A Responsive Image Slider

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:

and a link to more information on the subject:

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 .

Источник

How can I convert a Raw Data file of Audio in .Wav with python

Knowing some things like : Encoding : 16bit pcm, Byte order Little Endian , Channels : 1 Mono, Start offset : 0 bytes , Amount to import : 100% and Sample rate 16000 Hz.

That Characteristics were given by Audacity, in that way I can hear it in this program.

But I want to do it in Python code.

Yes, can do this using the wave library or wavio api

Open The wave file in write binary mode.

wave.open(file[, mode]) If file is a string, open the file by that name, otherwise treat it as a seekable file-like object. mode can be any of

Set all the parameter of the wav file as you have shared w.r.t the audacity

Wave_write.setparams(tuple) The tuple should be (nchannels, sampwidth, framerate, nframes, comptype, compname), with values valid for the set*() methods. Sets all parameters.

Open the raw file in binary mode and pass the data to these function.

Wave_write.writeframesraw(data)¶ Write audio frames, without correcting nframes.

Wave_write.writeframes(data) Write audio frames and make sure nframes is correct.

once done close the file handle.

Wave_write.close() Make sure nframes is correct, and close the file if it was opened by wave. This method is called upon object collection.

I hope you can implement the python code based on these description.

Collected from the Internet

Please contact javaer1[email protected] to delete if infringement.

Источник

audioread 3.0.0

Decode audio files using whichever backend is available. The library currently supports:

  • Gstreamer via PyGObject.
  • Core Audio on Mac OS X via ctypes. (PyObjC not required.)
  • MAD via the pymad bindings.
  • FFmpeg or Libav via its command-line interface.
  • The standard library wave, aifc, and sunau modules (for uncompressed audio formats).
with audioread.audio_open(filename) as f: print(f.channels, f.samplerate, f.duration) for buf in f: do_something(buf)

Buffers in the file can be accessed by iterating over the object returned from audio_open . Each buffer is a bytes-like object ( buffer , bytes , or bytearray ) containing raw 16-bit little-endian signed integer PCM data. (Currently, these PCM format parameters are not configurable, but this could be added to most of the backends.)

Additional values are available as fields on the audio file object:

  • channels is the number of audio channels (an integer).
  • samplerate is given in Hz (an integer).
  • duration is the length of the audio in seconds (a float).

The audio_open function transparently selects a backend that can read the file. (Each backend is implemented in a module inside the audioread package.) If no backends succeed in opening the file, a DecodeError exception is raised. This exception is only used when the file type is unsupported by the backends; if the file doesn’t exist, a standard IOError will be raised.

A second optional parameter to audio_open specifies which backends to try (instead of trying them all, which is the default). You can use the available_backends function to get a list backends that are usable on the current system.

Audioread supports Python 3 (3.6+).

Example

The included decode.py script demonstrates using this package to convert compressed audio files to WAV files.

Version History

Drop support for Python 2 and older versions of Python 3. The library now requires Python 3.6+. Increase default block size in FFmpegAudioFile to get slightly faster file reading. Cache backends for faster lookup (thanks to @bmcfee). Audio file classes now inherit from a common base AudioFile class.

Work correctly with GStreamer 1.18 and later (thanks to @ssssam).

Fix an unhandled OSError when FFmpeg is not installed.

Properly close some filehandles in the FFmpeg backend (thanks to @RyanMarcus and @ssssam). The maddec backend now always produces bytes objects, like the other backends (thanks to @ssssam). Resolve an audio data memory leak in the GStreamer backend (thanks again to @ssssam). You can now optionally specify which specific backends audio_open should try (thanks once again to @ssssam). On Windows, avoid opening a console window to run FFmpeg (thanks to @flokX).

Fix a “no such process” crash in the FFmpeg backend on Windows Subsystem for Linux (thanks to @llamasoft). Avoid suppressing SIGINT in the GStreamer backend on older versions of PyGObject (thanks to @lazka).

Properly clean up the file handle when a backend fails to decode a file. Fix parsing of “N.M” channel counts in the FFmpeg backend (thanks to @piem). Avoid a crash in the raw backend when a file uses an unsupported number of bits per sample (namely, 24-bit samples in Python < 3.4). Add a __version__ value to the package.

Fix a bug in the FFmpeg backend where, after closing a file, the program’s standard input stream would be “broken” and wouldn’t receive any input.

Avoid some warnings in the GStreamer backend when using modern versions of GLib. We now require at least GLib 2.32.

Fix a file descriptor leak when opening and closing many files using GStreamer.

Just fix ReST formatting in the README.

The FFmpeg backend can now also use Libav’s avconv command. Fix a warning by requiring GStreamer >= 1.0. Fix some Python 3 crashes with the new GStreamer backend (thanks to @xix-xeaon).

The GStreamer backend now uses GStreamer 1.x via the new gobject-introspection API (and is compatible with Python 3).

When running FFmpeg on Windows, disable its crash dialog. Thanks to jcsaaddupuy.

Fix an unhandled exception when opening non-raw audio files (thanks to aostanin). Fix Python 3 compatibility for the raw-file backend.

Add support for FFmpeg on Windows (thanks to Jean-Christophe Saad-Dupuy).

Add support for Sun/NeXT Au files via the standard-library sunau module (thanks to Dan Ellis).

Use the rawread (standard-library) backend for .wav files.

Send SIGKILL, not SIGTERM, to ffmpeg processes to avoid occasional hangs.

When GStreamer fails to report a duration, raise an exception instead of silently setting the duration field to None.

Catch GStreamer’s exception when necessary components, such as uridecodebin , are missing. The GStreamer backend now accepts relative paths. Fix a hang in GStreamer when the stream finishes before it begins (when reading broken files). Initial support for Python 3.

All decoding errors are now subclasses of DecodeError .

Fix opening WAV and AIFF files via Unicode filenames.

Make FFmpeg timeout more robust. Dump FFmpeg output on timeout. Fix a nondeterministic hang in the Gstreamer backend. Fix a file descriptor leak in the MAD backend.

Fix crash when FFmpeg fails to report a duration. Fix a hang when FFmpeg fills up its stderr output buffer. Add a timeout to ffmpeg tool execution (currently 10 seconds for each 4096-byte read); a ReadTimeoutError exception is raised if the tool times out.

Fix channel count detection for FFmpeg backend.

Fix a problem with the Gstreamer backend where audio files could be left open even after the GstAudioFile was “closed”.

Fix a hang in the GStreamer backend that occurs occasionally on some platforms.

Et Cetera

audioread is by Adrian Sampson. It is made available under the MIT license. An alternative to this module is decoder.py.

Источник

Оцените статью