SoundGeneration Wiki
SoundGeneration Wiki
org
/wiki/SoundGeneration
SoundGeneration — wiki
""" Some examples for generating and converting sounds for pygame.
Shows:
- a simple 'square wave' generated
Square Wave
https://en.wikipedia.org/wiki/Square_wave
https://en.wikipedia.org/wiki/MOD_(file_format)
pygame.mixer.get_init
https://www.pygame.org/docs/ref/mixer.html#pygame.mixer.get_init
pygame.Sound
https://www.pygame.org/docs/ref/mixer.html#pygame.mixer.Sound
https://docs.python.org/3/library/array.html
https://docs.python.org/3/library/wave.html
https://docs.python.org/3/library/audioop.html?
highlight=audio#audioop.ratecv
"""
class Tone(pg.mixer.Sound):
"""This generates a 'Square wave' with a generator.
1/7
Then creates an array of samples, and passes that into pygame.Sound.
"""
self.frequency = frequency
if array_type == 'b':
samples = self.signed_char_to_signed_short(
self.make_samples_b()
samples = self.make_samples_h()
else:
raise ValueError('array_type not supported')
pg.mixer.Sound.__init__(self, buffer=samples)
self.set_volume(volume)
def make_samples_b(self):
"""
mixer_frequency = pg.mixer.get_init()[0]
mixer_format = pg.mixer.get_init()[1]
max_amplitude = 2 ** (abs(mixer_format) - 1) - 1
# print(f'mixer_frequency:{mixer_frequency}, mixer_format:
{mixer_format}')
# print(f'period:{period}, max_amplitude:{max_amplitude}')
# https://docs.python.org/3/library/array.html
samples = array('b',
)
return samples
2/7
"""
def make_samples_h(self):
"""
mixer_frequency = pg.mixer.get_init()[0]
mixer_format = pg.mixer.get_init()[1]
max_amplitude = 2 ** (abs(mixer_format) - 1) - 1
# print(f'mixer_frequency:{mixer_frequency}, mixer_format:
{mixer_format}')
# print(f'period:{period}, max_amplitude:{max_amplitude}')
# https://docs.python.org/3/library/array.html
samples = array('h',
)
return samples
class Sample(pg.mixer.Sound):
""" For playing a sample.
"""
def __init__(self, fname, volume=.1):
array('b', f.read())
pg.mixer.Sound.__init__(self, buffer=samples)
self.set_volume(volume)
3/7
"""
import time
t0=time.time()
factor = int(32767 / 127)
samples = array('h', (
t1=time.time()
print(t1-t0)
return samples
def fetch_example_mod_file(mod_fname):
""" Grab a file that has a sound samples in it from the net.
and was the first module file format. MOD files use the ".MOD"
https://en.wikipedia.org/wiki/MOD_(file_format)
"""
import os
url = 'https://api.modarchive.org/downloads.php?moduleid=101996'
if not os.path.exists(mod_fname):
import urllib2
print ('Fetching %s .mod into file: %s' % (url, mod_fname))
data = urllib2.urlopen(url).read()
modf.write(data)
def resample(mod_fname):
4/7
"""
import audioop
import wave
from io import BytesIO
in_framerate = 8363
in_sampwidth = 1
in_nchannels = 1
out_framerate = 44100
num_seconds = 5
f.read(8363*2)
in_frame_data = f.read(in_framerate * num_seconds)
# https://docs.python.org/3/library/audioop.html?
highlight=audio#audioop.ratecv
in_frame_data,
in_sampwidth,
in_nchannels,
in_framerate,
out_framerate,
None)
# print(f'len(newfragment):{len(newfragment)}')
pg.mixer.Sound(buffer=newfragment).play(-1)
# TODO:
# Converting between modo and stereo?
# https://docs.python.org/3/library/audioop.html?
highlight=audio#audioop.tomono
5/7
# using pygame.draw.lines transforming audio into
# Surface space.
# Saw tooth.
if __name__ == "__main__":
# Set the mixer to have less buffer size, so it
# https://www.pygame.org/docs/ref/mixer.html#pygame.mixer.init
pg.display.set_mode((320, 200))
mod_fname = 'outrun_3.mod'
fetch_example_mod_file(mod_fname)
# https://www.pygame.org/docs/ref/mixer.html#pygame.mixer.Sound.play
if 0:
Tone(frequency=808, array_type='b').play(-1)
if 0:
try:
Sample(mod_fname).play(-1)
except IOError:
print ('no %s' % mod_fname)
if 0:
pg.mixer.music.load(mod_fname)
pg.mixer.music.play()
if 1:
resample(mod_fname)
going = True
while going:
for e in pg.event.get():
6/7
if e.type in [pg.QUIT, pg.KEYDOWN]:
going = False
7/7