def pre_exp(exp): exp.interface = theForm.Interface(alternatives = exp.validKeys.split(",")) exp.interface.update_Title_Center(exp.note) exp.interface.update_Title_Right("Subject {:}".format(exp.subjID), redraw=True) exp.audiodev = m.open_device() exp.interface.show_Buttons(False) exp.interface.show_Notify_Left(False) exp.interface.update_Prompt("Press any key to begin", show=True, redraw=True) # Wait for a keypress ret = exp.interface.get_resp() exp.interface.update_Prompt("Which Interval?", show=False, redraw=True)
def main(): fs = 44100.0 x, fs = medussa.read_file("test/clean.wav") # x,fs = medussa.read_file("test/speech-noise-tone.wav") # y = medussa.write_wav("test/clean2.wav", x, fs, bits='u8') # y = medussa.write_ogg("test/clean2.ogg", x, fs) # y = medussa.write_flac("test/clean2.flac", x, fs, bits=16) # flac16 = formats.SF_FORMAT_FLAC[0] | formats.SF_FORMAT_PCM_16[0] # oggv = formats.SF_FORMAT_OGG[0] | formats.SF_FORMAT_VORBIS[0] # y = medussa.writefile("test/clean2.ogg", x, fs, format=oggv) # print y d = medussa.open_device() dd = medussa.open_device() sleep(1) sa = d.open_array(x, fs) sf = d.open_file("test/clean.wav") st = d.create_tone(400, fs) sw = d.create_white(fs) sp = d.create_pink(fs) sp2 = dd.create_pink(fs) # medussa.write_wav('test/clean2.wav', x, fs) def sweep_right(s, delta=0.001, steps=500): s.play() fade = np.linspace(0.0, 1.0, steps) for i in xrange(steps): s.mix_mat = np.array([1.0 - fade[i], fade[i]]) sleep(delta) s.pause() def sweep_left(s, delta=0.001, steps=500): s.play() fade = np.linspace(0.0, 1.0, steps) for i in xrange(steps): s.mix_mat = np.array([fade[i], 1.0 - fade[i]]) sleep(delta) s.pause()
def pre_exp(exp): exp.audiodev = m.open_device() exp.interface = theForm.Interface() exp.interface.update_Title_Center(exp.note) exp.interface.update_Title_Right("Subject {:}".format(exp.subjID)) exp.interface.update_Status_Left("Press '/' to quit") exp.interface.show_Notify_Left(False) exp.interface.show_Notify_Right(False) exp.stim.n = exp.interface.posbar_w * 2. exp.user.results = "Subject: {:}\n".format(exp.subjID) # Wait for a keypress ret = exp.interface.get_resp()
def play(self): # Get values i = self.options[self.keys.index(ord('i'))]["val"] s = self.options[self.keys.index(ord('s'))]["val"] n = self.options[self.keys.index(ord('n'))]["val"] o = self.options[self.keys.index(ord('o'))]["val"] c = self.options[self.keys.index(ord('c'))]["val"] w = self.options[self.keys.index(ord('w'))]["val"] r = self.options[self.keys.index(ord('r'))]["val"] a = self.options[self.keys.index(ord('a'))]["val"] d = self.options[self.keys.index(ord('d'))]["val"] # Create noise signal = np.random.randn(np.int32(d * s)) # RMS signal = signal * (r / np.sqrt(np.mean(np.square(signal)))) # Atten signal = signal * np.exp(np.float32(-a) / 8.6860) # Filter hp = np.round(c * (2.**np.float32(-w / 2.))) lp = np.round(c * (2.**np.float32(w / 2.))) bh, ah = scipy.signal.butter(6, hp / (s / 2.), btype='high') signal = scipy.signal.lfilter(bh, ah, signal) bl, al = scipy.signal.butter(6, lp / (s / 2.)) signal = scipy.signal.lfilter(bl, al, signal) # 20-ms ramps rdur = np.int(np.round(.02 * s)) rf = np.hanning(np.int32(2. * rdur)) signal[0:rdur] = signal[0:rdur] * rf[0:rdur] signal[-(rdur - 1):] = signal[-(rdur - 1):] * rf[-(rdur - 1):, ] # Access hardware dev = m.open_device(i, i, n) stream = dev.open_array(signal, s) mm = stream.mix_mat mm[:] = 0 mm[o - 1] = 1 stream.mix_mat = mm self.disabled = True inst = self._instructions.text self._instructions.text = "Playing noise; {:}-{:} Hz".format(hp, lp) self._screen.force_update() self._screen.force_update() stream.play() while stream.is_playing: time.sleep(.1) self.disabled = False self._instructions.text = inst self._screen.force_update()
def play(self): # Get values i = self.options[self.keys.index(ord('i'))]["val"] s = self.options[self.keys.index(ord('s'))]["val"] n = self.options[self.keys.index(ord('n'))]["val"] o = self.options[self.keys.index(ord('o'))]["val"] f = self.options[self.keys.index(ord('f'))]["val"] a = self.options[self.keys.index(ord('a'))]["val"] d = self.options[self.keys.index(ord('d'))]["val"] # Create tone dur = np.int32((np.float32(d) * s)) freq = np.ones(dur) * f signal = np.sin(2. * np.pi * np.cumsum(freq) / s) # 20-ms ramps rdur = np.int(np.round(.02 * s)) rf = np.hanning(np.int32(2. * rdur)) signal[0:rdur] = signal[0:rdur] * rf[0:rdur] signal[-(rdur - 1):] = signal[-(rdur - 1):] * rf[-(rdur - 1):, ] dev = m.open_device(i, i, n) stream = dev.open_array(signal, s) mm = stream.mix_mat mm[:] = 0 mm[o - 1] = 1 stream.mix_mat = mm self.disabled = True inst = self._instructions.text self._instructions.text = "Playing tone..." self._screen.force_update() stream.play() while stream.is_playing: time.sleep(.1) self.disabled = False self._instructions.text = inst self._screen.force_update()
def process_tone(menus, dev, tone): need_params = True status = "Tone menu selected" while need_params: this_menu = menus.tone.format(tone.f, tone.amp, tone.dur) pad = menu_max_size - this_menu.count("\n") print("\n" * 12 + this_menu + "\n" * pad) print("Status: {}\n".format(status)) ret = raw_input("Choose an option: ") if ret == "a": this_menu = menus.tone_amp pad = menu_max_size - this_menu.count("\n") + 2 print("\n" * 12 + this_menu + "\n" * pad) new_amp = raw_input("Enter value: ") if is_number(new_amp): new_amp_f = np.float32(new_amp) if new_amp_f <= 1: tone.amp = new_amp_f status = "Amplitude changed: {:}".format(new_amp_f) elif new_amp == "d": tone.amp = default_tone.amp status = "Amplitude set to default: {:}".format( default_tone.amp) elif new_amp == "": status = "Amplitude unchanged: {:}".format(tone.amp) else: status = "Amplitude unchanged: {:}; Unrecognized input: {}".format( tone.amp, new_amp) elif ret == "d": this_menu = menus.tone_dur pad = menu_max_size - this_menu.count("\n") + 2 print("\n" * 12 + this_menu + "\n" * pad) new_dur = raw_input("Enter value: ") if is_number(new_dur): new_dur_f = np.float32(new_dur) if 0 <= new_dur_f: tone.dur = new_dur_f status = "Duration changed: {:}".format(new_dur_f) elif new_dur == "d": tone.dur = default_tone.dur status = "Duration set to default: {:}".format( default_tone.dur) elif new_dur == "": status = "Duration unchanged: {:}".format(tone.dur) else: status = "Duration unchanged: {:}; Unrecognized input: {}".format( tone.dur, new_dur) elif ret == "f": this_menu = menus.tone_f pad = menu_max_size - this_menu.count("\n") + 2 print("\n" * 12 + this_menu + "\n" * pad) new_f = raw_input("Enter value: ") if is_number(new_f): new_f_f = np.float32(new_f) if 0 < new_f_f: tone.f = new_f_f status = "Frequency changed: {:}".format(new_f_f) elif new_f == "d": tone.f = default_tone.f status = "Frequency set to default: {:}".format(default_tone.f) elif new_f == "": status = "Frequency unchanged: {:}".format(tone.f) else: status = "Frequency unchanged: {:}; Unrecognized input: {}".format( tone.f, new_f) elif ret == "p": print("Playing pure tone with f={:}; amp={:}; dur={:}".format( tone.f, tone.amp, tone.dur)) signal = psylab.signal.tone(tone.f, tone.fs, tone.dur * 1000., amp=tone.amp) signal = psylab.signal.ramps(signal, tone.fs) print("Device id: {}".format(dev.id)) d = m.open_device(int(dev.id), int(dev.id), int(dev.n)) s = d.open_array(signal, int(dev.fs)) mm = s.mix_mat mm[:] = 0 mm[int(dev.ch) - 1] = 1 s.mix_mat = mm s.play() while s.is_playing: time.sleep(.1) status = "Played tone" elif ret == "b": status = "" need_params = False return tone, status
def process_noise(menus, dev, noise): need_params = True status = "Noise menu selected" while need_params: this_menu = menus.noise.format(noise.cf, noise.bw, noise.rms, noise.amp, noise.dur) pad = menu_max_size - this_menu.count("\n") print("\n" * 12 + this_menu + "\n" * pad) print("Status: {}\n".format(status)) ret = raw_input("Choose an option: ") if ret == "c": this_menu = menus.noise_cf pad = menu_max_size - this_menu.count( "\n") + 2 # +2 when there is no status line print("\n" * 12 + this_menu + "\n" * pad) new_cf = raw_input("Enter value: ") if is_number(new_cf): new_cf_f = np.float32(new_cf) if new_cf_f > 0: noise.cf = new_cf_f status = "Center frequency changed: {:}".format(new_cf_f) elif new_cf == "d": noise.cf = default_noise.cf status = "Center frequency set to default: {:}".format( default_noise.cf) elif new_cf == "": status = "Center frequency unchanged: {:}".format(noise.cf) else: status = "Center frequency unchanged: {:}; Unrecognized input: {}".format( noise.cf, new_cf) if ret == "w": this_menu = menus.noise_bw pad = menu_max_size - this_menu.count("\n") + 2 print("\n" * 12 + this_menu + "\n" * pad) new_bw = raw_input("Enter value: ") if is_number(new_bw): new_bw_f = np.float32(new_bw) if new_bw_f > 0: noise.bw = new_bw_f status = "Bandwidth changed: {:}".format(new_bw_f) elif new_bw == "d": noise.bw = default_noise.bw status = "Bandwidth set to default: {:}".format( default_noise.bw) elif new_bw == "": status = "Bandwidth unchanged: {:}".format(noise.bw) else: status = "Bandwidth unchanged: {:}; Unrecognized input: {}".format( noise.bw, new_bw) elif ret == "a": this_menu = menus.noise_amp pad = menu_max_size - this_menu.count("\n") + 2 print("\n" * 12 + this_menu + "\n" * pad) new_amp = raw_input("Enter value: ") if is_number(new_amp): new_amp_f = np.float32(new_amp) if new_amp_f <= 1: noise.amp = new_amp_f status = "Amplitude changed: {:}".format(new_amp_f) elif new_amp == "d": noise.amp = default_noise.amp status = "Amplitude set to default: {:}".format( default_noise.amp) elif new_amp == "": status = "Amplitude unchanged: {:}".format(noise.amp) else: status = "Amplitude unchanged: {:}; Unrecognized input: {}".format( noise.amp, new_amp) elif ret == "r": this_menu = menus.noise_rms pad = menu_max_size - this_menu.count("\n") + 2 print("\n" * 12 + this_menu + "\n" * pad) new_rms = raw_input("Enter value: ") if is_number(new_rms): new_rms_f = np.float32(new_rms) if new_rms_f <= 1: noise.rms = new_rms_f status = "RMS changed: {:}".format(new_rms_f) elif new_rms == "d": noise.rms = default_noise.rms status = "RMS set to default: {:}".format(default_noise.rms) elif new_rms == "": status = "RMS unchanged: {:}".format(noise.rms) else: status = "RMS unchanged: {:}; Unrecognized input: {}".format( noise.rms, new_rms) elif ret == "d": this_menu = menus.noise_dur pad = menu_max_size - this_menu.count("\n") + 2 print("\n" * 12 + this_menu + "\n" * pad) new_dur = raw_input("Enter value: ") if is_number(new_dur): new_dur_f = np.float32(new_dur) if 0 <= new_dur_f: noise.dur = new_dur_f status = "Duration changed: {:}".format(new_dur_f) elif new_dur == "d": noise.dur = default_noise.dur status = "Duration set to default: {:}".format( default_noise.dur) elif new_dur == "": status = "Duration unchanged: {:}".format(noise.dur) else: status = "Duration unchanged: {:}; Unrecognized input: {}".format( noise.dur, new_dur) elif ret == "p": print("Playing noise with cf={:}; bw={:}; amp={:}; dur={:}".format( noise.cf, noise.bw, noise.amp, noise.dur)) signal = np.random.randn(np.int32(noise.dur * noise.fs)) signal = signal * (noise.rms / psylab.signal.rms(signal)) signal = psylab.signal.atten(signal, noise.amp) bw = noise.bw / 2. fc = psylab.signal.oct2f(noise.cf, bw) hp = fc[0] lp = fc[1] bh, ah = psylab.signal.filters.butter(6, hp / (noise.fs / 2.), btype='high') signal = psylab.signal.lfilter(bh, ah, signal) bl, al = psylab.signal.filters.butter(6, lp / (noise.fs / 2.)) signal = psylab.signal.lfilter(bl, al, signal) signal = psylab.signal.ramps(signal, noise.fs) d = m.open_device(int(dev.id), int(dev.id), int(dev.n)) s = d.open_array(signal, int(dev.fs)) mm = s.mix_mat mm[:] = 0 mm[int(dev.ch) - 1] = 1 s.mix_mat = mm s.play() while s.is_playing: time.sleep(.1) status = "Played noise" elif ret == "b": status = "" need_params = False return noise, status
from __future__ import print_function import medussa import numpy as np from time import sleep import sys import random fs = 44100.0 #medussa.init() d = medussa.open_device() TEST_ALL = True # sanity checks: create and immediately delete streams ##################################################################### if TEST_ALL or 0: print("testing: sanity checks: creating and deleting streams") print("create_tone()") s = d.create_tone(440, fs) del s print("create_tone() DONE") print("create_white()") s = d.create_white(fs) del s print("create_white() DONE")