| 
									
										
										
										
											2021-03-13 11:37:38 +01:00
										 |  |  | import sounddevice as sd | 
					
						
							|  |  |  | import matplotlib.pyplot as plt | 
					
						
							|  |  |  | import numpy as np | 
					
						
							| 
									
										
										
										
											2021-04-15 12:08:50 +07:00
										 |  |  | import platform | 
					
						
							| 
									
										
										
										
											2022-12-29 14:17:43 +01:00
										 |  |  | import csv | 
					
						
							| 
									
										
										
										
											2021-03-13 11:37:38 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | if __name__ == '__main__': | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-15 12:08:50 +07:00
										 |  |  |     # If you got "ValueError: No input device matching", that is because your PC name example device | 
					
						
							|  |  |  |     # differently from tested list below. Uncomment the next line to see full list and try to pick correct one | 
					
						
							|  |  |  |     # print(sd.query_devices()) | 
					
						
							| 
									
										
										
										
											2021-03-13 11:37:38 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-15 12:08:50 +07:00
										 |  |  |     fs = 48000           # Sample rate | 
					
						
							|  |  |  |     duration = 100e-3    # Duration of recording | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if platform.system() == 'Windows': | 
					
						
							|  |  |  |         # MME is needed since there are more than one MicNode device APIs (at least in Windows) | 
					
						
							|  |  |  |         device = 'Microphone (MicNode) MME' | 
					
						
							|  |  |  |     elif platform.system() == 'Darwin': | 
					
						
							|  |  |  |         device = 'MicNode' | 
					
						
							|  |  |  |     else: | 
					
						
							|  |  |  |         device ='default' | 
					
						
							| 
									
										
										
										
											2021-03-13 11:37:38 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     myrecording = sd.rec(int(duration * fs), samplerate=fs, channels=1, dtype='int16', device=device) | 
					
						
							|  |  |  |     print('Waiting...') | 
					
						
							|  |  |  |     sd.wait()  # Wait until recording is finished | 
					
						
							|  |  |  |     print('Done!') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     time = np.arange(0, duration, 1 / fs)  # time vector | 
					
						
							|  |  |  |     plt.plot(time, myrecording) | 
					
						
							|  |  |  |     plt.xlabel('Time [s]') | 
					
						
							|  |  |  |     plt.ylabel('Amplitude') | 
					
						
							|  |  |  |     plt.title('MicNode') | 
					
						
							| 
									
										
										
										
											2021-03-13 11:41:46 +01:00
										 |  |  |     plt.show() | 
					
						
							| 
									
										
										
										
											2022-12-29 14:17:43 +01:00
										 |  |  |      | 
					
						
							|  |  |  |     samples = np.array(myrecording) | 
					
						
							|  |  |  |     np.savetxt('Output.csv', samples, delimiter=",", fmt='%s') | 
					
						
							| 
									
										
										
										
											2021-03-13 11:41:46 +01:00
										 |  |  |      |