Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7843ca1

Browse files
author
Simon Tippe
committed
Added support for audio postprocessing
1 parent 4dc63ff commit 7843ca1

File tree

4 files changed

+58
-6
lines changed

4 files changed

+58
-6
lines changed

‎audio.py

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,54 @@
1+
# External imports
12
import pyaudio
23
import wave
34
import struct
45
import sys
56
import math
67

7-
class Audio:
8+
# Internal imports
9+
import postprocessing.audio_postprocessing
10+
11+
class Audio():
812
"""
913
1014
"""
1115
def __init__(self, filename, chunksize, paudio):
16+
1217
filestream = wave.open(filename, "rb")
1318

14-
self._audiostream = paudio.open(format = paudio.get_format_from_width(filestream.getsampwidth()),
19+
self._audiostream = paudio.open(format = paudio.get_format_from_width(filestream.getsampwidth(), False),
1520
channels = filestream.getnchannels(),
1621
rate = filestream.getframerate(),
1722
output = True)
1823

1924
self.progress = 0.0
2025
self.pitch = 1.0
21-
self.volume = 1.0
26+
self.channels = filestream.getnchannels()
27+
self.frequency = filestream.getframerate()
28+
self.postprocessing = []
29+
2230
self._chunksize = chunksize
2331

32+
# All the format we accept are signed
33+
self._maxvalue = int(pow(2, 8 * filestream.getsampwidth() - 1)) - 1
34+
2435
# We store the data as integers here
2536
self._audiodata = []
2637

2738
# These formats should work most of the time
2839
# See: https://docs.python.org/2/library/struct.html#format-characters
29-
types = {1: 'B', 2: 'h', 4: 'i'}
40+
types = {1: 'b', 2: 'h', 4: 'i'}
41+
endianness = {"big": '>', "little": '<'}
42+
43+
# Checl for data type here
3044
self._type = types[filestream.getsampwidth()]
45+
3146
# Check for system byteorder
32-
endianness = {"big": '>', "little": '<'}
3347
self._endianness = endianness[sys.byteorder]
3448

3549
audiodata = filestream.readframes(chunksize)
3650

51+
# Copy byte data to integer list
3752
while(audiodata):
3853
fmt = self._endianness + self._type * int(len(audiodata) / filestream.getsampwidth())
3954
data = struct.unpack(fmt, audiodata)
@@ -52,6 +67,18 @@ def update(self):
5267
index = math.floor(self.progress)
5368
count = math.ceil(readout)
5469
subdata = self._audiodata[index : min(index + count, len(self._audiodata))]
70+
71+
# Apply pitch processor here
72+
73+
74+
# Apply post-processing here
75+
for postprocessor in self.postprocessing:
76+
subdata = postprocessor.apply(subdata, self.channels, self.frequency)
77+
78+
# Check values are in their specified range
79+
for i in range(0, len(subdata)):
80+
subdata[i] = max(min(subdata[i], self._maxvalue), -self._maxvalue)
81+
5582
fmt = self._endianness + self._type * len(subdata)
5683
audiodata = struct.pack(fmt, *subdata)
5784
self._audiostream.write(audiodata)

‎main.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import audio
2+
from postprocessing.volume_postprocessing import VolumePostprocessing
23
import pyaudio
34

45
paudio = pyaudio.PyAudio()
56

67
audio = audio.Audio("MenuTheme2_final.wav", 1024, paudio)
78

8-
audio.volume=2.0
9+
audio.postprocessing.append(VolumePostprocessing(10.0))
910

1011
while True:
1112
audio.update()

‎postprocessing/audio_postprocessing.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"""
2+
Abstract class for audio postprocessing.
3+
Audio postprocessing is applied per audio stream
4+
after the pitch processing.
5+
"""
6+
class AudioPostprocessing():
7+
8+
"""
9+
10+
"""
11+
def apply(self, data, channels, frequency):
12+
raise NotImplementedError("Class %s doesn't implement apply()" % (self.__class__.__name__))

‎postprocessing/volume_postprocessing.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import postprocessing.audio_postprocessing
2+
3+
class VolumePostprocessing(postprocessing.audio_postprocessing.AudioPostprocessing):
4+
5+
def __init__(self, volume = 1.0):
6+
self.volume = volume
7+
8+
def apply(self, data, channels, frequency):
9+
for i in range(0, len(data)):
10+
data[i] = int(float(data[i]) * self.volume)
11+
12+
return data

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /