This repository has been archived on 2023-10-09. You can view files and clone it, but cannot push or open issues or pull requests.
Files
blender-archive/extern/audaspace/bindings/python/examples/tetris3.py
Joerg Mueller 986267300b Audaspace: Moving audaspace 1.3 into extern.
Deleting the old internal audaspace.

Major changes from there are:
- The whole library was refactored to use C++11.
- Many stability and performance improvements.
- Major Python API refactor:
 - Most requested: Play self generated sounds using numpy arrays.
 - For games: Sound list, random sounds and dynamic music.
 - Writing sounds to files.
 - Sequencing API.
 - Opening sound devices, eg. Jack.
- Ability to choose different OpenAL devices in the user settings.
2017-08-18 08:24:12 +02:00

64 lines
1.7 KiB
Python

#!/usr/bin/python
import aud, math, time
def parseNotes(notes, bpm, basefreq, rate = 44100,
notechars = "XXXCXDXEFXGXAXHcXdXefXgXaXhp"):
pos = 0
fadelength = 60/bpm/10
halfchars = "#b"
durationchars = "2345678"
position = 0
sequence = aud.Sequence()
while pos < len(notes):
char = notes[pos]
mod = None
dur = 1
pos += 1
while pos < len(notes) and notes[pos] not in notechars:
if notes[pos] in halfchars:
mod = notes[pos]
elif notes[pos] in durationchars:
dur = notes[pos]
pos += 1
freq = notechars.find(char)
if mod == '#':
freq += 1
elif mod == 'b':
freq -= 1
freq = math.pow(2, freq/12)*basefreq
length = float(dur)*60/bpm
if char != 'p':
note = aud.Sound.square(freq, rate).fadein(0, fadelength).fadeout(length - fadelength, fadelength)
sequence.add(note, position, position + length, 0)
position += length
return sequence.limit(0, position)
def tetris(bpm = 300, freq = 220, rate = 44100):
notes = "e2Hcd2cH A2Ace2dc H3cd2e2 c2A2A4 pd2fa2gf e3ce2dc H2Hcd2e2 c2A2A2p2"
s11 = parseNotes(notes, bpm, freq, rate)
notes = "e4c4 d4H4 c4A4 G#4p4 e4c4 d4H4 A2c2a4 g#4p4"
s12 = parseNotes(notes, bpm, freq, rate)
notes = "EeEeEeEe AaAaAaAa AbabAbabAbabAbab AaAaAAHC DdDdDdDd CcCcCcCc HhHhHhHh AaAaA2p2"
s21 = parseNotes(notes, bpm, freq, rate, notechars = "AXHCXDXEFXGXaXhcXdXefXgXp")
notes = "aeaeaeae g#dg#dg#dg#d aeaeaeae g#dg#dg#2p2 aeaeaeae g#dg#dg#dg#d aeaeaeae g#dg#dg#2p2"
s22 = parseNotes(notes, bpm, freq/2, rate)
return s11.join(s12).join(s11).volume(0.5).mix(s21.join(s22).join(s21).volume(0.3))
if __name__ == "__main__":
dev = aud.Device()
handle = dev.play(tetris(300, 220, dev.rate))
while handle.status:
time.sleep(0.1)