Generating audio with python: Difference between revisions

From XPUB & Lens-Based wiki
(Created page with "<source lang="python"> import wave, struct filename = "output.wav" nframes=0 nchannels=1 sampwidth=2 # in bytes so 2=16bit, 1=8bit framerate=44100 bufsize=2048 w = wave.open...")
 
 
(6 intermediate revisions by the same user not shown)
Line 1: Line 1:
=== Hello Wave (Pong hit) ===
<source lang="python">
<source lang="python">
import wave, struct
import wave, struct
Line 37: Line 39:
w.close()
w.close()
</source>
</source>
[[File:Genaudio.01.ogg]]
=== Alien landing ===
<source lang="python">
#!/usr/bin/env python
#-*- coding:utf-8 -*-
import wave, struct
filename = "output.wav"
nframes=0
nchannels=2
sampwidth=2 # in bytes so 2=16bit, 1=8bit
framerate=44100
bufsize=2048
w = wave.open(filename, 'w')
w.setparams((nchannels, sampwidth, framerate, nframes, 'NONE', 'not compressed'))
max_amplitude = float(int((2 ** (sampwidth * 8)) / 2) - 1)
freq = 880
cycle = framerate / freq
for _ in range(100):
    data = ''
    cycle = framerate / freq
    for _ in range(10):
        for _ in range(cycle/2):
            data += struct.pack('h', int(0.5 * max_amplitude))
            data += struct.pack('h', 0)
        for _ in range(cycle/2):
            data += struct.pack('h', int(-0.5 * max_amplitude))
            data += struct.pack('h', 0)
    for _ in range(10):
        for _ in range(cycle/2):
            data += struct.pack('h', 0)
            data += struct.pack('h', int(0.5 * max_amplitude))
        for _ in range(cycle/2):
            data += struct.pack('h', 0)
            data += struct.pack('h', int(-0.5 * max_amplitude))
    w.writeframesraw(data)
    freq -= 10
w.close()
</source>
[[File:Genaudio.02.ogg]]
== Resources ==
* http://zacharydenton.com/generate-audio-with-python/

Latest revision as of 16:50, 8 October 2013

Hello Wave (Pong hit)

import wave, struct

filename = "output.wav"
nframes=0
nchannels=1
sampwidth=2 # in bytes so 2=16bit, 1=8bit
framerate=44100
bufsize=2048

w = wave.open(filename, 'w')
w.setparams((nchannels, sampwidth, framerate, nframes, 'NONE', 'not compressed'))

max_amplitude = float(int((2 ** (sampwidth * 8)) / 2) - 1)

# split the samples into chunks (to reduce memory consumption and improve performance)
#for chunk in grouper(bufsize, samples):
#    frames = ''.join(''.join(struct.pack('h', int(max_amplitude * sample)) for sample in channels) for channels in chunk if channels is not None)
#    w.writeframesraw(frames)

freq = 440
# this means that FREQ times a second, we need to complete a cycle
# there are FRAMERATE samples per second
# so FRAMERATE / FREQ = CYCLE LENGTH
cycle = framerate / freq


data = ''
for i in range(10):
    for x in range(100):
        data += struct.pack('h', int(0.5 * max_amplitude))
    for x in range(100):
        data += struct.pack('h', int(-0.5 * max_amplitude))

w.writeframesraw(data)

w.close()

File:Genaudio.01.ogg

Alien landing

#!/usr/bin/env python
#-*- coding:utf-8 -*-

import wave, struct

filename = "output.wav"
nframes=0
nchannels=2
sampwidth=2 # in bytes so 2=16bit, 1=8bit
framerate=44100
bufsize=2048

w = wave.open(filename, 'w')
w.setparams((nchannels, sampwidth, framerate, nframes, 'NONE', 'not compressed'))
max_amplitude = float(int((2 ** (sampwidth * 8)) / 2) - 1)

freq = 880
cycle = framerate / freq

for _ in range(100):
    data = ''
    cycle = framerate / freq
    for _ in range(10):
        for _ in range(cycle/2):
            data += struct.pack('h', int(0.5 * max_amplitude))
            data += struct.pack('h', 0)
        for _ in range(cycle/2):
            data += struct.pack('h', int(-0.5 * max_amplitude))
            data += struct.pack('h', 0)

    for _ in range(10):
        for _ in range(cycle/2):
            data += struct.pack('h', 0)
            data += struct.pack('h', int(0.5 * max_amplitude))
        for _ in range(cycle/2):
            data += struct.pack('h', 0)
            data += struct.pack('h', int(-0.5 * max_amplitude))
    w.writeframesraw(data)

    freq -= 10

w.close()

File:Genaudio.02.ogg

Resources