How do I write a file in headerless PCM format? - python

How do I write a file in headerless PCM format? The source data is in a numpy array, and I have an application that is expecting the data in a headerless PCM file, but I can't find any documentation of the file format? Is it basically the same as the data chunk in a wave file?

The problem is that there is no one "headerless PCM" format. For example, 8-bit mono 22K and little-endian 16-bit stereo 48K are both perfectly fine examples of PCM, and the whole point of "headerless" is that you need to know that information through some out-of-band channel.
But, assuming you have the expected format, it's as simple as it sounds.
for sample in samples:
for channel in channels:
f.write(the_bytes_in_the_right_endianness)
And if you've already got data in that format, just write it to the file as one big block.
The "default" codec for WAV files—and the only one supported by Python's wave module—is PCM. So, yes, if you have a WAV file in the same format you want, you can just copy its raw frame data into a headerless file, and you've got a headerless PCM file. (And if you have a wav in the wrong format, you can usually use a simple transformation, or at worst something out of audioop, to convert it.)

Related

simple-itk: reading a sequence from a single tiff file

I've got a tif file which contains a series of images. It is a 3D volume of pixels, produced by a CT-scan.
But after reading the image with simple-itk, there is only a single layer:
reader = sitk.ImageFileReader()
reader.SetFileName("FILENAME.tif")
img = reader.Execute()
img.GetDepth() # 0
arr = sitk.GetArrayFromImage(img)
arr.shape # (512, 512, 4)
How can I configure simple-itk to read the whole sequence of images from the tif file ?
Reading the docs for simple-itk shows how to read sequences of images, from sequences of files. That's not what I need, there is a single tif file for the 3D data.
I am able to read this with the tifffile module, it is able to see the correct number of slices and the plots look good, too. So the data itself seems to be valid.
I'm afraid since this is medical data, it's not possible for me to provide a sample. I'm just interested in the corresponding simple-itk documentation.
Likely the Tiff tags are not fully supported by ITK's ImageIO. Providing a sample data file would be best to determine the structure of the tiff file. Without that some information can be obtained with the command line tool tiffinfo from the libtiff tools to share to reveal the structure of the file.

How to convert binary data into image file in Python

transmit image file into small segment (packet)
I used python to transmit image file on UDP sockets as required.
It is required to break the image file into small 'pieces' and in binary format not in string. Size of each packet is 1024B ('content'). And I make the packets.Adding checksum, sequence number,or corrupted data into the packet of 'Sender' is required as well.
need to display package of binary data into image
In the 'Receiver', extract the data from the packet to get 'content'(1024B), is it possible to display the packet data into image? There are transmit in binary format, and I want to show the realtime transmission,eg. data is corrupted, some part of the image(if can be displayed) will be 'blur'.
question
how to display each packet data into an image or cumulative data into images?
Can anyone help? I have searched much but cannot get the solution well.

get the amplitude data from an mp3 audio files using python

I have an mp3 file and I want to basically plot the amplitude spectrum present in that audio sample.
I know that we can do this very easily if we have a wav file. There are lot of python packages available for handling wav file format. However, I do not want to convert the file into wav format then store it and then use it.
What I am trying to achieve is to get the amplitude of an mp3 file directly and even if I have to convert it into wav format, the script should do it on air during runtime without actually storing the file in the database.
I know we can convert the file like follows:
from pydub import AudioSegment
sound = AudioSegment.from_mp3("test.mp3")
sound.export("temp.wav", format="wav")
and it creates the temp.wav which it supposed to but can we just use the content without storing the actual file?
MP3 is encoded wave (+ tags and other stuff). All you need to do is decode it using MP3 decoder. Decoder will give you whole audio data you need for further processing.
How to decode mp3? I am shocked there are so few available tools for Python. Although I found a good one in this question. It's called pydub and I hope I can use a sample snippet from author (I updated it with more info from wiki):
from pydub import AudioSegment
sound = AudioSegment.from_mp3("test.mp3")
# get raw audio data as a bytestring
raw_data = sound.raw_data
# get the frame rate
sample_rate = sound.frame_rate
# get amount of bytes contained in one sample
sample_size = sound.sample_width
# get channels
channels = sound.channels
Note that raw_data is 'on air' at this point ;). Now it's up to you how do you want to use gathered data, but this module seems to give you everything you need.

how to create BMP image from binary data in Python?

Suppose I already read binary data from a binary file, how can I create a BMP image from that binary data?
You can find a definition of the bitmap file format on Wikipedia among other places. Use the struct module to create the necessary headers. Because the format is uncompressed it is very easy to write out. The color information must come in BGR order, bottom line to top line, and each line must be padded with zeros to a multiple of 4 bytes.
Or if you'd rather do it the easy way, PIL knows how to read and write BMP.

Determine MP3 bit depth in Python via Mutagen

Is there a way to determine an MP3 file's encoded bit depth (ie 8, 16, 24, 32) in Python using the Mutagen library?
The transformations done by the MP3 encoding process drop completely the concept of “bit depth”. You can only know the bit depth of the source audio if such information was stored in a tag of the MP3 file. Otherwise, you can take the MP3 data and produce 8-bit, 16-bit or 24-bit audio.
I've not heard "bit depth" with regard to mp3s so I'm assuming you mean bit rate. From the Mutagen tutorial:
from mutagen.mp3 import MP3
audio = MP3("example.mp3")
print audio.info.length, audio.info.bitrate
That second portion (audio.info.bitrate) should be what you need.

Categories

Resources