This repository has been archived by the owner on Mar 11, 2024. It is now read-only.
forked from hmc-cs111-fall2015/fluency-lab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hw3pr1.py
343 lines (235 loc) · 9.79 KB
/
hw3pr1.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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# -*- coding: utf-8 -*-
import random
import math
from csaudio import play, readwav, writewav
def scale(L, scale_factor):
'''Given a list L, scales each element of the list by scale_factor'''
return [scale_factor * n for n in L]
def wrap1( L ):
""" Given a list L, shifts each element in L to the right by 1
(the element at the end of the list wraps around to the beginning)
"""
# I changed the body of this function to call wrapN,
# so that I can reuse the code I wrote for that problem
return wrapN(L, 1)
def wrapN(L, N):
""" Given a list L, shifts each element in L to the right by N
(elements at the end of the list wrap around to the beginning)
"""
length = len(N)
return [ L[i-N] for i in range(length) ]
def add_2(L, M):
'''Given two lists, L and M, adds their respective elements. If the two
lists have different lengths, the function truncates the result so that
it is as long as the shorter list.
'''
length = min(len(L), len(M)) # find the shorter length
return [ L[i] + M[i] for i in range(length) ]
# Here's an alternative solution that uses the built-in zip function,
# which truncates for us and creates tuples of the corresponding elements.
#return [l + m for (l, m) in zip(L, M)]
def add_scale_2(L, M, L_scale, M_scale):
'''Given two lists, L and M, and two scale factors, L_scale and M_scale,
scales each list by its respective scale factor, then adds the
results, pairwise. If the two lists have different lengths, the function
truncates the result so that it is as long as the shorter list.
'''
return add_2(scale(L, L_scale), scale(M, M_scale)) # yay for code re-use!
# generalized versions of add_2 and add_scale_2
def add_N(lists):
'''Given a list of lists, adds their respective elements. If the two
lists have different lengths, the function truncates the result so that
it is as long as the shortest list.
'''
return map(sum, apply(zip, lists)) # lots of higher-order functions here!
def add_scale_N(lists, scaleFactors):
'''Given a list of lists and a list of scale factors, scales each list by
its respective scale factor, then sums the results, element-wise. If the
lists have different lengths, the function truncates the result so that
it is as long as the shortest list.
'''
scaledLists = [scale(l, f) for (l, f) in zip(lists, scaleFactors)]
return add_N(scaledLists)
# Helper function: randomize
def randomize( x, chance_of_replacing ):
""" randomize takes in an original value, x
and a fraction named chance_of_replacing.
With the "chance_of_replacing" chance, it
should return a random float from -32767 to 32767.
Otherwise, it should return x (not replacing it).
"""
r = random.uniform(0,1)
if r < chance_of_replacing:
return random.uniform(-32768,32767)
else:
return x
def replace_some(L, chance_of_replacing):
'''Given a list L, returns a new list L' where each element in L has a
chance_of_replacing chance of being replaced with a random,
floating-point value in the range -32767 to 32767.
'''
return [randomize(e, chance_of_replacing) for e in L]
#
# below are functions that relate to sound-processing ...
#
# a function to make sure everything is working
def test():
""" a test function that plays swfaith.wav
You'll need swfailt.wav in this folder.
"""
play( 'swfaith.wav' )
# The example changeSpeed function
def changeSpeed(filename, newsr):
""" changeSpeed allows the user to change an audio file's speed
input: filename, the name of the original file
newsr, the *new* sampling rate in samples per second
output: no return value; creates and plays the file 'out.wav'
"""
samps, sr = readwav(filename)
print "The first 10 sound-pressure samples are\n", samps[:10]
print "The original number of samples per second is", sr
newsamps = samps # no change to the sound
writewav( newsamps, newsr, "out.wav" ) # write data to out.wav
print "\nPlaying new sound..."
play( 'out.wav' ) # play the new file, 'out.wav'
def flipflop(filename):
""" flipflop swaps the halves of an audio file
input: filename, the name of the original file
output: no return value, but
this creates the sound file 'out.wav'
and plays it
"""
print "Playing the original sound..."
play(filename)
print "Reading in the sound data..."
samps, sr = readwav(filename)
print "Computing new sound..."
# this gets the midpoint and calls it x
x = len(samps)/2
newsamps = samps[x:] + samps[:x] # flip flop
newsr = sr # no change to the sr
writewav( newsamps, newsr, "out.wav" )
print "Playing new sound..."
play( 'out.wav' )
# Sound function to write #1: reverse
def reverse(filename):
""" reverse reverses the original file
input: filename, the name of the original file
output: no return value, but
this creates the sound file 'out.wav'
and plays it
"""
print "Playing the original sound..."
play(filename)
print "Reading in the sound data..."
samps, sr = readwav(filename)
print "Computing new sound..."
newsamps = samps[::-1] # reverse
newsr = sr # no change to the sr
writewav( newsamps, newsr, "out.wav" )
print "Playing new sound..."
play( 'out.wav' )
# Sound function to write #2: volume
def volume(filename, scale_factor):
""" volume increases the volume by the floating-point value scale_value
input: filename, the name of the original file
output: no return value, but
this creates the sound file 'out.wav'
and plays it
"""
print "Playing the original sound..."
play(filename)
print "Reading in the sound data..."
samps, sr = readwav(filename)
print "Computing new sound..."
newsamps = [x * scale_factor for x in samps]
newsr = sr # no change to the sr
writewav( newsamps, newsr, "out.wav" )
print "Playing new sound..."
play( 'out.wav' )
# Sound function to write #3: static
def static(filename, probability_of_static):
""" static introduces random bits of static based on the probability given
input: filename, the name of the original file
output: no return value, but
this creates the sound file 'out.wav'
and plays it
"""
print "Playing the original sound..."
play(filename)
print "Reading in the sound data..."
samps, sr = readwav(filename)
print "Computing new sound..."
newsamps = replace_some(samps, probability_of_static)
newsr = sr # no change to the sr
writewav( newsamps, newsr, "out.wav" )
print "Playing new sound..."
play( 'out.wav' )
# Sound function to write #4: overlay
def overlay(filename1, filename2):
""" overlay creates a sound that overlays the two given sound files
input: filename, the name of the original file
output: no return value, but
this creates the sound file 'out.wav'
and plays it
"""
print "Playing the original sound..."
play(filename1)
play(filename2)
print "Reading in the sound data..."
samps1, sr1 = readwav(filename1)
samps2, sr2 = readwav(filename2)
print "Computing new sound..."
newsamps = add_scale_2(samps1, samps2, 0.5, 0.5)
newsr = (sr1+sr2)/2 # no change to the sr
writewav( newsamps, newsr, "out.wav" )
print "Playing new sound..."
play( 'out.wav' )
# Sound function to write #5: echo
def echo(filename, time_delay):
""" echo takes in a filename and overlays the same sound on itself, shifted by time_delay
input: filename, the name of the original file
output: no return value, but
this creates the sound file 'out.wav'
and plays it
"""
print "Playing the original sound..."
play(filename)
print "Reading in the sound data..."
samps, sr = readwav(filename)
print "Computing new sound..."
echosamps = [0]*(time_delay*float(sr)) + samps
echosamps = echosamps[:len(samps)]
newsamps = [echosamps[i]+samps[i] for i in xrange(len(samps))]
newsr = sr # no change to the sr
writewav( newsamps, newsr, "out.wav" )
print "Playing new sound..."
play( 'out.wav' )
# Helper function for generating pure tones
def gen_pure_tone(freq, seconds):
""" pure_tone returns the y-values of a cosine wave
whose frequency is freq Hertz.
It returns nsamples values, taken once every 1/44100 of a second;
thus, the sampling rate is 44100 Hertz.
0.5 second (22050 samples) is probably enough.
"""
sr = 44100
# how many data samples to create
nsamples = int(seconds*sr) # rounds down
# our frequency-scaling coefficient, f
f = 2*math.pi/sr # converts from samples to Hz
# our amplitude-scaling coefficient, a
a = 32767.0
# the sound's air-pressure samples
samps = [ a*math.sin(f*n*freq) for n in range(nsamples) ]
# return both...
return samps, sr
def pure_tone(freq, time_in_seconds):
""" plays a pure tone of frequence freq for time_in_seconds seconds """
print "Generating tone..."
samps, sr = gen_pure_tone(freq, time_in_seconds)
print "Writing out the sound data..."
writewav( samps, sr, "out.wav" )
print "Playing new sound..."
play( 'out.wav' )
# Sound function to write #6: chord