-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplay_and_record.py
More file actions
122 lines (90 loc) · 2.51 KB
/
Copy pathplay_and_record.py
File metadata and controls
122 lines (90 loc) · 2.51 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# -*- coding: utf-8 -*-
"""
Created on Oct 15 21:33:54 2015
@author: Owen
"""
# -*- coding: utf-8 -*-
import pyaudio
import wave
import numpy as np
import pylab as pl
def readwav(path):
f = wave.open(path,"rb")
# get parameters
params = f.getparams()
nchannels, sampwidth, framerate, nframes = params[:4]
# read data of the wave
str_data = f.readframes(nframes)
f.close()
#transfer data into array
wave_data = np.fromstring(str_data, dtype=np.short)
wave_data.shape = -1, 2
wave_data = wave_data.T
time = np.arange(0, nframes) * (1.0 / framerate)
return time, wave_data
readf = wave.open(r"sample.wav", 'rb')
CHUNK = 1024
params = readf.getparams()
CHANNELS, SAMPLEWIDTH, RATE, NFRAMES = params[:4]
# pyaudio for playing the file
pread = pyaudio.PyAudio()
# pyaudio for recording the sound
precord = pyaudio.PyAudio()
FORMAT = pread.get_format_from_width(SAMPLEWIDTH)
# Open stream
# stream 1 is for playing, while stream1 is for recording
stream1 = pread.open(format = FORMAT,
channels = CHANNELS,
rate = RATE,
output = True)
stream2 = precord.open(format = FORMAT,
channels = CHANNELS,
rate = RATE,
input = True)
# write frames into stream2, record
print("* recording")
frames = []
while True:
# write data in stream 1 and play the file
data = readf.readframes(CHUNK)
if data == "": break
stream1.write(data)
# Record the sound
read_data = stream2.read(CHUNK)
frames.append(read_data)
print("* done recording")
# close all
stream1.close()
stream2.close()
pread.terminate()
precord.terminate()
readf.close()
# write recorded data
wf = wave.open("output.wav", 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(SAMPLEWIDTH)
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
wf.close()
time1, wave_data = readwav("sample.wav")
time2, read_data = readwav("output.wav")
print("time1: " + str(len(time1)),"time2: " + str(len(time2)))
#transfer data into array
# plot
pl.subplot(321)
pl.plot(time1, wave_data[0],'g')
pl.xlabel("Original: Channel 1")
pl.subplot(323)
pl.plot(time1, wave_data[1],'g')
pl.xlabel("Original: Channel 2")
pl.subplot(325)
pl.magnitude_spectrum(wave_data[0],Fs = RATE,color='g')
pl.subplot(322)
pl.plot(time2, read_data[0],'b')
pl.xlabel("Record: Channel 1")
pl.subplot(324)
pl.plot(time2, read_data[1],'b')
pl.xlabel("Record: Channel 2")
pl.subplot(326)
pl.magnitude_spectrum(read_data[0],Fs = RATE,color='b')
pl.show()