Script Links issue - probably an easy fix

I’m trying to make a visualizer in blender, but at the moment I’m stuck on the simplest thing. This script is run every frame:


import Blender

if (Blender.Get('curframe')==1):
    import wave
    snd = wave.open('C:\Documents and Settings\harry van ness\My Documents\Blendah\GirlAndTheSea.wav', 'rb');
else:
    print snd.tell()
    print snd.readframes(1)

‘snd’ doesn’t seem to exist in any frame other than the first. It seems that it needs to be defined in the same frame in which it is used. Is there a way to make snd globally accessible.
thanks

Note that each time a script link is executed, it’s considered a discrete event from other executions. Hence something like following might do the trick:


import wave
import Blender

WAVE_FILE = 'C:\Documents and Settings\harry van ness\My Documents\Blendah\GirlAndTheSea.wav'

snd = wave.open(WAVE_FILE, 'rb')
cur_frame = Blender.Get('curframe')

snd.setpos(cur_frame - 1) # move cursor position before the current frame
print snd.tell()
print snd.readframes(1) # just get the wave sample at current frame

I haven’t tested it but I hope it gives you the idea. :slight_smile:

cool, that sortof works. Of course, the print snd.tell() goes after the setpos, but you had the right idea. However, the print snd.readframes doesn’t work. It just turns up blank… I havn’t actually managed to read it in blender yet, only in python, in cmd

edit: it works in the interactive console…

It prints out sound fragments just fine here if I hook up the above script to a FrameChanged script link. I had some problems finding a wave file that would get loaded by the wave module but after I found once it worked out just fine.

really? I find that extremely surprising.


str = '\x00\x00\x00\x00'
print len(str) #gives 4
print str #blank

It looks like python sees it as a regular expression. People have written books on regex and even after looking around the web for about an hour, im no closer to knowing what to do

It’s up to you to decide what to do with the data. See http://docs.python.org/library/wave.html#wave.Wave_read.readframes and http://mail.python.org/pipermail/tutor/2004-September/032026.html for reference.


import wave
import Blender
from struct import *

WAVE_FILE = 'C:\Documents and Settings\harry van ness\My Documents\Blendah\GirlAndTheSea.wav'

snd = wave.open(WAVE_FILE, 'rb')
cur_frame = Blender.Get('curframe')

snd.setpos(cur_frame+9999
print unpack('2h', snd.readframes(1))

print snd.tell()

that works :smiley: