# Make file executable # chmod +x ./monitor.py # # Check with the following command what name the Built-in Output device has # python -m sounddevice IN = 'BlackHole 16ch' OUT = 'Built-in Output' output = sd.OutputStream(channels=2, blocksize=0, latency="low", device=OUT) output.start() def input_callback(indata, frames, time, status): output.write(np.ascontiguousarray( indata[:, [2, 3]])) # want to hear channels 3 and 4 with sd.InputStream(channels=6, callback=input_callback, blocksize=0, latency="low", device=IN): print('#' * 80) print('press Return to quit') print('#' * 80) if sys.version_info[0] < 3: raw_input() else: input()
for index, freq in enumerate(freqs): location = int(freq * n / fs) amplitude = amplitudes[location, 0] if amplitude > 1: print('freq {} on'.format(freq)) self.phases[0, index] = phases[location, 0] - phases[location, 1] print('{: >#016.4f}{: >#016.4f}{: >#016.4f}{: >#016.4f}'.format( self.phases[0, 0], self.phases[0, 1], self.phases[0, 2], self.phases[0, 3])) pygame.init() screen = pygame.display.set_mode((500, 500)) r = Recorder() stream = sd.InputStream(channels=2, samplerate=fs, callback=r.callback, blocksize=int(duration * fs)) with stream: done = False while not done: for event in pygame.event.get(): if event.type == pygame.QUIT: done = True phase = np.average(r.phases[:, 0]) if phase < -20: phase = -20 if phase > 20: phase = 20 screen.fill((0, 0, 0)) pygame.draw.rect(screen, ((phase + 20) * 255 / 40, 0, 255 - (phase + 20) * 255 / 40),
def callback(indata, frames, time, status): global statuses statuses |= status if any(indata): magnitude = np.abs(np.fft.rfft(indata[:, 0], n=fftsize)) magnitude *= args.gain / fftsize # print( "low: high = ", low_bin, args.columns) print(indata, len(indata)) line = (gradient[int(np.clip(x, 0, 1) * (len(gradient) - 1))] for x in magnitude[low_bin:(low_bin + args.columns)]) # print(*line, sep='', end='\x1b[0m\n', flush=True) else: print('no input', flush=True) with sd.InputStream(device=args.device, channels=1, callback=callback, blocksize=int(samplerate * args.block_duration / 1000), samplerate=samplerate): while True: response = input() if response in ('', 'q', 'Q'): break for ch in response: if ch == '+': args.gain *= 2 elif ch == '-': args.gain /= 2 else: print('\x1b[31;40m', usage_line.center(args.columns, '#'), '\x1b[0m', sep='') break if statuses:
def main(parser, room): try: if args.list_devices: print(sd.query_devices()) parser.exit(0) samplerate = sd.query_devices(args.device, 'input')['default_samplerate'] delta_f = (high - low) / (room.length - 1) fftsize = math.ceil(samplerate / delta_f) # low_bin = math.floor(low / delta_f) # For smoothing, we keep our previous values previous_values = [0] * room.length smoothing = args.smoothing**(1 / 8) def audio_callback(indata, frames, time, status): if any(indata): # Do Fast Fourier Transform on the audio data magnitude = np.abs(np.fft.rfft(indata[:, 0], n=fftsize)) magnitude *= args.gain / fftsize # Take only the frequencies we're interested in grouped = [[] for i in range(room.length)] for i, x in enumerate( magnitude[int(low // delta_f):int(high // delta_f)]): grouped[int(i)].append(x) # This scales the actual freqency magnitudes so that lower # magnitudes get picked up better, especially on higher freqs # where magnitudes are lower despite sounding as powerful as # the lower ones, due to logarithmic perception of sound in # humans. I suck at DSP, so take with a grain of salt. for i, x in enumerate(grouped): if len(x) > 0 and np.max(x) > 0: grouped[i] = ((i + 1) / room.length) * \ args.gain * abs(np.max(x)) ** 2 else: grouped[i] = 0 colors = [] # Thanks to batching our operations, we'll append every # LED's update call into a single packet for i, x in enumerate(grouped): previous = previous_values[i] value = np.clip(x, 1 / 255, 1) # Smooth the value if value < previous: value = previous * smoothing + x * (1 - smoothing) br = value previous_values[i] = value # Calculate pixel color on an 256 segment rainbow pos = (i * 256 // room.length) r, g, b = rainbow(pos & 255) # Apply brightness (magnitude from FFT) r = math.ceil(r * br) g = math.ceil(g * br) b = math.ceil(b * br) colors.append([r, g, b]) room.set_colors(colors) with sd.InputStream(device=args.device, channels=1, callback=audio_callback, blocksize=int(samplerate * args.block_duration / 1000), samplerate=samplerate): print('Successfully started InputStream') while True: response = input() if response in ('', 'q', 'Q'): break except KeyboardInterrupt: parser.exit('Stopped') except Exception as e: parser.exit(type(e).__name__ + ': ' + str(e))
start_index = int(input('Enter start index')) for label in labels: if i < start_index: i += 1 continue issue = True while issue: os.system('cls' if os.name == 'nt' else 'clear') print('Please read the following:\n\n') print('\x1b[1;36;40m' + label + '\x1b[0m') input('Press any key to START the recording.') stream = sd.InputStream(channels=CHANNELS, samplerate=SAMPLE_RATE, callback=stream_callback) try: with stream: print('Get ready...') time.sleep(3) print('Recording started...') input('Press any key to STOP the recording.') print('dene') time.sleep(3) except: print('here') break streamed_data = [] while not q.empty():
def denoiser_live(): global VAD_RESULT global LIVE LIVE = 1 print("live request") pkg = torch.load(MODEL_PATH) if 'model' in pkg: if 'best_state' in pkg: pkg['model']['state'] = pkg['best_state'] model = deserialize_model(pkg['model']) else: model = deserialize_model(pkg) model.eval() frame_num = 1 streamer = DemucsStreamer(model, dry=DRY, num_frames=frame_num) sample_rate = 16_000 caps = query_devices(None, "input") channels_in = min(caps['max_input_channels'], 2) stream_in = sd.InputStream( device=None, samplerate=sample_rate, channels=channels_in) device_out = "Soundflower (2ch)" caps = query_devices(device_out, "output") channels_out = min(caps['max_output_channels'], 2) stream_out = sd.OutputStream( device=None, samplerate=sample_rate, channels=channels_out) stream_in.start() stream_out.start() first = True current_time = 0 last_log_time = 0 last_error_time = 0 cooldown_time = 2 log_delta = 10 sr_ms = sample_rate / 1000 stride_ms = streamer.stride / sr_ms print(f"Ready to process audio, total lag: {streamer.total_length / sr_ms:.1f}ms.") while (LIVE == 1): if current_time > last_log_time + log_delta: last_log_time = current_time tpf = streamer.time_per_frame * 1000 rtf = tpf / stride_ms print(f"time per frame: {tpf:.1f}ms, ", end='') print(f"RTF: {rtf:.1f}") streamer.reset_time_per_frame() last_log_time = current_time length = streamer.total_length if first else streamer.stride first = False current_time += length / sample_rate frame, overflow = stream_in.read(length) if VAD_RESULT == 1: frame = torch.from_numpy(frame).mean(dim=1).to("cpu") with torch.no_grad(): out = streamer.feed(frame[None])[0] if not out.numel(): continue # compresser # out = 0.99 * torch.tanh(out) out = out[:, None].repeat(1, channels_out) mx = out.abs().max().item() if mx > 1: print("Clipping!!") out.clamp_(-1, 1) out = out.cpu().numpy() underflow = stream_out.write(out) if overflow or underflow: if current_time >= last_error_time + cooldown_time: last_error_time = current_time tpf = 1000 * streamer.time_per_frame RESULT = f"time per frame is {tpf:.1f}ms, need to be below {stride_ms:.1f}ms for acceptable quality" socketio.emit('my_response',{'data': RESULT}) print(f"time per frame is {tpf:.1f}ms, need to be below {stride_ms:.1f}ms for acceptable quality") # print (f"Denoiser is running! time per frame is {tpf:.1f}ms, need to be below {stride_ms:.1f}ms") stream_out.stop() stream_in.stop() return ('', 204)
def main(): global plotdata import argparse parser = argparse.ArgumentParser(add_help=False) parser.add_argument('-l', '--list-devices', action='store_true', help='show list of audio devices and exit') args, remaining = parser.parse_known_args() if args.list_devices: print(sd.query_devices()) parser.exit(0) parser = argparse.ArgumentParser( description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter, parents=[parser]) parser.add_argument('channels', type=int, default=[1], nargs='*', metavar='CHANNEL', help='input channels to plot (default: the first)') parser.add_argument('-d', '--device', type=int_or_str, help='input device (numeric ID or substring)') parser.add_argument('-w', '--window', type=float, default=200, metavar='DURATION', help='visible time slot (default: %(default)s ms)') parser.add_argument( '-i', '--interval', type=float, default=30, help='minimum time between plot updates (default: %(default)s ms)') parser.add_argument('-b', '--blocksize', type=int, help='block size (in samples)') parser.add_argument('-r', '--samplerate', type=float, help='sampling rate of audio device') parser.add_argument('-n', '--downsample', type=int, default=10, metavar='N', help='display every Nth sample (default: %(default)s)') args = parser.parse_args(remaining) if any(c < 1 for c in args.channels): parser.error('argument CHANNEL: must be >= 1') mapping = [c - 1 for c in args.channels] # Channel numbers start with 1 q = queue.Queue() def audio_callback(indata, frames, time, status): """This is called (from a separate thread) for each audio block.""" if status: print(status, file=sys.stderr) # Fancy indexing with mapping creates a (necessary!) copy: q.put(indata[::args.downsample, mapping]) def update_plot(frame): """This is called by matplotlib for each plot update. Typically, audio callbacks happen more frequently than plot updates, therefore the queue tends to contain multiple blocks of audio data. """ global plotdata while True: try: data = q.get_nowait() except queue.Empty: break shift = len(data) plotdata = np.roll(plotdata, -shift, axis=0) plotdata[-shift:, :] = data for column, line in enumerate(lines): line.set_ydata(plotdata[:, column]) return lines try: if args.samplerate is None: device_info = sd.query_devices(args.device, 'input') args.samplerate = device_info['default_samplerate'] length = int(args.window * args.samplerate / (1000 * args.downsample)) plotdata = np.zeros((length, len(args.channels))) fig, ax = plt.subplots() lines = ax.plot(plotdata) if len(args.channels) > 1: ax.legend(['channel {}'.format(c) for c in args.channels], loc='lower left', ncol=len(args.channels)) ax.axis((0, len(plotdata), -1, 1)) ax.set_yticks([0]) ax.yaxis.grid(True) ax.tick_params(bottom=False, top=False, labelbottom=False, right=False, left=False, labelleft=False) fig.tight_layout(pad=0) stream = sd.InputStream(device=args.device, channels=max(args.channels), samplerate=args.samplerate, callback=audio_callback) ani = FuncAnimation(fig, update_plot, interval=args.interval, blit=True) with stream: plt.show() except Exception as e: parser.exit(type(e).__name__ + ': ' + str(e))
print(minval) if maxval > 2**31: mic_boost = mic_boost / 1.5 a[:] = a[:] * 1.5 elif maxval < 2**29: mic_boost = mic_boost * 1.2 a[:] = a[:] / 1.2 q.put(np.int32(a)) try: # Make sure the file is opened before recording anything: with sd.InputStream(samplerate=40000, device='bluealsa', channels=1, callback=callback): print('#' * 80) print('press Ctrl+C to stop the TX') print('#' * 80) while True: if (driver.get_tx_fifo_vacancy() > 1030): driver.write_data(q.get()) print('TX fifo vacancy: ', driver.get_tx_fifo_vacancy()) #print('Max values: ',driver.get_max_amplitude()) #print('Average values: ',driver.get_average_amplitude()) except KeyboardInterrupt: parser.exit(0) except Exception as e:
ceplifter=0, appendEnergy=False, winfunc=np.hanning) mfccs = mfccs.transpose() # Make prediction from model in_tensor = np.float32(mfccs.reshape(1, mfccs.shape[0], mfccs.shape[1], 1)) interpreter.set_tensor(input_details[0]['index'], in_tensor) interpreter.invoke() output_data = interpreter.get_tensor(output_details[0]['index']) val = output_data[0][0] if val > word_threshold: print("Emergency shut down detected!") GPIO.output(led_pin, GPIO.LOW) word_flag = 1 if debug_acc: print(val) if debug_time: print(timeit.default_timer() - start) # Start streaming from microphone with sd.InputStream(channels=num_channels, samplerate=sample_rate, blocksize=int(sample_rate * rec_duration), callback=sd_callback): while word_flag == 0: pass print("Done!")
PREV_SIGNAL = indata spec = audio_to_melspectrogram(signal) spec = spec[:, -SPEC_BLOCK_SIZE:] SPEC_LST = SPEC_LST[1:] + [spec] def update_spec_plot(frame): spec = np.concatenate(SPEC_LST, axis=1) pred = PREDICTOR.predict(spec)[0] top3 = np.argsort(pred)[::-1][:3] top3 = [ f"{config.classes[idx].rjust(20)} - {pred[idx]:.2f}" for idx in top3 ] print("\t".join(top3)) im = plt.imshow(spec) return [im] if __name__ == "__main__": fig = plt.figure(figsize=(14, 7)) plt.title("Mel-spectrogram") stream = sounddevice.InputStream(callback=audio_callback, channels=1, blocksize=signal_block_size, samplerate=config.audio.sampling_rate) animation = FuncAnimation(fig, update_spec_plot, interval=1000, blit=True) with stream: plt.show()
# Intialize (I)ntegrated Loudness and gates to -23.0 dBFS => 0 LU M = -23.0 I = -23.0 Iprev = -23.0 # previous in order to save writing to disk G1mean = -23.0 G1 = 0 # gate counters to calculate the accu mean G2 = 0 ################################################################## # Main loop: open a capture stream that process 100ms audio blocks ################################################################## print('(loudness_monitor) Start monitoring') with sd.InputStream(device=args.input_device, callback=callback, blocksize=BS, samplerate=fs, channels=2, dither_off=True): while True: # Reading captured 100 ms (b)locks: b100 = qIn.get() # “K” weight (f)iltering the 100ms chunks f100 = lfilter(b100, hpf_coeffs) # 100Hz HPF f100 = lfilter(f100, hshelf_coeffs) # 1000Hz High Shelf +4dB # Sliding the 400ms (w)indow w400[:BS * 3] = w400[BS:] w400[BS * 3:] = f100
line.set_ydata(plotdata) return line, try: length = int(WINDOW * SAMPLERATE / (1000 * DOWNSAMPLE)) plotdata = np.zeros(length) fig, ax = plt.subplots() line, = ax.plot(plotdata) ax.axis((0, len(plotdata), -1, 1)) ax.set_yticks([0]) ax.yaxis.grid(True) ax.tick_params(bottom=False, top=False, labelbottom=False, right=False, left=False, labelleft=False) fig.tight_layout(pad=0) stream = sd.InputStream(device=DEVICE, channels=CHANNELS, samplerate=SAMPLERATE, callback=audio_callback) ani = FuncAnimation(fig, update_plot, interval=INTERVAL, blit=True) with stream: plt.show() except Exception as e: parser.exit(type(e).__name__ + ': ' + str(e))
import sounddevice as sd import sys import scipy maxi = 0 max_amp = 1 current_max = 0 signal = [] def audio_callback(indata, frames, time, status): global current_max global signal signal = [max([abs(d) for d in data]) for data in indata] foft = abs(scipy.fft(signal)) #maxi = min([max_amp, max([max([abs(d) for d in data]) for data in indata])]) max_foft = max(foft) sys.stdout.write("\r" + "".join(["|" if val > max_foft / 10 else " " for val in foft[0:100]])) #current_max = max([current_max * 0.9, maxi]) #nb_of_bars = int(round(current_max * 99 / max_amp)) + 1 #sys.stdout.write("\r" + ("|" * nb_of_bars) + (" " * (100 - nb_of_bars))) sys.stdout.flush() try: stream = sd.InputStream(device=0, channels=2, samplerate=44100.0, callback=audio_callback) with stream: while True: a=0#print("ok") except KeyboardInterrupt: print('Interrupted by user')
dev_info = sd.query_devices(2, 'input') #dev_info = default.device() # samplerate = int(dev_info['default_samplerate']) samplerate = 48000 def data_callback(input_data, frames, time, status): if status: print(status, file=sys.stderr) q.put(input_data.copy()) with sf.SoundFile(rere, mode='x', samplerate=samplerate, channels=2) as file: with sd.InputStream(samplerate=samplerate, device=2, channels=2, callback=data_callback, blocksize=20500): rec_time = int(time.time()) - rec_start _thread.start_new_thread(countdown, (0, t0, 1)) while rec_time <= RECORD_TIME: file.write(q.get()) rec_time = int(time.time()) - rec_start result_array = np.empty((0, 100)) path = pa0 files = glob.glob(path) result_array = np.empty((0, 27)) try:
def start(): # Import classifier model logger.info('Initializing a convolutional neural network model...') global model THEANO_FLAGS = ('device=cpu,' 'floatX=float32,' 'dnn.conv.algo_bwd_filter=deterministic,' 'dnn.conv.algo_bwd_data=deterministic') os.environ['THEANO_FLAGS'] = THEANO_FLAGS os.environ['KERAS_BACKEND'] = 'theano' import keras keras.backend.set_image_dim_ordering('th') with open('project/model.json', 'r') as file: cfg = file.read() model = keras.models.model_from_json(cfg) model.load_weights('project/model.h5') logger.debug('Loaded Keras model with weights.') # Start audio capture sd.default.device = AUDIO_DEVICE logger.info('Priming recording device {}.'.format(AUDIO_DEVICE)) stream = sd.InputStream(channels=1, dtype='float32', callback=capture_audio, samplerate=SAMPLING_RATE, blocksize=BLOCK_SIZE) stream.start() blocks = [] processing_queue = collections.deque() # Process incoming audio blocks while True: while len(audio_queue) > 0 and len(blocks) < PREDICTION_STEP: blocks.append(audio_queue.popleft()) if len(blocks) == PREDICTION_STEP: new_audio = np.concatenate(blocks) # Populate audio for live streaming live_audio_feed.append(new_audio[:, 0].copy()) blocks = [] processing_queue.append(new_audio) if len(processing_queue ) > PROCESSING_DELAY + 1: # +1 for JavaScript streaming delay start_time = time.time() # Populate audio signal step_audio = processing_queue.pop() n_samples = len(step_audio) signal[:-n_samples] = signal[n_samples:] signal[-n_samples:] = step_audio[:] # Populate spectrogram new_spec = librosa.feature.melspectrogram(np.concatenate( [last_chunk, step_audio])[:, 0], SAMPLING_RATE, n_fft=FFT_SIZE, hop_length=CHUNK_SIZE, n_mels=MEL_BANDS) with warnings.catch_warnings(): warnings.simplefilter('ignore') # Ignore log10 zero division new_spec = librosa.core.perceptual_weighting(new_spec, MEL_FREQS, amin=1e-5, ref_power=1e-5, top_db=None) new_spec = np.clip(new_spec, 0, 100) n_chunks = np.shape(new_spec)[1] spectrogram[:, :-n_chunks] = spectrogram[:, n_chunks:] spectrogram[:, -n_chunks:] = new_spec # Classify incoming audio predictions[:, :-1] = predictions[:, 1:] offset = SEGMENT_LENGTH // 2 pred = classify([ np.stack([spectrogram[:, -(SEGMENT_LENGTH + offset):-offset]]), np.stack([spectrogram[:, -SEGMENT_LENGTH:]]), ]) predictions[:, -1] = pred target = labels[np.argmax(pred)] # Clean up last_chunk[:] = step_audio[-CHUNK_SIZE:] end_time = time.time() time_spent = int((end_time - start_time) * 1000) temp, freq = get_raspberry_stats() blocks_in_ms = int(PREDICTION_STEP * BLOCK_SIZE / SAMPLING_RATE * 1000) msg = '[{}] {}% = {} ms / {} ms ({} blocks) - temp: {} | freq: {} ==> {}' timestamp = time.strftime('%H:%M:%S') if target == "dog" or target == "glass_breaking" or target == "clock_alarm" or target == "engine" and target != lastTarget: logger.debug( msg.format(timestamp, np.round(time_spent / blocks_in_ms * 100, 1), time_spent, blocks_in_ms, PREDICTION_STEP, temp, freq, target)) result = firebase.put('/user', 'sound', target) lastTarget = target time.sleep(0.05)
opening = True else: # mouth size based on volume mouth_size = int(volume / 50 * max_mouth_size * mouth_size_multiplier) if mouth_size > max_mouth_size: mouth_size = max_mouth_size #print(volume) # just verify that mouth_size is within the limits given, prevents weird errors. I'm lazy. mouth_size = 0 if mouth_size < 0 else mouth_size mouth_size = max_mouth_size if mouth_size > max_mouth_size else mouth_size image = cv2.imread('me.PNG') image = cv2.ellipse(image, (mouth_x, mouth_y), (mouth_width, mouth_size), 0, 0, 360, (0, 0, 255), 5) cv2.imshow('image', image) cv2.waitKey(wait_time) try: with sd.InputStream(callback=audio_callback): # This is just me screwing around, but now im to lazy to change it so, here is an example of ... reallllllly bad # code lol. Like what does it even do? who knows? true = True while true: sd.sleep(1) sleep(1) cv2.destroyAllWindows() except Exception: cv2.destroyAllWindows()
def listen(self, until: Until): """ Record the system's audio until a condition is met and transcribe the voice. :param until: A function that takes no arguments and returns a boolean. :return: (str) A transcription of the audio. """ debug('Awaiting recording.') start_time = time() # Default timeout timer while until() == RecordStatus.AWAIT: now = time() # Wait until we are clear to begin recording. # It the wait it too long, throw an error. if (now - start_time) > self._recording.default_timeout: debug('Recording timeout while AWAIT') raise ValueError(f'Recording timeout while AWAIT') debug('Begin recording.') # Create a unique temp directory and save the file there. tmp_dir_name = str(uuid.uuid1()) full_path = f'{tmp_dir_name}\\{self._recording.buffer_name}.wav' if not os.path.exists(tmp_dir_name): os.mkdir(tmp_dir_name) # Open an intermediate file for recording storage. with sf.SoundFile(full_path, mode='x', samplerate=self._recording.rate, channels=self._recording.channels, subtype="PCM_16") as file: # Create an input stream on the default device. with sd.InputStream(samplerate=self._recording.rate, channels=self._recording.channels, callback=self._audio_callback): # Continue recording while the Until condition holds. while until() == RecordStatus.RECORD: chunk = self._audio_data.get() file.write(chunk) now = time() if (now - start_time) > self._recording.default_timeout: debug('Recording timeout.') break else: raise SystemError('Failed to generate a unique work directory.') debug('Recording over!') config = { "language_code": self._transcription.language, "sample_rate_hertz": self._recording.rate, "encoding": self._transcription.encoding, } with open(full_path, "rb") as f: content = f.read() content_str = base64.b64encode(content).decode('utf-8') data = {'config': config, 'audio': {'content': content_str}} response = requests.post( f'{self.url}?key={self._authentication.api_key}', data=json.dumps(data)) result = json.loads(response.text) # TODO: Check that response was not an error. # Finally, clean up the temp directory. del config rmtree(tmp_dir_name) return result['results'][0]['alternatives'][0]['transcript']
checkpoint_path = "./ckpt/ckpt_NoisySpeechDataset_fft_512_kernel3_multi_1007/ckpt_epoch200.pt" # ネットワークモデルを指定 model = MCDUnet_kernel3() # GPUが使える場合はGPUを使用、使えない場合はCPUを使用 device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") print("使用デバイス:", device) # 学習済みのパラメータをロード model_params = torch.load(checkpoint_path, map_location=device) model.load_state_dict(model_params['model_state_dict']) # Unetを使って推論 # ネットワークを推論モードへ model.eval() # 参考: 「https://python-sounddevice.readthedocs.io/en/0.3.12/examples.html#recording-with-arbitrary-duration」 q = queue.Queue() # データを格納した順に取り出すことができるキューを作成 # Make sure the file is opened before recording anything with sf.SoundFile(estimated_voice_path, mode='w', samplerate=args.sample_rate, channels=args.channels) as file: with sd.InputStream(samplerate=args.sample_rate, blocksize=48000, device=args.device, channels=args.channels, callback=audio_callback): print('#' * 50) print('press Ctrl+C to stop the recording') print('#' * 50) while True: file.write(q.get())
#if keyId in noteBank: #noteBank[keyId].append(q) #else: #noteBank[keyId] = list(q) noteBank[keyId] = q count += q_size # # ----------- MAIN LOOP --------------- # arduino = serial.Serial('/dev/cu.usbmodem1411', 9600, timeout=.1) in_stream = sd.InputStream(callback=callback_in) out_stream = sd.OutputStream(callback=callback_out) playmode = False recmode = False print("Entering main loop...") while True: ino = arduino.read() if ino: #print(ino) if ino == b'l': LLEN = (60 / ino) * bars * sr cycle = np.zeros((int(LLEN), 2)) new_layer = np.zeros_like(cycle) if ino == b'p':
text="Listen", bd=2, relief="sunken", command=micToggle) # Microphone sample processing callback function def micCallback(data, f, t, s): sample = np.linalg.norm(data) micSamples.insert(0, sample) if len(micSamples) > msLimit: micSamples.pop() # Microphone Stream Object micStream = sd.InputStream(callback=micCallback) # Animation Loop for Mic Level and Talking Status def animationLoop(): global mouthShape global mouthOpenFull global mouthClosed global blinking global personaViewHandle if len(micSamples) > 0: # do nothing if we have no audio samples average = np.average(micSamples) # average the samples mlFrame.configure(text=format(average, "0>5.2f")) # show the average if average < 12.0: # Choose the color of the mic level bar color = "green" elif average < 16.0:
q = queue.Queue() def callback(indata, frames, time, status): """This is called (from a separate thread) for each audio block.""" if status: print(status, file=sys.stderr) q.put(indata.copy()) # Make sure the file is opened before recording anything: with sf.SoundFile(args.filename, mode='x', samplerate=args.samplerate, channels=args.channels, subtype=args.subtype) as file: with sd.InputStream(samplerate=args.samplerate, device=args.device, channels=args.channels, callback=callback): print('#' * 80) print('press Ctrl+C to stop the recording') print('#' * 80) while True: if keyboard.is_pressed('r'): break print('recording started') while True: file.write(q.get()) except KeyboardInterrupt: print('\nRecording finished: ' + repr(args.filename)) parser.exit(0)
if status: print(status, file=sys.stderr) q.put(indata.copy()) device_info = sd.query_devices(2, 'input') samplerate = int(device_info['default_samplerate']) i = 0 try: while (True): with sf.SoundFile("sciezki/output{}.wav".format(i), mode='x', samplerate=samplerate, channels=2) as file: with sd.InputStream(samplerate=samplerate, device=2, channels=2, callback=callback): czas = time.time() while (time.time() - czas) <= 5: file.write(q.get()) wynik = aT.fileClassification("sciezki/output{}.wav".format(i), "svmSMtemp", "svm") klasyfikacja = ficzery[int(wynik[0])] if wynik[1][0] < 0.5: print(klasyfikacja) print(i) if klasyfikacja == "alarm" or klasyfikacja == "rakietybaza" or klasyfikacja == "c4" or klasyfikacja == "tlumik" or klasyfikacja == "statek" or klasyfikacja == "helka": client.send(Message(text=klasyfikacja), thread_id='ID',
def __init__(self): self.stream = sd.InputStream() self.stream.start()
def create_stream(self, device=None): if self.stream is not None: self.stream.close() self.stream = sd.InputStream( device=device, channels=1, callback=self.audio_callback) self.stream.start()
def main(): args = get_parser().parse_args() if args.num_threads: torch.set_num_threads(args.num_threads) model = get_model(args).to(args.device) model.eval() print("Model loaded.") streamer = DemucsStreamer(model, dry=args.dry, num_frames=args.num_frames) device_in = parse_audio_device(args.in_) caps = query_devices(device_in, "input") channels_in = min(caps['max_input_channels'], 2) stream_in = sd.InputStream(device=device_in, samplerate=model.sample_rate, channels=channels_in) device_out = parse_audio_device(args.out) caps = query_devices(device_out, "output") channels_out = min(caps['max_output_channels'], 2) stream_out = sd.OutputStream(device=device_out, samplerate=model.sample_rate, channels=channels_out) stream_in.start() stream_out.start() first = True current_time = 0 last_log_time = 0 last_error_time = 0 cooldown_time = 2 log_delta = 10 sr_ms = model.sample_rate / 1000 stride_ms = streamer.stride / sr_ms print( f"Ready to process audio, total lag: {streamer.total_length / sr_ms:.1f}ms." ) while True: try: if current_time > last_log_time + log_delta: last_log_time = current_time tpf = streamer.time_per_frame * 1000 rtf = tpf / stride_ms print(f"time per frame: {tpf:.1f}ms, ", end='') print(f"RTF: {rtf:.1f}") streamer.reset_time_per_frame() length = streamer.total_length if first else streamer.stride first = False current_time += length / model.sample_rate frame, overflow = stream_in.read(length) frame = torch.from_numpy(frame).mean(dim=1).to(args.device) with torch.no_grad(): out = streamer.feed(frame[None])[0] if not out.numel(): continue if args.compressor: out = 0.99 * torch.tanh(out) out = out[:, None].repeat(1, channels_out) mx = out.abs().max().item() if mx > 1: print("Clipping!!") out.clamp_(-1, 1) out = out.cpu().numpy() underflow = stream_out.write(out) if overflow or underflow: if current_time >= last_error_time + cooldown_time: last_error_time = current_time tpf = 1000 * streamer.time_per_frame print( f"Not processing audio fast enough, time per frame is {tpf:.1f}ms " f"(should be less than {stride_ms:.1f}ms).") except KeyboardInterrupt: print("Stopping") break stream_out.stop() stream_in.stop()
rawchosendevice = input("\nChoose your device: ") chosendevice[i] = int(rawchosendevice) namef = (sd.query_devices(chosendevice[i], 'input')['name']) filename[i] = "Local Mic Recordings/" + time.strftime("%d%m%y_%H%M%S") + ' Device' + str(i + 1) + '_' + namef + '.wav' q = queue.Queue() def callback(indata, frames, time, status): """This is called (from a separate thread) for each audio block.""" if status: print(status, file=sys.stderr) q.put(indata.copy()) if numdevice == 1: with sf.SoundFile(filename[0], mode='x', samplerate=args.samplerate, channels=args.channels, subtype=args.subtype) as file: with sd.InputStream(samplerate=args.samplerate, device=chosendevice[0], channels=args.channels, callback=callback): print("\n") print('#' * 80) print('press Ctrl+C to stop the recording') print('#' * 80) while True: file.write(q.get()) elif numdevice == 2: with sf.SoundFile(filename[0], mode='x', samplerate=args.samplerate, channels=args.channels, subtype=args.subtype) as file: with sf.SoundFile(filename[1], mode='x', samplerate=args.samplerate, channels=args.channels, subtype=args.subtype) as file1: with sd.InputStream(samplerate=args.samplerate, device=chosendevice[0], channels=args.channels, callback=callback): with sd.InputStream(samplerate=args.samplerate, device=chosendevice[1],
pin9 = board.get_pin('d:9:p') pin10 = board.get_pin('d:10:p') pin11 = board.get_pin('d:11:p') led = board.get_pin('d:13:o') fs = 44100 T = 1.0 / fs channels = 2 seconds = .02 N = int(fs * seconds) sd.default.device = 4 sd.default.samplerate = fs sd.default.channels = channels stream = sd.InputStream() stream.start() de = collections.deque([0]) thresh_samples = [0] * 10 thresh = 20 x = 0 print('Ready!') while True: ### color wheel for i in range(10): for led in [pin9, pin5, pin6]: # scale up for i in range(100):
def recordAudio(a_name=TEST_NAME): ''' video control make sure the android video camera is already pointed to subject ''' import sounddevice as sd import soundfile as sf sd.default.device = MICROPHONE sd.default.channels = [1, 0] sd.default.latency = 'low' sd.default.samplerate = 44100 try: if a_name == TEST_NAME: print("\naudio test\n") newFile = a_name + FILE_EXTENSION else: print("\ninit audio capture\n") newFile = AUDIO_CAPTURES + a_name + FILE_EXTENSION # Checks and deletes the output file # You cant have a existing file or it will through an error if os.path.isfile(newFile): os.remove(newFile) if args.list_devices: print(sd.query_devices()) parser.exit(0) if args.samplerate is None: device_info = sd.query_devices(sd.default.device, 'input') # soundfile expects an int, sounddevice provides a float: args.samplerate = int(device_info['default_samplerate']) if args.filename != None: args.filename = tempfile.mktemp(prefix='test_', suffix='.wav', dir='') else: args.filename = newFile q = queue.Queue() def callback(indata, frames, time, status): """This is called (from a separate thread) for each audio block.""" # if status: # print(status, file=sys.stderr) q.put(indata.copy()) # Make sure the file is opened before recording anything: with sf.SoundFile(args.filename, mode='x', samplerate=args.samplerate, channels=args.channels, subtype=args.subtype) as file: with sd.InputStream(samplerate=args.samplerate, device=sd.default.device, channels=args.channels, callback=callback): print('#' * 80) print('press Ctrl+C to stop the recording') print('#' * 80) while True: file.write(q.get()) except KeyboardInterrupt: print('\nRecording finished: ' + repr(args.filename)) parser.exit(0) except Exception as e: parser.exit(type(e).__name__ + ': ' + str(e))
args.samplerate = device_info['default_samplerate'] length = int(args.window * args.samplerate / (1000 * args.downsample)) plotdata = np.zeros((length, len(args.channels))) fig, ax = plt.subplots() lines = ax.plot(plotdata) if len(args.channels) > 1: ax.legend(['channel {}'.format(c) for c in args.channels], loc='lower left', ncol=len(args.channels)) ax.axis((0, len(plotdata), -1, 1)) ax.set_yticks([0]) ax.yaxis.grid(True) ax.tick_params(bottom=False, top=False, labelbottom=False, right=False, left=False, labelleft=False) fig.tight_layout(pad=0) stream = sd.InputStream(device=args.device, channels=max(args.channels), samplerate=args.samplerate, callback=audio_callback) ani = FuncAnimation(fig, update_plot, interval=args.interval, blit=True) with stream: plt.show() except Exception as e: parser.exit(type(e).__name__ + ': ' + str(e))
def start(): caption_status.set('Conversion started, you may speak') button_start['state'] = tk.DISABLED button_stop['state'] = tk.NORMAL global device_in global device_out device_in = combobox_in_device.current() device_out = combobox_out_device.current() config['files']['weights'] = file_path config['devices']['device_in'] = str(device_in) config['devices']['device_out'] = str(device_out) with open(file_config, 'w') as configfile: config.write(configfile) segan = None def sound_processing(): global audio_process audio_process = True while audio_process: item = q_in.get() print('PROCESSING:') wav = data_in[:, 0] wav = normalize_wave_minmax(wav) wav = pre_emphasize(wav, 0.95) pwav = torch.FloatTensor(wav).view(1, 1, -1) g_wav, g_c = segan.generate(pwav) g_wav = denormalize_wave_minmax(g_wav) data_temp[:, 0] = g_wav audio = (data_temp.astype('float32') - offset) / abs_max audio = samplerate.resample(audio, ratio_out, 'sinc_best') if q_out.empty(): q_out.put(audio) data_out[:] = audio def input_callback(indata, frames, time, status): print('INPUT:') data_in[:] = indata q_in.put(data_in) def output_callback(outdata, frames, time, status): print('OUTPUT:') if not q_out.empty(): d = q_out.get() outdata[:] = data_out try: file_train = 'train.opts' #os.path.join(os.path.dirname(sys.executable), 'train.opts') with open(file_train, 'r') as cfg_f: args = ArgParser(json.load(cfg_f)) segan = WSEGAN(args) segan.G.load_pretrained(file_path, True) segan.G.eval() global input_stream global output_stream global sound_thread # sd_stream = sd.Stream(device=(device_in, device_out), # samplerate=16000, blocksize=3200, #16000 - 1 sec # dtype='int16', # channels=1, callback=callback) input_stream = sd.InputStream( device=device_in, channels=1, dtype='int16', blocksize=8000, #16000 - 1 sec samplerate=16000, callback=input_callback) output_stream = sd.OutputStream(device=device_out, channels=1, dtype='float32', blocksize=24000, samplerate=48000, callback=output_callback) sound_thread = threading.Thread(target=sound_processing) sound_thread.start() input_stream.start() output_stream.start() except Exception as e: # parser.exit(type(e).__name__ + ': ' + str(e)) button_start['state'] = tk.NORMAL button_stop['state'] = tk.DISABLED mb.showwarning("Exceptoin", str(e))