def handle_read(self): data = self.recv(packet_size) modulated = data_to_wavhndl(data) sd.play(modulated[0], modulated[1]) sd.wait() #wait for data to play print 'stat:', sd.get_status() if data: print ":Transmitting (" + str(len(modulated[0])) + ") to dest" print "Array:", modulated print "data sent:", data
'''demo for using sound device and sound file: Taken from: https://python-sounddevice.readthedocs.io/en/0.2.1/examples.html ''' import argparse import logging log = logging.getLogger(__name__) # To use, cd into helpers directory, run >> python demo/sound_card_demo.py "filename" # Example: python demo/sound_card_demo.py "../static/sounds/chime.wav" parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("filename", help="audio file to be played back") parser.add_argument("-d", "--device", type=int, help="device ID") args = parser.parse_args() try: import sounddevice as sd import soundfile as sf devices = sd.query_devices() print(devices) data, fs = sf.read(args.filename, dtype='float32') sd.play(data, fs, device=args.device, blocking=True) status = sd.get_status() if status: log.warning(str(status)) except BaseException as e: # This avoids printing the traceback, especially if Ctrl-C is used. raise SystemExit(str(e))
import sounddevice as sd import numpy as np import matplotlib.pyplot as plt sd.default.device = [7, 4] # input (>), output (<) print(sd.query_devices()) volume = 1 fs = 44100 duration = 0.2 f = 440.0 samples = (np.sin(2 * np.pi * np.arange(fs * duration) * f / fs)).astype( np.float32) sd.play(samples, fs) print(sd.get_status()) sd.wait() # print("Starting to record...") # myrecording = sd.rec(int(duration * fs), samplerate=fs, channels=2) # sd.wait() # # print("Playing back...") # sd.play(myrecording, fs) # sd.wait() # plt.plot(myrecording) # plt.show()
#!/usr/bin/env python3 """Load an audio file and play its contents. PySoundFile (https://github.com/bastibe/PySoundFile/) has to be installed! """ import argparse import logging parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("filename", help="audio file to be played back") parser.add_argument("-d", "--device", type=int, help="device ID") args = parser.parse_args() try: import sounddevice as sd import soundfile as sf data, fs = sf.read(args.filename, dtype='float32') sd.play(data, fs, device=args.device, blocking=True) status = sd.get_status() if status: logging.warning(str(status)) except BaseException as e: # This avoids printing the traceback, especially if Ctrl-C is used. raise SystemExit(str(e))
import math import time import random duration = 10 centerF = 500 sd.default.samplerate = 44100 def getSound(freq, dur): sampRate = 44100 samps = int(round(dur * sampRate)) inputArray = np.empty(samps) for i in range(0, samps): inputArray[i] = 2 * (math.floor(2 * i * freq / sampRate) % 2) - 1 #inputArray[i] = math.sin(2*3.14*2*i*freq/sampRate) return inputArray #sd.play(getSound(1000, .1), loop=True) while True: print "start" sd.stop() print "stopped dev" delta = random.randint(20, 100) sd.play(getSound(centerF + delta, .05), loop=True) sd.sleep(50) print sd.get_status()