Skip to content
Browse files

MIDI makes more sense with unit values.

  • Loading branch information...
1 parent d5d44c2 commit 9c222db891cd875d12e63e7921ce3b8ba240d957 @sixohsix committed May 17, 2012
Showing with 37 additions and 18 deletions.
  1. +3 −3 musi/base.py
  2. +11 −12 musi/examples/sequences.py
  3. +18 −2 musi/midi.py
  4. +5 −1 musi/player.py
View
6 musi/base.py
@@ -59,11 +59,11 @@ def no_zero_bpm(now):
self.bpm_f = no_zero_bpm
self.bpb = bpb
- def song_beat(self, now):
+ def song_beats(self, now):
return int(now / 60.0 * self.bpm_f(now))
- def song_bar(self, now):
- return self.song_beat(now) / self.bpb
+ def song_bars(self, now):
+ return self.song_beats(now) / self.bpb
def Duration(self, bars=0, beats=0):
nbeats = float((bars * self.bpb) + beats)
View
23 musi/examples/sequences.py
@@ -6,8 +6,10 @@
dest = MIDIDestination("sequence control")
midi_input = midi.Input(dest.recv)
-tempo_control = math.LinScale(C(60.0), C(2.0), midi.ControllerValue(midi_input, 2, 1))
-filter_control = midi.ControllerValue(midi_input, 2, 2)
+tempo_control = math.LinScale(
+ C(30.0), C(200.0),
+ midi.UnitControllerValue(midi_input, 2, 1, 0.5))
+filter_control = midi.UnitControllerValue(midi_input, 2, 2)
tempo = Tempo(tempo_control, 4)
@@ -26,18 +28,15 @@
midi.NoteSequencer(1, [36], beat_f),
midi.NoteSequencer(1, [None, 38], beat_f),
)
-bass = midi.Mix(
- Eval(waves.Sequencer(
- [midi.NoteSequencer(0, [48], beat_f),
- C(())],
- tempo.Duration(bars=1),
- )
- )
- )
-filter_ = midi.ControllerChange(
+bassline = [48, 48, None, None] * 4 + [51, 51, None, None] * 4
+bass = If(math.LessThan(C(8.0), tempo.song_bars),
+ midi.NoteSequencer(0, bassline, beat_f),
+ midi.nothing)
+
+filter_ = midi.UnitControllerChange(
0, 52,
- math.LinScale(C(0.0), C(0.5), filter_control))
+ math.Mul(C(0.5), filter_control))
song = midi.Mix(
notes,
View
20 musi/midi.py
@@ -1,8 +1,10 @@
from .base import C
+nothing = C(())
+
def mclamp(val):
from .math import clamp
- return clamp(0, 127, val)
+ return clamp(0, 127, int(val))
def all_notes_off(now=None):
@@ -24,6 +26,13 @@ def cc(now):
return cc
+def UnitControllerChange(channel, ccid, val_f):
+ from . import math, C
+ return ControllerChange(
+ channel, ccid,
+ math.Mul(C(127.0), val_f))
+
+
def Mix(*mfs):
from itertools import chain
def midi_mix(now):
@@ -105,7 +114,7 @@ def __init__(self, midi_input, channel, ccid, initial_val=0):
self.action = 0xb0 | clamp(0, 15, int(channel))
self.ccid = ccid
self.midi_input = midi_input
- self.ccval = initial_val
+ self.ccval = mclamp(initial_val)
def __call__(self, now):
inp = self.midi_input(now)
for i, byte in enumerate(inp):
@@ -116,4 +125,11 @@ def __call__(self, now):
return self.ccval
+def UnitControllerValue(midi_input, channel, ccid, initial_val=0.0):
+ from . import math, C
+ return math.Mul(
+ C(1.0 / 127),
+ ControllerValue(midi_input, channel, ccid, initial_val * 127.0))
+
+
del C
View
6 musi/player.py
@@ -1,3 +1,6 @@
+from __future__ import print_function
+
+
def play(song, send_midi=None, loop_rate_hz=200):
from time import time, sleep
from heapq import heappush, heappop
@@ -38,5 +41,6 @@ def heappush_all(heap, seq):
def countdown():
from time import sleep
for i in range(4, 0, -1):
- print i
+ print(i)
sleep(1)
+ print("go")

0 comments on commit 9c222db

Please sign in to comment.
Something went wrong with that request. Please try again.