User:Lucia Dossin/Protyping/Assignment 3: Difference between revisions

From XPUB & Lens-Based wiki
(Created page with "Python Generated Sound File <source> import wave, struct filename = "paaaaaaapa.wav" nframes=0 nchannels=1 sampwidth=2 # in bytes so 2=16bit, 1=8bit framerate=44100 bufsize...")
 
No edit summary
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
Python Generated Sound File
Python Generated Sound Files


<source>
<source lang="python">
import wave, struct
import wave, struct
   
   
Line 38: Line 38:
w.close()
w.close()
</source>
</source>
Output:
Sound [[File:paaaaaaapa.ogg]]
Sound Graphic [[File:paaaaaaapa.png]]
<source lang="python">
import wave, struct
filename = "oeoeoe.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)
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 k in range(300):
    for v in range(300-k):
        data += struct.pack('h', int(0.5 * max_amplitude))
    for v in range(k):
        data += struct.pack('h', int(-0.5 * max_amplitude))
    for s in range(900):
        data += struct.pack('h', int(0.5 * max_amplitude))
    for s in range(900):
        data += struct.pack('h', int(-0.5 * max_amplitude))
w.writeframesraw(data)
#print(data)
w.close()
</source>
Output:
[[File:oeoeoe.ogg]]
<source lang="python">
import wave, struct
filename = "truck.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)
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 k in range(300):
    for v in range(300-k):
        data += struct.pack('h', int(0.5 * max_amplitude))
    for v in range(k):
        data += struct.pack('h', int(-0.5 * max_amplitude))
w.writeframesraw(data)
w.close()
</source>
Output:
Sound [[File:truck.ogg]]
Sound Graphic [[File:truck.png]]

Latest revision as of 12:32, 14 October 2013

Python Generated Sound Files

import wave, struct
 
filename = "paaaaaaapa.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)
 
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(300):
    for x in range(i):
        data += struct.pack('h', int(0.5 * max_amplitude))
    for x in range(i):
        data += struct.pack('h', int(-0.5 * max_amplitude))
for i in range(100):
    for x in range(i):
        data += struct.pack('h', int(0.5 * max_amplitude))
    for x in range(i):
        data += struct.pack('h', int(-0.5 * max_amplitude))
 
w.writeframesraw(data)
 
w.close()

Output: Sound File:Paaaaaaapa.ogg Sound Graphic Paaaaaaapa.png

import wave, struct
 
filename = "oeoeoe.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)
 
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 k in range(300):
    for v in range(300-k):
        data += struct.pack('h', int(0.5 * max_amplitude))
    for v in range(k):
        data += struct.pack('h', int(-0.5 * max_amplitude))
    for s in range(900):
        data += struct.pack('h', int(0.5 * max_amplitude))
    for s in range(900):
        data += struct.pack('h', int(-0.5 * max_amplitude))
 
w.writeframesraw(data)
#print(data)
 
w.close()

Output: File:Oeoeoe.ogg

import wave, struct
 
filename = "truck.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)
 
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 k in range(300):
    for v in range(300-k):
        data += struct.pack('h', int(0.5 * max_amplitude))
    for v in range(k):
        data += struct.pack('h', int(-0.5 * max_amplitude))
 
w.writeframesraw(data)
 
w.close()

Output: Sound File:Truck.ogg Sound Graphic Truck.png