pydub copying, resaving and splitting 192kHz sample rate wav file - python

I have .wav files sampled at 192kHz and want to split them based on time to many smaller files while keeping the same sample rate.
To start with I thought I would just open and re-save the wav file using pydub in order to learn how to do this. However when I save it it appears to resave at a much lower file size, I'm not sure why, perhaps the sample rate is lower? and I also can't open the new file with the audio analysis program I usually use (Song scope).
So I had two questions:
- How to open, read, copy and resave a wav file using pydub without changing it? (Sorry I know this is probably easy I just can't find it yet).
Whether Python and Pydub are a sensible choice for what I am trying to do? Or maybe there is a much simpler way.
what I am exactly trying to do is:
Split about 10 high sample frequency wav files (~ 1GB each) into many
(about 100) small wave files. (I plan to make a list of start and end
times for each of the smaller wav files needed then get Python to open
copy and resave the wav file data between those times).
I assume it is possible since I've seen questions for lower frequency wav files, but if you know otherwise or know of a simpler way please let me know. Thanks!!
My code so far is as follows:
from pydub import AudioSegment
# Input audio file to be sliced
audio = AudioSegment.from_wav("20190212_164446.wav")
audio.export("newWavFile.wav")
(I put the wav file and ffmpeg into the same directory as the Python file to save time since was having a lot of trouble getting pydub to find ffmpeg).
In case it's relevant the files are of bat calls, these bats make calls between around 1kHz and 50kHz which is quite low frequency for bats. I'm trying to crop out the actual calls from some very long files.
I know this is a basic question, I just couldn't find the answer yet, please also feel free to direct me to the answer if it's a duplicate.
thanks!!

Related

How to prevent audio generation loss in python?

I have a script that needs to run audio through ffmpeg and pydub multiple times each. I realized that by the end of all this the audio sounds a bit muffled compared to the original. What steps can I take to make sure the quality is as high as possible at the end? I'm going from mp3/m4b to wav btw.

Return type of wavfile.read in python

I need to analyse a sound file, in order to get when the sound is louder.
I have this :
rate, data = wavfile.read('test.wav')
I know the meaning of the rate value, but what really is in the data variable ?
It works well when I want to retrieve the time intervals of the louder part of the audio, by looking at the data list, but I can't really find out the meaning of this list...
Thank you very much
data holds a numpy array representing the sound in your .wav file.
Some good explanations on how the sound is represented in that data can be found in the following question:
What do the bytes in a .wav file represent?
data in wav files is audio samples. Most of the time it's 16bit signed integers. For wav files you mostly care about rate (sound frequency) and number of channels (if your wav file is not mono).

Python reading and writing sound frequencies to wav files

I'm new to python programming & have no knowledge at all of creating and working with sound files.
I am looking for some help with the following:
Using python I want to be able to create a wav file and write sound frequencies to that file.
In another part of the program I am writing I also want to be able to read back those sound frequencies.
Also knowing nothing about sound what so ever. If it right sound frequencies to a file. Are sound frequencies on there own enough to create a sound on playback, or is additional data required?
Also the values contained within the file I am using are very low values. as low as zero, so to make them audible I assume I would have to add an additional value to the value read from the file in order to hear that frequency on playback?
Thanks in advance for any help that I receive!
Clinton.

Estimating zip size/creation time

I need to create ZIP archives on demand, using either Python zipfile module or unix command line utilities.
Resources to be zipped are often > 1GB and not necessarily compression-friendly.
How do I efficiently estimate its creation time / size?
Extract a bunch of small parts from the big file. Maybe 64 chunks of 64k each. Randomly selected.
Concatenate the data, compress it, measure the time and the compression ratio. Since you've randomly selected parts of the file chances are that you have compressed a representative subset of the data.
Now all you have to do is to estimate the time for the whole file based on the time of your test-data.
I suggest you measure the average time it takes to produce a zip of a certain size. Then you calculate the estimate from that measure. However I think the estimate will be very rough in any case if you don't know how well the data compresses. If the data you want to compress had a very similar "profile" each time you could probably make better predictions.
If its possible to get progress callbacks from the python module i would suggest finding out how many bytes are processed pr second ( By simply storing where in the file you where at start of the second, and where you are at the end ). When you have the data on how fast the computer your on you can off course save it, and use it as a basis for your next zip file. ( I normally collect about 5 samples before showing a time prognosses )
Using this method can give you Microsoft minutes so as you get more samples you would need to average it out. This would esp be the case if your making a zip file that contains a lot of files, as ZIP tends to slow down when compressing many small files compared to 1 large file.
If you're using the ZipFile.write() method to write your files into the archive, you could do the following:
Get a list of the files you want to zip and their relative sizes
Write one file to the archive and time how long it took
Calculate ETA based on the number of files written, their size, and how much is left.
This won't work if you're only zipping one really big file though. I've never used the zip module myself, so I'm not sure if it would work, but for small numbers of large files, maybe you could use the ZipFile.writestr() function and read in / zip up your files in chunks?

How to edit raw PCM audio data without an audio library?

I'm interested in precisely extracting portions of a PCM WAV file, down to the sample level. Most audio modules seem to rely on platform-specific audio libraries. I want to make this cross platform and speed is not an issue, are there any native python audio modules that can do this?
If not, I'll have to interpret the PCM binary. While I'm sure I can dig up the PCM specs fairly easily, and raw formats are easy enough to walk, I've never actually dealt with binary data in Python before. Are there any good resources that explain how to do this? Specifically relating to audio would just be icing.
I read the question and the answers and I feel that I must be missing something completely obvious, because nobody mentioned the following two modules:
audioop: manipulate raw audio data
wave: read and write WAV files
Perhaps I come from a parallel universe and Guido's time machine is actually a space-time machine :)
Should you need example code, feel free to ask.
PS Assuming 48kHz sampling rate, a video frame at 24/1.001==23.976023976… fps is 2002 audio samples long, and at 25fps it's 1920 audio samples long.
I've only written a PCM reader in C++ and Java, but the format itself is fairly simple. A decent description can be found here: http://ccrma.stanford.edu/courses/422/projects/WaveFormat/
Past that you should be able to just read it in (binary file reading, http://www.johnny-lin.com/cdat_tips/tips_fileio/bin_array.html) and just deal with the resulting array. You may need to use some bit shifting to get the alignments correct (https://docs.python.org/reference/expressions.html#shifting-operations) but depending on how you read it in, you might not need to.
All of that said, I'd still lean towards David's approach.
Is it really important that your solution be pure Python, or would you accept something that can work with native audio libraries on various platforms (so it's effectively cross-platform)? There are several examples of the latter at http://wiki.python.org/moin/PythonInMusic
Seems like a combination of open(..., "rb"), struct module, and some details about the wav/riff file format (probably better reference out there) will do the job.
Just curious, what do you intend on doing with the raw sample data?
I was looking this up and I found this: http://www.swharden.com/blog/2009-06-19-reading-pcm-audio-with-python/
It requires Numpy (and matplotlib if you want to graph it)
import numpy
data = numpy.memmap("test.pcm", dtype='h', mode='r')
print "VALUES:",data
Check out the original author's site for more details.

Categories

Resources