3

Recommendations welcome on how to slice a .wav1 file into time-delimited segments using a Python library.

1 The actual file type isn't really that material, I'm sure I'll be able to convert between different types if needed.

Tim McNamara
  • 18,019
  • 4
  • 52
  • 83

2 Answers2

3

I would use the wave module to open the file, read the headers, figure out how many frames are in 10 seconds, then read that many frames. Write those frames out to files with the same header info (except length) until done.

Coder404
  • 742
  • 2
  • 7
  • 21
cobbal
  • 69,903
  • 20
  • 143
  • 156
2

I would suggest looking at the data structure for a given file, and 'cutting' the data at an appropriate point along the line so no frames are chopped off early.

This would mean looking at the frequency of the recording and the bit rate, and using that to get the size (in bits) of each frame. Then you can take segments of audio without cutting individual frame data.

Have a look at this SO posting. It suggests treating your audio as a binary read string. As it's a string you can basically copy, cut and move the string as you want to a new output file.

Check this one out: http://docs.python.org/library/binascii.html

Also worth looking at: https://ccrma.stanford.edu/courses/422/projects/WaveFormat/

Either that or just keep it as binary and use byte arrays. Need to think about the header file and what happens to that, although each format is different. MP3 is easy to keep the header as it is interleaved amongst the data:

http://en.wikipedia.org/wiki/Mp3#File_structure

Ok, a bunch of stuff.


FINALLY: One you'll no doubt have seen already: http://sourceforge.net/projects/audiotools/

Updated....

Use the bits_per_sample() method in the audio tools link from sourceforge.net

--Returns the number of bits-per-sample in this audio file as a positive integer.

Then divide your audio into a byte array using that info and some of the info from above. You can then at least reconstruct accurately some RAW audio data.

You can take the length of the file in bits and divide it by 16. You can then use a method divide the array according to time in milliseconds. It sounds complicated but it's really rudimentary maths.

Community
  • 1
  • 1
Alex
  • 4,844
  • 7
  • 44
  • 58
  • this is a really useful answer. I haven't accepted it because it doesn't answer the question specifically enough, e.g. doesn't detail the library and possibly provide usage instructions. – Tim McNamara Nov 01 '10 at 21:06
  • @Tim McNamara I've updated the post to include a few links. The last one should hopefully give you some quick results for what you're after. – Alex Nov 01 '10 at 23:59
  • No problem, I enjoyed helping. – Alex Nov 15 '10 at 21:28