forked from ag1le/deepmorse-decoder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmic_read.py
More file actions
81 lines (63 loc) · 1.69 KB
/
Copy pathmic_read.py
File metadata and controls
81 lines (63 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
"""
mic_read.py
Created By Alexander Yared (akyared@gmail.com)
Microphone controller module for the Live Spectrogram project, a real time
spectrogram visualization tool
Dependencies: pyaudio, numpy and matplotlib
"""
############### Import Libraries ###############
import pyaudio
import numpy as np
import matplotlib.pyplot as plt
############### Constants ###############
# RATE = 44100 #sample rate
RATE = 8000
FORMAT = pyaudio.paInt16 # conversion format for PyAudio stream
CHANNELS = 1 # microphone audio channels
CHUNK_SIZE = 8192 # number of samples to take per read
SAMPLE_LENGTH = int(CHUNK_SIZE * 1000 / RATE) # length of each sample in ms
############### Functions ###############
"""
open_mic:
creates a PyAudio object and initializes the mic stream
inputs: none
ouputs: stream, PyAudio object
"""
def open_mic():
pa = pyaudio.PyAudio()
stream = pa.open(
format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK_SIZE,
)
return stream, pa
"""
get_data:
reads from the audio stream for a constant length of time, converts it to data
inputs: stream, PyAudio object
outputs: int16 data array
"""
def get_data(stream, pa):
input_data = stream.read(CHUNK_SIZE)
data = np.fromstring(input_data, np.int16)
return data
############### Test Functions ###############
"""
make_10k:
creates a 10kHz test tone
"""
def make_10k():
x = np.linspace(-2 * np.pi, 2 * np.pi, 21000)
x = np.tile(x, int(SAMPLE_LENGTH / (4 * np.pi)))
y = np.sin(2 * np.pi * 5000 * x)
return x, y
"""
show_freq:
plots the test tone for a sanity check
"""
def show_freq():
x, y = make_10k()
plt.plot(x, y)
plt.show()