Administrator approval is now required for registering new accounts. If you are registering a new account, and are external to the University, please ask the repository owner to contact ServiceLine to request your account be approved. Repository owners must include the newly registered email address, and specific repository in the request for approval.

Commit 6bfdb8be authored by Edward Rogers's avatar Edward Rogers
Browse files

Implement SoundLibrary

parent 2f5a090f
......@@ -25,23 +25,43 @@ import matplotlib.pyplot as plt
import soundfile as sf
import time
if __name__ == '__main__':
class SoundLibrary():
def __init__(self, fs, sample_length, freqs):
self._data = {}
self.fs = fs
self.sample_length = sample_length
self.generate_sounds(freqs)
def generate_sounds(self, freqs):
t = np.arange(0, self.sample_length, 1 / self.fs)
for f in freqs:
sound = np.sin(2*np.pi*f * t)
self._data[f] = sound
@property
def freqs(self):
return self._data.keys()
def play(self, freq):
sd.play(self[freq], self.fs, blocking=False)
def __getitem__(self, item):
return self._data[item]
def main():
print(sd.default.device['output'])
device = sd.query_devices(sd.default.device['output'])
fs = device['default_samplerate']
length = 0.5
f = 5000
# sound = np.zeros(int(length*fs))
#
# sound = np.sin(f*t)*norm
# sound, fs = sf.read('C:\Windows\media\Windows Background.wav', dtype='float32')
# t = np.arange(0, sound.shape[0]) * (1 / fs)
# plt.plot(t, sound)
# plt.show()
# print(device)
# sd.play(sound, fs, blocking=False)
# time.sleep(1)
t = np.arange(0, length, 1 / fs)
sound2 = np.sin(f*t)
sd.play(sound2, fs, blocking=True)
f = [20, 50, 100, 200, 500, 1000, 2000, 5000, 10000]
library = SoundLibrary(fs, length, f)
for freq in library.freqs:
library.play(freq)
time.sleep(1)
if __name__ == '__main__':
main()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment