diff --git a/HearingTest.py b/HearingTest.py index 4ca0211ca10780be90eb77ca16ac8465334451a6..67916ec3754dd58ad2b8ca6ac48afcd3970ca824 100644 --- a/HearingTest.py +++ b/HearingTest.py @@ -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()