-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecord.py
59 lines (46 loc) · 1.7 KB
/
record.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
# Records audio directly from the computer
import sounddevice as sd
import soundfile as sf
import wavio
# Initial recording is saved as a np file
import numpy as np
from scipy.io.wavfile import write
# Used to dictate how long we are recording for
import time
# Record audio for duration seconds
def record(duration, filename, IBMthread, threadList, index):
# Closes all the previous threads
i = 1
while(i < index):
if (not threadList[i].is_alive()):
threadList[i].join()
i = i + 1
# Indicate that audio is being recorded
print("Audio" + str(index) + " is being recorded")
# Default Variables
fs = 44100
sd.default.samplerate = fs
sd.default.channels = 2
# Record the audio for duration time and save it into filename + ".wav"
myarray = sd.rec(int(duration * fs))
sd.wait()
wavio.write("./WavFiles/" + filename + '.wav', myarray, fs, sampwidth=2)
# Prints that this recording has finished
print("Finished recording. File saved in " + filename + ".wav")
# Converts the file to a flac
wavToFlac(filename)
# Starts converting the created flac to a text file
IBMthread.start()
# Starts the next recording
if (index < len(threadList) - 1):
threadList[i + 1].start()
# Ends IBMthread
IBMthread.join()
threadList[0] = threadList[0] + 1
# Converts a .wav file to .flac
def wavToFlac(filename):
print("Started convert " + filename + "wav to flac")
# Reads the wav file and writes it into a flac
wav, samplerate = sf.read("./WavFiles/" + filename + '.wav')
sf.write("./FlacFiles/" + filename + '.flac', wav, samplerate)
print("Finished converting " + filename + "wav to flac")