File: generate_soundfiles.py

package info (click to toggle)
pysoundfile 0.10.3%2Bpost1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 268 kB
  • sloc: python: 1,789; makefile: 156; sh: 21
file content (75 lines) | stat: -rwxr-xr-x 1,579 bytes parent folder | download | duplicates (5)
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
#!/usr/bin/env python

import struct


def uint32(number):
    return struct.pack('<I', number)


def uint16(number):
    return struct.pack('<H', number)


mono_raw = struct.pack('<5h', 0, 1, 2, -2, -1)

# floating point data is typically limited to the interval [-1.0, 1.0],
# but smaller/larger values are supported as well
stereo_raw = struct.pack('<8f',
                         1.75, -1.75,
                         1.0,  -1.0,
                         0.5,  -0.5,
                         0.25, -0.25)

mono_data = (
    b'RIFF' +
    uint32(46) +  # size

    b'WAVE' +  # file type

    b'fmt ' +
    uint32(16) +  # chunk size
    uint16(1) +  # data type (PCM)
    uint16(1) +  # channels
    uint32(44100) +  # samplerate
    uint32(44100 * 2) +  # bytes per second
    uint16(2) +  # frame size
    uint16(16) +  # bits per sample

    b'data' +
    uint32(5 * 2) +  # chunk size
    mono_raw
)

stereo_data = (
    b'RIFF' +
    uint32(80) +  # size

    b'WAVE' +  # file type

    b'fmt ' +
    uint32(16) +  # chunk size
    uint16(3) +  # data type (float)
    uint16(2) +  # channels
    uint32(44100) +  # samplerate
    uint32(44100 * 2 * 4) +  # bytes per second
    uint16(2 * 4) +  # frame size
    uint16(32) +  # bits per sample

    b'fact' +
    uint32(4) +  # chunk size
    uint32(4) +  # number of frames

    b'data' +
    uint32(8 * 4) +  # chunk size
    stereo_raw
)

with open('stereo.wav', 'wb') as f:
    f.write(stereo_data)

with open('mono.wav', 'wb') as f:
    f.write(mono_data)

with open('mono.raw', 'wb') as f:
    f.write(mono_raw)