Wav Dump example

From The Foundry MODO SDK wiki
Jump to: navigation, search

Demonstration script. Dumps audio from the timeline to a WAV file.

#!/usr/bin/env python
 
################################################################################
#
# wavdump.py
#
# Version: 1.000
#
# Author: Gwynne Reddick
#
################################################################################
 
import traceback, wave
 
def main():
    inpoint = lx.eval('time.range current in:?')
    outpoint = lx.eval('time.range current out:?')
    asvc = lx.service.AudioAnim()
    aud = asvc.Preview(inpoint, outpoint)
 
    nchannels = aud.Channels()
    duration = aud.Duration()
    freq = aud.Frequency()
    atype = aud.Type()
    nframes = 16 * 2 * duration
 
    storage = aud.Data()
    storage.setType('b')
    storage.setSize(freq * nchannels * (atype / 8) * duration)
    buf = str(bytearray(storage.get()))
 
    wout = wave.open(r'C:\Data\wavout.wav', 'wb')
    wout.setparams((nchannels,(atype / 8),freq,nframes,'NONE', 'not compressed'))
    wout.writeframesraw(buf)
    wout.writeframes('')
    wout.close()
 
 
if __name__ == '__main__':
    try:
        main()
    except:
        lx.out(traceback.format_exc())