Skip to content

Commit 574adad

Browse files
author
tippesi
committed
Update with new postprocessor
1 parent d93b598 commit 574adad

File tree

4 files changed

+22
-11
lines changed

4 files changed

+22
-11
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,9 @@
11
# Python Audio
2+
## What you need
3+
- Python 3.6.x installed (PyAudio won't work with 3.7.x)
4+
- PyAudio installed
5+
## What you get
6+
- Tiny framework which allows to play with audio effects
7+
- Easy to integratge postprocessing steps per audio stream
8+
## Example
9+
Have a look at main.py

audio.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class Audio():
1212
"""
1313
1414
"""
15+
1516
def __init__(self, filename, chunksize, paudio):
1617

1718
filestream = wave.open(filename, "rb")
@@ -23,6 +24,7 @@ def __init__(self, filename, chunksize, paudio):
2324

2425
self.progress = 0.0
2526
self.pitch = 1.0
27+
self.loop = False
2628
self.channels = filestream.getnchannels()
2729
self.frequency = filestream.getframerate()
2830
self.postprocessing = []
@@ -62,7 +64,14 @@ def __del__(self):
6264
self._audiostream.close()
6365

6466
def update(self):
65-
self.progress = self.progress % float(len(self._audiodata))
67+
# Check if playback should be in a loop
68+
if self.loop:
69+
self.progress = self.progress % float(len(self._audiodata))
70+
else:
71+
if self.progress >= len(self._audiodata):
72+
return
73+
74+
# Depending on the pitch we want to process a different number of samples
6675
readout = self.pitch * float(self._chunksize)
6776
index = math.floor(self.progress)
6877
count = math.ceil(readout)
@@ -75,12 +84,11 @@ def update(self):
7584
for postprocessor in self.postprocessing:
7685
subdata = postprocessor.apply(subdata, self.channels, self.frequency)
7786

78-
# Check values are in their specified range
87+
# Check if values are in their specified range
7988
for i in range(0, len(subdata)):
8089
subdata[i] = max(min(subdata[i], self._maxvalue), -self._maxvalue)
8190

8291
fmt = self._endianness + self._type * len(subdata)
8392
audiodata = struct.pack(fmt, *subdata)
84-
print(audiodata)
8593
self._audiostream.write(audiodata)
8694
self.progress += readout

main.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
import audio
1+
from audio import Audio
22
from postprocessing.volume_postprocessing import VolumePostprocessing
33
from postprocessing.channel_sine_postprocessing import ChannelSinePostprocessing
44
import pyaudio
55

66
paudio = pyaudio.PyAudio()
77

8-
audio = audio.Audio("MenuTheme2_final.wav", 1024, paudio)
8+
audio = Audio("MenuTheme2_final.wav", 2048, paudio)
99

1010
# audio.postprocessing.append(VolumePostprocessing(1.0))
11-
audio.postprocessing.append(ChannelSinePostprocessing(0.1))
11+
audio.postprocessing.append(ChannelSinePostprocessing(0.5))
1212

1313
while True:
1414
audio.update()

postprocessing/channel_sine_postprocessing.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,10 @@ def apply(self, data, channels, frequency):
2020
leftVolume = 0.5 * math.sin(deltatime) + 0.5
2121
rightVolume = 1.0 - leftVolume
2222

23-
# print(leftVolume)
24-
2523
for i in range(0, len(data)):
26-
"""
2724
if i % 2:
2825
data[i] = int(float(data[i]) * leftVolume)
2926
else:
3027
data[i] = int(float(data[i]) * rightVolume)
31-
"""
32-
data[i] = data[i] & 255
3328

3429
return data

0 commit comments

Comments
 (0)