def main(): # check that the device can capture audio if not sf.SoundRecorder.is_available(): print("Sorry, audio capture is not supported by your system") return # choose the sample rate sample_rate = int( input( "Please choose the sample rate for sound capture (44100 is CD quality): " )) # wait for user input... input("Press enter to start recording audio") # here we'll use an integrated custom recorder, which saves the captured data into a sf.SoundBuffer recorder = sf.SoundBufferRecorder() # audio capture is done in a separate thread, so we can block the main thread while it is capturing recorder.start(sample_rate) input("Recording... press enter to stop") recorder.stop() # get the buffer containing the captured data buffer = recorder.buffer # display captured sound informations print("Sound information:") print("{0} seconds".format(buffer.duration)) print("{0} samples / seconds".format(buffer.sample_rate)) print("{0} channels".format(buffer.channel_count)) # choose what to do with the recorded sound data choice = input( "What do you want to do with captured sound (p = play, s = save) ? ") if choice == 's': # choose the filename filename = input("Choose the file to create: ") # save the buffer buffer.to_file(filename) else: # create a sound instance and play it sound = sf.Sound(buffer) sound.play() # wait until finished while sound.status == sf.Sound.PLAYING: # leave some CPU time for other threads sf.sleep(sf.milliseconds(100)) # finished ! print("Done !") # wait until the user presses 'enter' key input("Press enter to exit...")
def do_server(port): # build an audio stream to play sound data as it is received through the network audio_stream = NetworkAudioStream() audio_stream.start(port) # loop until the sound playback is finished while audio_stream.status != sf.SoundStream.STOPPED: # leave some CPU time for other threads sf.sleep(sf.milliseconds(100)) # wait until the user presses 'enter' key input("Press enter to replay the sound...") # replay the sound (just to make sure replaying the received data is OK) audio_stream.play(); # loop until the sound playback is finished while audio_stream.status != sf.SoundStream.STOPPED: sf.sleep(sf.milliseconds(100))
def emit(self, system, dt): #TAIL_INTERVAL = EXPLOSION_DURATION / TAILS_PER_EXPLOSION TAIL_INTERVAL = sf.milliseconds(float(EXPLOSION_DURATION.milliseconds) / float(TAILS_PER_EXPLOSION)) # accumulate passed time: if enough time has passed (TAIL_INTERVAL), # emit a new tail and decrease accumulator self._accumulated_time += dt while self._accumulated_time - TAIL_INTERVAL > sf.Time.ZERO: self._emit_tail(system) self._accumulated_time -= TAIL_INTERVAL
def main(): # check that the device can capture audio if not sf.SoundRecorder.is_available(): print("Sorry, audio capture is not supported by your system") return # choose the sample rate sample_rate = int(input("Please choose the sample rate for sound capture (44100 is CD quality): ")) # wait for user input... input("Press enter to start recording audio") # here we'll use an integrated custom recorder, which saves the captured data into a sf.SoundBuffer recorder = sf.SoundBufferRecorder() # audio capture is done in a separate thread, so we can block the main thread while it is capturing recorder.start(sample_rate) input("Recording... press enter to stop") recorder.stop() # get the buffer containing the captured data buffer = recorder.buffer # display captured sound informations print("Sound information:") print("{0} seconds".format(buffer.duration)) print("{0} samples / seconds".format(buffer.sample_rate)) print("{0} channels".format(buffer.channel_count)) # choose what to do with the recorded sound data choice = input("What do you want to do with captured sound (p = play, s = save) ? ") if choice == 's': # choose the filename filename = input("Choose the file to create: ") # save the buffer buffer.to_file(filename); else: # create a sound instance and play it sound = sf.Sound(buffer) sound.play(); # wait until finished while sound.status == sf.Sound.PLAYING: # leave some CPU time for other threads sf.sleep(sf.milliseconds(100)) # finished ! print("Done !") # wait until the user presses 'enter' key input("Press enter to exit...")
def play_music(): # load an ogg music file music = sf.Music.from_file("data/orchestral.ogg") # display music informations print("orchestral.ogg:") print("{0} seconds".format(music.duration)) print("{0} samples / sec".format(music.sample_rate)) print("{0} channels".format(music.channel_count)) # play it music.play() # loop while the music is playing while music.status == sf.Music.PLAYING: # leave some CPU time for other processes sf.sleep(sf.milliseconds(100))
def play_music(): # load an ogg music file music = sf.Music.from_file("data/orchestral.ogg") # display music informations print("orchestral.ogg:") print("{0} seconds".format(music.duration)) print("{0} samples / sec".format(music.sample_rate)) print("{0} channels".format(music.channel_count)) # play it music.play(); # loop while the music is playing while music.status == sf.Music.PLAYING: # leave some CPU time for other processes sf.sleep(sf.milliseconds(100))
def play_sound(): # load a sound buffer from a wav file buffer = sf.SoundBuffer.from_file("data/canary.wav") # display sound informations print("canary.wav:") print("{0} seconds".format(buffer.duration)) print("{0} samples / sec".format(buffer.sample_rate)) print("{0} channels".format(buffer.channel_count)) # create a sound instance and play it sound = sf.Sound(buffer) sound.play() # loop while the sound is playing while sound.status == sf.Sound.PLAYING: # leave some CPU time for other processes sf.sleep(sf.milliseconds(100))
def play_sound(): # load a sound buffer from a wav file buffer = sf.SoundBuffer.from_file("data/canary.wav") # display sound informations print("canary.wav:") print("{0} seconds".format(buffer.duration)) print("{0} samples / sec".format(buffer.sample_rate)) print("{0} channels".format(buffer.channel_count)) # create a sound instance and play it sound = sf.Sound(buffer) sound.play(); # loop while the sound is playing while sound.status == sf.Sound.PLAYING: # leave some CPU time for other processes sf.sleep(sf.milliseconds(100))
def on_get_data(self, chunk): # we have reached the end of the buffer and all audio data have been played : we can stop playback if self.offset >= len(self.samples) and self.has_finished: return False # no new data has arrived since last update : wait until we get some while self.offset >= len(self.samples) and not self.has_finished: sf.sleep(sf.milliseconds(10)) # don't forget to lock as we run in two separate threads lock = threading.Lock() lock.acquire() # fill audio data to pass to the stream chunk.data = self.samples.data[self.offset*2:] # update the playing offset self.offset += len(chunk) lock.release() return True