-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwav.py
50 lines (44 loc) · 3.04 KB
/
wav.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
# aud2arr
# In this example an array of numbers is converted to an audio file.
# I just need to do this in reverse
import wave
import array
# Series of numbers representing the positions of the speaker
series = [
-95, 50, -103, 58, 38, 102, 17, 97, 14, 64, -32, 17, -81, 18, 76, -73, 121, 30, -93, -43, 81, 27, 109,
-77, 9, -46, 105, -30, -77, 19, 16, 64, 50, -71, -113, 113, 61, 35, 18, -88, -66, -13, -10, 114, 57,
-75, 90, 40, 46, -18, 69, 126, -45, -9, -58, 73, 89, 55, -89, 35, 69, -59, 67, -21, 41, 96, -85, 38,
-127, -119, 94, 6, -35, -2, 40, -47, -59, -27, -14, 67, -5, 79, -58, -47, 58, -93, -89, -45, 61, 112,
85, 50, -113, 85, -33, -26, -63, -55, 113, 94, -32, 33, 37, -87, 116, 61, 85, 124, -34, 56, -97, 64,
116, 9, -43, -29, -128, 11, -28, -71, 7, 54, 60, 35, 110, 42, -22, -33, -77, -38, 114, 32, -68, 74,
66, 112, 52, 60, -64, -92, 79, -33, 53, 76, -120, -76, 59, -11, 109, 55, -66, -50, -68, 15, 2, -72,
-52, -20, 99, 9, -34, -80, -79, 6, -3, 2, -84, -104, 61, 37, 41, -40, -2, 120, -68, 111, -70, -123,
-77, -84, 120, 127, 115, 118, 59, 45, -107, -52, -22, -120, 97, 77, -57, 40, -41, 17, 27, 10, -115,
33, -86, 117, -120, 36, -18, -80, -13, 52, 79, 83, 114, -53, -118, -13, -87, -28, 1, -59, -126, -51,
109, -10, -98, -32, -62, -120, 16, -65, -87, 79, 70, -9, 31, -22, -60, -84, 102, -103, 108, -38, 7,
-18, 68, -40, 97, 80, 0, 68, -77, 24, 97, -112, 34, 63, 26, -56, 31, -36, -109, 24, 3, 100, 23, -40,
-26, 90, 50, 103, 27, -38, 24, 5, 118, -43, 111, -5, 96, 83, -52, -29, 15, 122, -26, -4, 77, 99, -53,
47, -70, 24, -1, -80, -113, -116, -90, -17, 53, 11, -120, 120, -9, -20, 88, -44, 24, -2, -101, -77, 30,
79, -118, -48, -85, 26, -73, -9, -104, 107, 106, 109, -75, -97, -74, -10, 18, 37, 21, 106, -40, -12, 43,
-36, -106, -126, -39, 48, -66, 40, -103, 64, 121, -39, -95, 62, 84, 39, -124, 123, -36, -31, -122, 50,
-60, 101, -78, -83, 21, -59, 16, -56, -14, -79, 20, -87, -55, -55, 21, -70, 40, 106, 16, -100, -74, -34,
61, 96, -62, 75, -18, -30, 31, 45, 105, 72, 95, 9, -46, 0, -47, 77, 64, -110, 4, -42, -30, -59, -8, 67,
-32, 15, -9, 84, -6, -73, 81, 54, -114, -43, 1, 78, 11, 63, -12, -83, 8, -15, -15, 85, -95, -75, -10,
39, 59, -65, -93, -4, -8, 101, -126, 2, 23, -44, 115, -115, 61, -115, -97, -40, 72, -38, -126, 99, 104,
-16, 95, 71, 41, 66, -72, 38, 115, -78, 126, -35, 13, 36, -121, 34, 60, 103, -5, 105, -116, 34, -113,
67, -8, 66, -97, -100, -128, 34, 8, 21, -102, -57, 18, 59, 10, -125, -4, 104, -107, 100, 31, 62, 100,
118, -91, 0, -4, 53, -106, 45, -51, -42, -51, -59, 76, -17
]
# Audio characteristics
sample_rate = 8000
num_channels = 1
sample_width = 1 # 2 is 16-bit, 1 is 8-bit
# Create an array of signed short integers (16-bit) from the series
audio_array = array.array('h', series)
# Open a new WAV file
with wave.open('output.wav', 'w') as wav_file:
wav_file.setnchannels(num_channels)
wav_file.setsampwidth(sample_width)
wav_file.setframerate(sample_rate)
# Write the audio array data to the WAV file
wav_file.writeframes(audio_array.tobytes())