-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSound.py
112 lines (80 loc) · 2.48 KB
/
Sound.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
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
import pyaudio
import wave
import soundfile as sf
CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 96000
RECORD_SECONDS = 3
def writeToFile(file):
pya = pyaudio.PyAudio()
streamAudio = pya.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK)
print("Recording started:")
frames = []
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
data = streamAudio.read(CHUNK)
frames.append(data)
print("Recording finished. ")
streamAudio.stop_stream()
streamAudio.close()
pya.terminate()
waveFile = wave.open(file, 'wb')
waveFile.setnchannels(CHANNELS)
waveFile.setsampwidth(pya.get_sample_size(FORMAT))
waveFile.setframerate(RATE)
waveFile.writeframes(b''.join(frames))
waveFile.close()
def readFromFile(plik):
print("Reading sound. ")
waveFile = wave.open(plik, 'rb')
pya = pyaudio.PyAudio()
streamAudio = pya.open(format=pya.get_format_from_width(waveFile.getsampwidth()),
channels=waveFile.getnchannels(),
rate=waveFile.getframerate(),
output=True)
framesFromFile = waveFile.readframes(CHUNK)
while len(framesFromFile) > 0:
streamAudio.write(framesFromFile)
framesFromFile = waveFile.readframes(CHUNK)
streamAudio.close()
pya.terminate()
def quantization(nazwa):
data, samplerate = sf.read(nazwa)
plik = "Rate_16_" + nazwa
sf.write(plik, data, samplerate, subtype="PCM_16")
readFromFile(plik)
plik = "Rate_8_" + nazwa
sf.write(plik, data, samplerate, subtype="PCM_U8")
readFromFile(plik)
def nagrajIOdtworz():
fileName = "Sound.py"
stringRate = '96000'
print("Rate " + stringRate)
writeToFile(stringRate + fileName)
odtworz(stringRate + fileName)
RATE = 44100
stringRate = '44100'
print("Rate " + stringRate)
writeToFile(stringRate + fileName)
odtworz(stringRate + fileName)
RATE = 22050
stringRate = '22050'
print("Rate " + stringRate)
writeToFile(stringRate + fileName)
odtworz(stringRate + fileName)
def odtworz(fileName):
readFromFile(fileName)
quantization(fileName)
def main():
wybor = input("1. Recording and playing\n2. Playing\n")
if wybor == "1":
nagrajIOdtworz()
elif wybor == "2":
fileName = "Beethoven.wav"
odtworz(fileName)
if __name__ == '__main__':
main()