def save_as(self, filename): if filename is None: raise Exception("No filename") f = pysndfile.sndfile(filename, mode='write', format=self._format, channels=self.numchan(), samplerate=self.samplerate) f.write_frames(self.frames) f.close() self.filename = filename self._saved_revision = self.history.revision()
def __init__(self, filename=None): self.filename = filename self.history = history.History() self.changed = Signal() if filename == None: # empty sound self.frames = numpy.array([]) self.samplerate = 44100 self._saved_revision = None self._format = pysndfile.formatinfo() else: filename = os.path.expanduser(filename) f = pysndfile.sndfile(filename) nframes = f.get_nframes() self.frames = f.read_frames(nframes) self.samplerate = f.get_samplerate() self._format = f._format f.close() self._saved_revision = self.history.revision()
def testPlayer(): from gum.lib.mock import Mock from math import sin SR = 44100 f0 = 440 time = 1 sine = numpy.array([sin(2 * 3.14 * f0 / SR * x) for x in range(time * SR)]) sound = Mock({"numchan": 1}) sound.samplerate = 44100 sound.frames = sine player = Player(sound) player.thread_play().join() from gum.lib import pysndfile import gum f = pysndfile.sndfile(gum.basedir + '/data/test/test1.wav') data = f.read_frames(f.get_nframes()) sound.frames = data player.set_sound(sound) player.thread_play().join() player.thread_play().join() # Testing position player.start = 40000 player.thread_play().join() player.start = 0 # Test reentrancy print("Two threads will attempt to play at a small interval, you should " "hear the first one being interrupted by the second one.") from time import sleep t1 = player.thread_play() sleep(0.5) t2 = player.thread_play() t1.join() t2.join() print("Two threads will attempt to play simultaneously, you should " "hear only one.") from time import sleep t1 = player.thread_play() t2 = player.thread_play() t1.join() t2.join() # Testing stop print() print("Testing stop(): the sound should stop after 0.3 seconds.") player.thread_play() sleep(0.3) player.stop() # Testing stereo f = pysndfile.sndfile(gum.basedir + '/data/test/test2.wav') data = f.read_frames(f.get_nframes()) sound = Mock({"numchan": 2}) sound.samplerate = 44100 sound.frames = data player = Player(sound) player.thread_play().join()
def testPlayer(): from gum.lib.mock import Mock from math import sin SR = 44100 f0 = 440 time = 1 sine = numpy.array([sin(2 * 3.14 * f0/SR * x) for x in range(time * SR)]) sound = Mock({"numchan": 1}) sound.samplerate = 44100 sound.frames = sine player = Player(sound) player.thread_play().join() from gum.lib import pysndfile import gum f = pysndfile.sndfile(gum.basedir + '/data/test/test1.wav') data = f.read_frames(f.get_nframes()) sound.frames = data player.set_sound(sound) player.thread_play().join() player.thread_play().join() # Testing position player.start = 40000 player.thread_play().join() player.start = 0 # Test reentrancy print ("Two threads will attempt to play at a small interval, you should " "hear the first one being interrupted by the second one.") from time import sleep t1 = player.thread_play() sleep(0.5) t2 = player.thread_play() t1.join() t2.join() print ("Two threads will attempt to play simultaneously, you should " "hear only one.") from time import sleep t1 = player.thread_play() t2 = player.thread_play() t1.join() t2.join() # Testing stop print print "Testing stop(): the sound should stop after 0.3 seconds." player.thread_play() sleep(0.3) player.stop() # Testing stereo f = pysndfile.sndfile(gum.basedir + '/data/test/test2.wav') data = f.read_frames(f.get_nframes()) sound = Mock({"numchan": 2}) sound.samplerate = 44100 sound.frames = data player = Player(sound) player.thread_play().join()