Hello. I'm new here, and new to Python. I have a little experience with C++, and assembly, and just started learning Python about 2 weeks ago.
I'm trying to write a program that will control servo motors in sync with an audio file (probably .wav). So far I've written programs moving the a servo in sawtooth, triangular, and sine wave patterns, and I have .wav files playing through winsound.
I tried chopping the .wav file using Wave_read.readframes(n) from the wave module and passing that to winsound.PlaySound(sound, flags), but it's giving me a "RuntimeError: Failed to play sound" error.
import winsound import sys import wave import winsound import time def play_sound(file): if sys.platform.startswith('win'): from winsound import PlaySound, SND_FILENAME, SND_ASYNC, SND_MEMORY winsound.PlaySound(file,SND_MEMORY) elif platform.find('linux')>-1: from wave import open as waveOpen from ossaudiodev import open as ossOpen s = waveOpen('tada.wav','rb') (nc,sw,fr,nf,comptype, compname) = s.getparams( ) dsp = ossOpen('/dev/dsp','w') try: from ossaudiodev import AFMT_S16_NE except ImportError: if byteorder == "little": AFMT_S16_NE = ossaudiodev.AFMT_S16_LE else: AFMT_S16_NE = ossaudiodev.AFMT_S16_BE dsp.setparameters(AFMT_S16_NE, nc, fr) data = s.readframes(nf) s.close() dsp.write(data) dsp.close() a = wave.open('tada.wav','rb') b = a.getnframes() c = a.readframes(b) play_sound(c)
The idea is to chop the audio into 0.05 second intervals, moving the motors 1 step in between each time, thus keeping the motors in sync with the sound.
I suspect that even if I get Python to do this, there will be other problems, such as the sound file not sounding right.
I'm open to any suggestions about either getting this code to work, or completely different approaches to the synchronization problem.