def checkMems(): fm.register(20, fm.fpioa.I2S0_IN_D0) fm.register(19, fm.fpioa.I2S0_WS) fm.register(18, fm.fpioa.I2S0_SCLK) mic = I2S(I2S.DEVICE_0) mic.channel_config(mic.CHANNEL_0, mic.RECEIVER, align_mode=I2S.STANDARD_MODE) mic.set_sample_rate(38640) img = image.Image() while button.value(): audio = mic.record(1024) fft_res = FFT.run(audio.to_bytes(), 512) fft_amp = FFT.amplitude(fft_res) img = img.clear() x_shift = 0 for i in range(50): if fft_amp[i] > 240: hist_height = 240 else: hist_height = fft_amp[i] hist_width = int(240 / 50) img = img.draw_rectangle( (x_shift, 240 - hist_height, hist_width, hist_height), [255, 255, 255], 2, True) img.draw_string(0, 0, "Press btn break", lcd.GREEN, scale=2) x_shift = x_shift + hist_width lcd.display(img) fft_amp.clear()
def work(self): self.agent.parallel_cycle() ui.canvas.draw_string(10, 30, "9 Audio Test", (127, 127, 255), scale=3) ui.canvas.draw_string(10, 70, "isconnected: %s" % (str)(self.isconnected), (255, 127, 0), scale=2) ui.canvas.draw_string(10, 100, "Test: %s" % ('play' if self.state == 0 else 'record'), (255, 127, 0), scale=3) #print(time.ticks_ms()) if self.isconnected: if self.state == 0 and self.is_play: if CubeAudio.event() == False: CubeAudio.load(os.getcwd() + "/res/sound/loop.wav", 100) #print('self.count', self.count) if self.count > 1: CubeAudio.i2s.set_sample_rate(22050) else: # pass CubeAudio.i2s.set_sample_rate(22050) elif self.state == 1: ui.canvas.draw_string(10, 200, "Press Any-Key \n Start", (255, 127, 0), scale=3) elif self.state == 2 and self.is_record: tmp = CubeAudio.i2s.record(1024) fft_res = FFT.run(tmp.to_bytes(), 512) fft_amp = FFT.amplitude(fft_res) if fft_amp[50] > 100 and fft_amp[100] > 100: AudioTest.RecordTest = True sample_page.next() for x_shift in range(240): hist_height = fft_amp[x_shift] ui.canvas.draw_rectangle((x_shift, 0, 1, hist_height), [255, 255, 255], 1, True) #print((x_shift, 0, 1, hist_height)) if self.isError != None: ui.canvas.draw_string(40, 80, self.isError, (255, 255, 255), scale=2) sample_page.next()
def fft_output(leds): while True: audio = rx.record(sample_points) FFT_res = FFT.run(audio.to_bytes(), FFT_points) FFT_amp = FFT.amplitude(FFT_res) interval = int(hist_num / n_leds) count = 0 for i in range(0, hist_num, interval): if FFT_amp[i] > 240: hist_height = 240 brightness = 255 / 5 else: hist_height = FFT_amp[i] brightness = FFT_amp[i] leds.set_led(count, (35, brightness, 10)) count = count + 1 leds.display()
def getVoiceFreq(): # get avarange frequency of sound # return a num in [0,256),usuall in [0,27] global freq_lis audio = mic.record(sample_points) fft_res = FFT.run(audio.to_bytes(), fft_points) fft_amp = FFT.amplitude(fft_res) ampl = max(fft_amp) freq = fft_amp.index(ampl) fft_amp.clear() if freq < 200 and ampl > 10: if len(freq_lis) < 5: freq_lis.append(freq) else: freq_lis.pop(0) freq_lis.append(freq) return 0 if not freq_lis else sum(freq_lis) / len(freq_lis)
sample_rate = 38640 rx.set_sample_rate(sample_rate) img = image.Image() sample_points = 128 FFT_points = 64 lcd_width = 320 lcd_height = 240 hist_num = FFT_points #changeable if hist_num > 320: hist_num = 320 hist_width = int(320 / hist_num) #changeable x_shift = 0 while True: audio = rx.record(sample_points) print(audio) FFT_res = FFT.run(audio.to_bytes(), FFT_points) FFT_amp = FFT.amplitude(FFT_res) #print(FFT_amp) img = img.clear() x_shift = 0 for i in range(hist_num): if FFT_amp[i] > 240: hist_height = 240 else: hist_height = FFT_amp[i] img = img.draw_rectangle( (x_shift, 240 - hist_height, hist_width, hist_height), [255, 255, 255], 2, True) x_shift = x_shift + hist_width lcd.display(img)
color = [(gammatable[148], gammatable[0], gammatable[211]), (gammatable[75], gammatable[0], gammatable[130]), (gammatable[0], gammatable[0], gammatable[255]), (gammatable[0], gammatable[255], gammatable[0]), (gammatable[150], gammatable[150], gammatable[0]), (gammatable[255], gammatable[127], gammatable[0]), (gammatable[255], gammatable[0], gammatable[0]), (gammatable[0], gammatable[127], gammatable[127])] old = time.ticks_ms() colorSelection = 0 while True: audio = rx.record(samplePoints) fftRes = FFT.run(audio.to_bytes(), fftPoints) fftAmp = FFT.amplitude(fftRes) new = time.ticks_ms() if new - old >= 500: colorSelection = random.randrange(8) old = new for i in range(histNum): if fftAmp[i] > maxHistHeight: histHeight = maxHistHeight else: histHeight = fftAmp[i] ledHeight = int(mapValue(histHeight, 0, maxHistHeight, 0, maxLedHeight)) for j in range(maxLedHeight * i, (maxLedHeight * i) + ledHeight): ws.set_led(j, color[colorSelection])
elif lcd_rotation == 3: widht,height = lcd.width(),lcd.height() x = widht - x y = height - y img = image.Image(size=(widht, height)) if HW_CAMERA: img = sensor.snapshot() new = img.copy(roi=(0, 0, 239, 239)) img = new audio = mic.record(1024) fft_res = FFT.run(audio.to_bytes(), 512) fft_amp = FFT.amplitude(fft_res) x_shift = 0 for i in range(50): if fft_amp[i] > 240: hist_height = 240 else: hist_height = fft_amp[i] hist_width = int(240 / 50) img.draw_rectangle((x_shift, 240-hist_height, hist_width, hist_height), [255, 255, 255], 2, True) x_shift = x_shift + hist_width fft_amp.clear()
fm.register(20, fm.fpioa.I2S0_IN_D0) fm.register(30, fm.fpioa.I2S0_WS) # 19 on Go Board and Bit(new version) fm.register(32, fm.fpioa.I2S0_SCLK) # 18 on Go Board and Bit(new version) rx = I2S(I2S.DEVICE_0) rx.channel_config(rx.CHANNEL_0, rx.RECEIVER, align_mode=I2S.STANDARD_MODE) rx.set_sample_rate(sample_rate) img = image.Image() if hist_x_num > 320: hist_x_num = 320 hist_width = int(320 / hist_x_num) #changeable x_shift = 0 while True: audio = rx.record(sample_points) fft_res = FFT.run(audio.to_bytes(), fft_points) fft_amp = FFT.amplitude(fft_res) img = img.clear() x_shift = 0 for i in range(hist_x_num): if fft_amp[i] > 240: hist_height = 240 else: hist_height = fft_amp[i] img = img.draw_rectangle( (x_shift, 240 - hist_height, hist_width, hist_height), [255, 255, 255], 2, True) x_shift = x_shift + hist_width lcd.display(img) fft_amp.clear()
fm.register(20, fm.fpioa.I2S0_IN_D0) fm.register(30, fm.fpioa.I2S0_WS) # 19 on Go Board fm.register(32, fm.fpioa.I2S0_SCLK) # 18 on Go Board rx = I2S(I2S.DEVICE_0) rx.channel_config(rx.CHANNEL_0, rx.RECEIVER, align_mode=I2S.STANDARD_MODE) rx.set_sample_rate(sample_rate) img = image.Image() if hist_x_num > 320: hist_x_num = 320 hist_width = int(320 / hist_x_num) #changeable x_shift = 0 while True: audio = rx.record(sample_points) fft_res = FFT.run(audio.tobyte(), fft_points) fft_amp = FFT.amplitude(fft_res) img = img.clear() x_shift = 0 for i in range(hist_x_num): if fft_amp[i] > 240: hist_height = 240 else: hist_height = fft_amp[i] img = img.draw_rectangle( (x_shift, 240 - hist_height, hist_width, hist_height), [255, 255, 255], 2, True) x_shift = x_shift + hist_width lcd.display(img) fft_amp.clear()