-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathpyrecplay_samplingblock.py
66 lines (51 loc) · 1.79 KB
/
pyrecplay_samplingblock.py
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
"""
PyAudio Example: Make a sampling between input and output (i.e., record a
few samples, multiply them with a unit pulse train, and play them back immediately).
Using block-wise processing instead of a for loop
Gerald Schuller, Octtober 2014
"""
import pyaudio
import struct
#import math
#import array
import numpy as np
#import scipy
CHUNK = 5000 #Blocksize
WIDTH = 2 #2 bytes per sample
CHANNELS = 1 #2
RATE = 32000 #Sampling Rate in Hz
RECORD_SECONDS = 8
p = pyaudio.PyAudio()
stream = p.open(format=p.get_format_from_width(WIDTH),
channels=CHANNELS,
rate=RATE,
input=True,
output=True,
#input_device_index=10,
frames_per_buffer=CHUNK)
print("* recording")
#Loop for the blocks:
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
#Reading from audio input stream into data with block length "CHUNK":
data = stream.read(CHUNK)
#Convert from stream of bytes to a list of short integers (2 bytes here) in "samples":
#shorts = (struct.unpack( "128h", data ))
shorts = (struct.unpack( 'h' * CHUNK, data ));
samples=list(shorts);
#start block-wise signal processing:
#Compute a block/an array of a unit pulse train corresponding a downsampling rate of N:
N=8.0;
#s=np.modf(np.arange(0,CHUNK)/N)[0]==0.0
#make unit pulse train with modulus function "%":
s=(np.arange(0,CHUNK)%N)==0
#multiply the signal with the unit pulse train:
samples=samples*s;
#end signal processing
#converting from short integers to a stream of bytes in "data":
data=struct.pack('h' * len(samples), *samples);
#Writing data back to audio output stream:
stream.write(data, CHUNK)
print("* done")
stream.stop_stream()
stream.close()
p.terminate()