Initial attempt at timestretching implementation.
[calfbox.git] / jack_api_example.py
blob7fca9dc97fb76c151144bc09e6e8390096b21e39
1 from calfbox import cbox
2 import time
4 def cmd_dumper(cmd, fb, args):
5 print ("%s(%s)" % (cmd, ",".join(list(map(repr,args)))))
7 cbox.init_engine()
9 cbox.Config.add_section("drumpattern:pat1", """
10 title=Straight - Verse
11 beats=4
12 track1=bd
13 track2=sd
14 track3=hh
15 track4=ho
16 bd_note=c1
17 sd_note=d1
18 hh_note=f#1
19 ho_note=a#1
20 bd_trigger=9... .... 9.6. ....
21 sd_trigger=.... 9..5 .2.. 9...
22 hh_trigger=9353 7353 7353 73.3
23 ho_trigger=.... .... .... ..3.
24 """)
26 cbox.Config.set("io", "use_usb", 0)
27 cbox.start_audio(cmd_dumper)
29 global Document
30 Document = cbox.Document
32 status = cbox.JackIO.status()
33 client_name = status.client_name
34 print ("Client name: %s" % client_name)
35 print ("Audio inputs: %d, outputs: %d" % (status.audio_inputs, status.audio_outputs))
36 print ("Sample rate: %d frames/sec" % (status.sample_rate))
37 print ("JACK period: %d frames" % (status.buffer_size))
38 uuid = cbox.JackIO.create_midi_output('drums')
39 cbox.JackIO.autoconnect_midi_output(uuid, '*alsa_pcm:.*')
40 cbox.JackIO.rename_midi_output(uuid, 'kettles')
42 uuid_in = cbox.JackIO.create_midi_input('extra')
43 cbox.JackIO.autoconnect_midi_input(uuid_in, '*alsa_pcm:.*')
44 cbox.JackIO.rename_midi_input(uuid_in, 'extra_port')
46 uuid2 = cbox.JackIO.create_midi_output('violins')
48 status = cbox.JackIO.status()
49 print ("Before deleting, MIDI outputs: %s" % status.midi_output)
51 cbox.JackIO.delete_midi_output(uuid2)
52 status = cbox.JackIO.status()
53 print ("After deleting, MIDI outputs: %s" % status.midi_output)
55 print ("Physical audio inputs: %s" % (",".join(cbox.JackIO.get_ports(".*", cbox.JackIO.AUDIO_TYPE, cbox.JackIO.PORT_IS_SOURCE | cbox.JackIO.PORT_IS_PHYSICAL))))
56 print ("Physical audio outputs: %s" % (",".join(cbox.JackIO.get_ports(".*", cbox.JackIO.AUDIO_TYPE, cbox.JackIO.PORT_IS_SINK | cbox.JackIO.PORT_IS_PHYSICAL))))
57 print ("Physical MIDI inputs: %s" % (",".join(cbox.JackIO.get_ports(".*", cbox.JackIO.MIDI_TYPE, cbox.JackIO.PORT_IS_SOURCE | cbox.JackIO.PORT_IS_PHYSICAL))))
58 print ("Physical MIDI outputs: %s" % (",".join(cbox.JackIO.get_ports(".*", cbox.JackIO.MIDI_TYPE, cbox.JackIO.PORT_IS_SINK | cbox.JackIO.PORT_IS_PHYSICAL))))
60 inputs = cbox.JackIO.get_ports(".*", cbox.JackIO.AUDIO_TYPE, cbox.JackIO.PORT_IS_SOURCE | cbox.JackIO.PORT_IS_PHYSICAL)
61 outputs = cbox.JackIO.get_ports(".*", cbox.JackIO.AUDIO_TYPE, cbox.JackIO.PORT_IS_SINK | cbox.JackIO.PORT_IS_PHYSICAL)
62 cbox.JackIO.port_connect(inputs[0], outputs[0])
63 cbox.JackIO.port_connect(inputs[1], outputs[1])
64 assert "cbox:in_3" in cbox.JackIO.get_connected_ports(inputs[0])
66 scene = Document.get_scene()
67 scene.clear()
68 instrument = scene.add_new_instrument_layer("test_sampler", "sampler").get_instrument()
69 pgm_no = instrument.engine.get_unused_program()
70 pgm = instrument.engine.load_patch_from_file(pgm_no, 'synthbass.sfz', 'SynthBass')
71 instrument.engine.set_patch(1, pgm_no)
72 instrument.engine.set_patch(10, pgm_no)
74 song = Document.get_song()
75 track = song.add_track()
76 track.set_external_output(uuid)
77 print ("Track outputs to: %s:%s" % (client_name, track.status().external_output))
78 pattern = song.load_drum_pattern("pat1")
79 track.add_clip(0, 0, pattern.status().loop_end, pattern)
80 song.set_loop(0, pattern.status().loop_end)
81 song.update_playback()
82 cbox.Transport.play()
84 print("Ready!")
86 while True:
87 events = cbox.get_new_events()
88 if events:
89 print (events)
90 time.sleep(0.05)