9

I would like to use markers in .wav files.

It seems to be supported by aifc module with getmarkers() : http://docs.python.org/2/library/aifc.html#aifc.aifc.getmarkers (for .aiff files), but not for wave module (http://docs.python.org/2/library/wave.html?highlight=wave#wave.Wave_read.getmarkers).

How could we read markers of .wav files ?

Basj
  • 41,386
  • 99
  • 383
  • 673

2 Answers2

4

Edit: here is an updated version of scipy.io.wavfile that adds many things (24 bit .wav files support for read/write, cue markers, cue markers labels, and some other metadata like pitch (if defined), etc.):

wavfile.py (enhanced)

Feel free to share it!


I finally found a solution (it uses some function of scipy.io.wavfile) :

def readmarkers(file, mmap=False):
    if hasattr(file,'read'):
        fid = file
    else:
        fid = open(file, 'rb')
    fsize = _read_riff_chunk(fid)
    cue = []
    while (fid.tell() < fsize):
        chunk_id = fid.read(4)
        if chunk_id == b'cue ':
            size, numcue = struct.unpack('<ii',fid.read(8))
            for c in range(numcue):
              id, position, datachunkid, chunkstart, blockstart, sampleoffset = struct.unpack('<iiiiii',fid.read(24))
              cue.append(position)
        else:
            _skip_unknown_chunk(fid)
    fid.close()
    return cue

Feel free to add it into Scipy's wavfile.py if someone is interested.

Basj
  • 41,386
  • 99
  • 383
  • 673
  • My markers are of type 'range' (id,start,end,duration ?). With your function I can only read the start position of each range. How can I modify your function in order to make it work for range too ? – Eric Jan 15 '14 at 15:04
  • This is the reference that I used : http://www.sonicspot.com/guide/wavefiles.html ... Where are stored your markers of type `range` ? In `Cue Chunk` (http://www.sonicspot.com/guide/wavefiles.html#cue) or `Playlist Chunk` (http://www.sonicspot.com/guide/wavefiles.html#plst) ? – Basj Jan 15 '14 at 19:51
  • In case it may help someone, your code was working good for me but it raised an exception in _skip_unknown_chunk because it was finding something it could not read. Since this was happening after the markers were correctly read I just added a break after the for loop :) (I added markers with adobe audition) – wizbcn Jun 27 '14 at 11:00
  • Was this ever submitted to scipy? – endolith May 02 '19 at 16:48
  • Yes it's here: https://github.com/scipy/scipy/pull/6852 and another version here https://github.com/X-Raym/wavfile.py – endolith May 02 '19 at 17:11
-2

it is in wave.Wave_read modules, called Wave_read.getmarkers() see the docs for detail: http://docs.python.org/2/library/wave.html?highlight=wave#wave.Wave_read.getmarkers

Luyi Tian
  • 371
  • 1
  • 12
  • 2
    I honestly can't understand why this doesn't raise `NotImplemented` instead – loopbackbee Dec 04 '13 at 16:21
  • FWIW, if you want to implement this yourself, there's a lot of good information [here](http://home.roadrunner.com/~jgglatt/tech/wave.htm) (see the section on cue points) – loopbackbee Dec 04 '13 at 16:25
  • I don't have enough skills (yet) about how to open files, go somewhere specific in a file, extract a chunk, etc. to do it myself unfortunately... About `NotImplemented`, how can we set it to this status `NotImplemented` ? – Basj Dec 04 '13 at 19:59
  • Do you think you could help me @goncalopp? – Basj Dec 04 '13 at 22:15
  • 1
    @Basj I don't have much time ATM, nor do I have access to a wave file with known markers. If you can send me one I may be able to come up with something in a day or two. Otherwise, this is a great opportunity to learn about [open](http://docs.python.org/2/library/functions.html#open), [files](http://docs.python.org/2/tutorial/inputoutput.html#methods-of-file-objects) and [struct](http://docs.python.org/2/library/struct.html) – loopbackbee Dec 05 '13 at 09:44
  • that's what I finally did, and I learnt lots of interesting things on open, files, and struct! (see my answer) – Basj Dec 05 '13 at 09:49