-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudio.py
93 lines (70 loc) · 3.11 KB
/
audio.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
# External imports
import pyaudio
import wave
import struct
import sys
import math
# Internal imports
import postprocessing.audio_postprocessing
from pitchprocessing.linear_pitchprocessing import LinearPitchProcessing
class Audio():
"""
"""
def __init__(self, filename, chunksize, paudio):
filestream = wave.open(filename, "rb")
self._audiostream = paudio.open(format = paudio.get_format_from_width(filestream.getsampwidth(), False),
channels = filestream.getnchannels(),
rate = filestream.getframerate(),
output = True)
self.progress = 0.0
self.loop = False
self.channels = filestream.getnchannels()
self.frequency = filestream.getframerate()
self.postprocessing = []
self.pitchprocessor = LinearPitchProcessing(1.0)
self._chunksize = chunksize
# All the formats we accept are signed
self._maxvalue = int(pow(2, 8 * filestream.getsampwidth() - 1)) - 1
# We store the data as integers here
self._audiodata = []
# These formats should work most of the time
# See: https://docs.python.org/2/library/struct.html#format-characters
types = {1: 'b', 2: 'h', 4: 'i'}
endianness = {"big": '>', "little": '<'}
# Checl for data type here
self._type = types[filestream.getsampwidth()]
# Check for system byteorder
self._endianness = endianness[sys.byteorder]
audiodata = filestream.readframes(chunksize)
# Copy byte data to integer list
while(audiodata):
fmt = self._endianness + self._type * int(len(audiodata) / filestream.getsampwidth())
data = struct.unpack(fmt, audiodata)
self._audiodata.extend(data)
audiodata = filestream.readframes(chunksize)
filestream.close()
def __del__(self):
self._audiostream.stop_stream()
self._audiostream.close()
def update(self):
# Check if playback should be in a loop
if self.loop:
self.progress = self.progress % float(len(self._audiodata))
else:
if self.progress >= len(self._audiodata):
return
chunksize = min(self._chunksize, int(math.floor((len(self._audiodata) - math.ceil(self.progress)
* self.channels) / self.pitchprocessor.pitch)))
# Apply pitch processor here
subdata = self.pitchprocessor.apply(self._audiodata, self.progress, chunksize,
self.channels, self.frequency)
# Apply post-processing here
for postprocessor in self.postprocessing:
subdata = postprocessor.apply(subdata, self.channels, self.frequency, self._maxvalue)
# Check if values are in their specified range
for i in range(0, len(subdata)):
subdata[i] = max(min(subdata[i], self._maxvalue), -self._maxvalue)
fmt = self._endianness + self._type * len(subdata)
audiodata = struct.pack(fmt, *subdata)
self._audiostream.write(audiodata)
self.progress += self.pitchprocessor.pitch * float(chunksize / self.channels)