# Chip Music Demo 0.0.1 # Copyright 2001 Lja MultiMedia # May be used under the GNU GPL """template.py : Python file header template""" import struct, math, audioop def tone(freq, vol, len): snd = "" for t in range(len): snd += struct.pack("b", int(math.sin(t * freq) * vol)) return snd dsp = file('/dev/dsp', 'wb') def play(score, beat=1000): for f in score: dsp.write(tone(f, 50, beat)) dsp.flush() def play2(score, beat=1000): for f in score: dsp.write(multi(f, 50, beat)) dsp.flush() def multi(f, v, l): a = tone(f * 0.8, v / 3.0, l) b = tone(f, v / 3.0, l) c = tone(f * 1.2, v / 3.0, l) d = audioop.add(a, b, 1) f = audioop.add(c, d, 1) return f def demo(): s = 1,2,3,4,5,4,5,6,7,5,3,1,2,3,2,3,4,6,8,7,6,5,4,5,4,3,2,3,2 s2 = [] for i in s: s2.append(i * 25) play(s2) def demo1(): s = 1,2,3,4,5,4,5,6,7,5,3,1,2,3,2,3,4,6,8,7,6,5,4,5,4,3,2,3,2 s2 = [] for i in s: s2.append(i * 25) play2(s2) def demo2(): a = tone(400, 20, 3000) b = tone(500, 30, 3000) c = tone(600, 20, 3000) d = audioop.add(a, b, 1) f = audioop.add(c, d, 1) dsp.write(f) dsp.flush() if __name__ == "__main__": demo1()