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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
|
#!/usr/bin/env python
""" pygame.examples.sound_array_demos
Creates an echo effect on any Sound object.
Uses sndarray and numpy to create offset faded copies of the
original sound. Currently it just uses hardcoded values for the
number of echos and the delay. Easy for you to recreate as
needed.
version 2. changes:
- Should work with different sample rates now.
- put into a function.
- Uses numpy by default, but falls back on Numeric.
"""
import os
import pygame as pg
from numpy import zeros, int32, int16
import time
# pg.mixer.init(44100, -16, 0)
pg.mixer.init()
# pg.mixer.init(11025, -16, 0)
# pg.mixer.init(11025)
def make_echo(sound, samples_per_second, mydebug=True):
"""returns a sound which is echoed of the last one."""
echo_length = 3.5
a1 = pg.sndarray.array(sound)
if mydebug:
print(f"SHAPE1: {a1.shape}")
length = a1.shape[0]
# myarr = zeros(length+12000)
myarr = zeros(a1.shape, int32)
if len(a1.shape) > 1:
# mult = a1.shape[1]
size = (a1.shape[0] + int(echo_length * a1.shape[0]), a1.shape[1])
# size = (a1.shape[0] + int(a1.shape[0] + (echo_length * 3000)), a1.shape[1])
else:
# mult = 1
size = (a1.shape[0] + int(echo_length * a1.shape[0]),)
# size = (a1.shape[0] + int(a1.shape[0] + (echo_length * 3000)),)
if mydebug:
print(int(echo_length * a1.shape[0]))
myarr = zeros(size, int32)
if mydebug:
print(f"size {size}")
print(myarr.shape)
myarr[:length] = a1
# print(myarr[3000:length+3000])
# print(a1 >> 1)
# print("a1.shape %s" % (a1.shape,))
# c = myarr[3000:length+(3000*mult)]
# print("c.shape %s" % (c.shape,))
incr = int(samples_per_second / echo_length)
gap = length
myarr[incr : gap + incr] += a1 >> 1
myarr[incr * 2 : gap + (incr * 2)] += a1 >> 2
myarr[incr * 3 : gap + (incr * 3)] += a1 >> 3
myarr[incr * 4 : gap + (incr * 4)] += a1 >> 4
if mydebug:
print(f"SHAPE2: {myarr.shape}")
sound2 = pg.sndarray.make_sound(myarr.astype(int16))
return sound2
def slow_down_sound(sound, rate):
"""returns a sound which is a slowed down version of the original.
rate - at which the sound should be slowed down. eg. 0.5 would be half speed.
"""
raise NotImplementedError()
# grow_rate = 1 / rate
# make it 1/rate times longer.
# a1 = pg.sndarray.array(sound)
# surf = pg.surfarray.make_surface(a1)
# print(a1.shape[0] * grow_rate)
# scaled_surf = pg.transform.scale(surf, (int(a1.shape[0] * grow_rate), a1.shape[1]))
# print(scaled_surf)
# print(surf)
# a2 = a1 * rate
# print(a1.shape)
# print(a2.shape)
# print(a2)
# sound2 = pg.sndarray.make_sound(a2.astype(int16))
# return sound2
def sound_from_pos(sound, start_pos, samples_per_second=None, inplace=1):
"""returns a sound which begins at the start_pos.
start_pos - in seconds from the beginning.
samples_per_second -
"""
# see if we want to reuse the sound data or not.
if inplace:
a1 = pg.sndarray.samples(sound)
else:
a1 = pg.sndarray.array(sound)
# see if samples per second has been given. If not, query the pg.mixer.
# eg. it might be set to 22050
if samples_per_second is None:
samples_per_second = pg.mixer.get_init()[0]
# figure out the start position in terms of samples.
start_pos_in_samples = int(start_pos * samples_per_second)
# cut the beginning off the sound at the start position.
a2 = a1[start_pos_in_samples:]
# make the Sound instance from the array.
sound2 = pg.sndarray.make_sound(a2)
return sound2
def main():
"""play various sndarray effects"""
main_dir = os.path.split(os.path.abspath(__file__))[0]
print(f"mixer.get_init {pg.mixer.get_init()}")
samples_per_second = pg.mixer.get_init()[0]
print(("-" * 30) + "\n")
print("loading sound")
sound = pg.mixer.Sound(os.path.join(main_dir, "data", "car_door.wav"))
print("-" * 30)
print("start positions")
print("-" * 30)
start_pos = 0.1
sound2 = sound_from_pos(sound, start_pos, samples_per_second)
print(f"sound.get_length {sound.get_length()}")
print(f"sound2.get_length {sound2.get_length()}")
sound2.play()
while pg.mixer.get_busy():
pg.time.wait(200)
print("waiting 2 seconds")
pg.time.wait(2000)
print("playing original sound")
sound.play()
while pg.mixer.get_busy():
pg.time.wait(200)
print("waiting 2 seconds")
pg.time.wait(2000)
# if 0:
# #TODO: this is broken.
# print(("-" * 30) + "\n")
# print("Slow down the original sound.")
# rate = 0.2
# slowed_sound = slow_down_sound(sound, rate)
# slowed_sound.play()
# while pg.mixer.get_busy():
# pg.time.wait(200)
print("-" * 30)
print("echoing")
print("-" * 30)
t1 = time.time()
sound2 = make_echo(sound, samples_per_second)
print("time to make echo %i" % (time.time() - t1,))
print("original sound")
sound.play()
while pg.mixer.get_busy():
pg.time.wait(200)
print("echoed sound")
sound2.play()
while pg.mixer.get_busy():
pg.time.wait(200)
sound = pg.mixer.Sound(os.path.join(main_dir, "data", "secosmic_lo.wav"))
t1 = time.time()
sound3 = make_echo(sound, samples_per_second)
print("time to make echo %i" % (time.time() - t1,))
print("original sound")
sound.play()
while pg.mixer.get_busy():
pg.time.wait(200)
print("echoed sound")
sound3.play()
while pg.mixer.get_busy():
pg.time.wait(200)
pg.quit()
if __name__ == "__main__":
main()
|