Reading data from a .wav file using only native python - python

I am attempting to write a program that detects the frequency of a sound in a .wav file. I would like to do this with exclusively native python, no third-party modules. I used the built-in read() and open() functions and got some strange results:
with open('pcm-test.wav', 'rb') as f:
data = f.read(255)
print data
When I run it, I get this:
>>>
RIFF$ÈWAVEfmt data
>>>
What am I doing wrong? Any advice would be appreciated. Thanks!
EDIT
I suppose I phrased this wrong. I'm looking for the frequency of the tone in the .wav file, not the sample rate. I have an algorithm for computing frequency based on an array of amplitudes, but I have no way of finding it. I guess my question would be how can I get raw amplitude data from the .wav file and store it as a list, tuple, etc.

What am I doing wrong?
Nothing, everything works just fine. Next, parse the WAVE header (http://soundfile.sapp.org/doc/WaveFormat/) and get your sample rate.

Related

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

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!!

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).

Loading Large Files as Dictionary

My first question on stackoverflow :)
I am trying to load a pertained vectors from Glove vectors and create a dictionary with words as keys and corresponding vectors as values. I did the usual naive method to this:
fp=open(wordEmbdfile)
self.wordVectors={}
# Create wordVector dictionary
for aline in fp:
w=aline.rstrip().split()
self.wordVectors[w[0]]=w[1:]
fp.close()
I see a huge memory pressure on Activity Monitor, and eventually after trying for an hour or two it crashes.
I am going to try splitting in multiple smaller files and create multiple dictionaries.
In the meantime I have following questions:
To read the word2vec file, is it better if I read the gzipped file using gzip.open or uncompress it and then read it with plain open.
The word vector file has text in first column and float in the rest, would it be more optimal to use genfromtext or loadtext from numpy?
I intend save this dictionary using chicle, I know loading it is going to be hard too. I read the suggestion to use shelve, how does it compare to cPickle in loading time and access time. May be its better to spend some more time loading with cPickle if improve future accesses (if cPickle does not crash, with 8G RAM), Does anyone have some suggestion on this?
Thanks!

How do you read this .wav file byte data?

I used the python wave module and read the first frame from a .wav file and it returned this :
b'\x00\x00\x00\x00\x00\x00'
What does each byte mean and will it be the same for every frame or for just some?
I've done some research into the subject and have found that there are bytes that give information about the .wav file in front of the sound data, so does python miss out this information and skip straight to the sound data or do I have to manually separate it?
There are 2 channels and a sample width of 3 according to python.
UPDATE
I have successfully created the waveform for the wav file, it wasn't as difficult as I first thought, now to show it whilst the song is playing....
The wave module reads the header for you, which is why it can tell you how many channels there are, and what the sample width is.
Reading frames gives you direct access to the raw sample data, but because the WAV format is a bit of a mixed, confused beast it depends on the sample width and channel count how you need to interpret each frame. See this article for a good in-depth discussion on that.

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.

Categories

Resources