archaeopteryx 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +21 -0
  3. data/MIDI_FLOW_DIAGRAM.md +61 -0
  4. data/README.md +133 -0
  5. data/bin/arcx +8 -0
  6. data/lib/archaeopteryx/app_pathname.rb +50 -0
  7. data/lib/archaeopteryx/application.rb +183 -0
  8. data/lib/archaeopteryx/application_runner.rb +23 -0
  9. data/lib/archaeopteryx/cli.rb +44 -0
  10. data/lib/archaeopteryx/client.rb +153 -0
  11. data/lib/archaeopteryx/clip.rb +119 -0
  12. data/lib/archaeopteryx/clip_application.rb +7 -0
  13. data/lib/archaeopteryx/commands/application_helper.rb +9 -0
  14. data/lib/archaeopteryx/commands/connections.rb +41 -0
  15. data/lib/archaeopteryx/commands/create_instrument.rb +15 -0
  16. data/lib/archaeopteryx/commands/generate.rb +44 -0
  17. data/lib/archaeopteryx/commands/new.rb +103 -0
  18. data/lib/archaeopteryx/commands/start.rb +13 -0
  19. data/lib/archaeopteryx/commands/status.rb +15 -0
  20. data/lib/archaeopteryx/commands/stop.rb +15 -0
  21. data/lib/archaeopteryx/commands/templates/Gemfile +3 -0
  22. data/lib/archaeopteryx/commands/templates/bin/arcx +6 -0
  23. data/lib/archaeopteryx/commands/templates/clips/clip.rb +3 -0
  24. data/lib/archaeopteryx/commands/templates/config/application.rb +8 -0
  25. data/lib/archaeopteryx/commands/templates/config/connections.yml +3 -0
  26. data/lib/archaeopteryx/commands/templates/instruments/instrument.rb +3 -0
  27. data/lib/archaeopteryx/commands/templates/live.rb +3 -0
  28. data/lib/archaeopteryx/commands.rb +12 -0
  29. data/lib/archaeopteryx/daemonize.rb +73 -0
  30. data/lib/archaeopteryx/duration_calculator.rb +35 -0
  31. data/lib/archaeopteryx/instrument.rb +60 -0
  32. data/lib/archaeopteryx/journey.rb +163 -0
  33. data/lib/archaeopteryx/live.rb +13 -0
  34. data/lib/archaeopteryx/mutator.rb +7 -0
  35. data/lib/archaeopteryx/note.rb +40 -0
  36. data/lib/archaeopteryx/notifier.rb +23 -0
  37. data/lib/archaeopteryx/probability_generator.rb +18 -0
  38. data/lib/archaeopteryx/registrable.rb +28 -0
  39. data/lib/archaeopteryx/rythm_sequence.rb +43 -0
  40. data/lib/archaeopteryx/track.rb +37 -0
  41. data/lib/archaeopteryx/track_notifier.rb +15 -0
  42. data/lib/archaeopteryx/version.rb +3 -0
  43. data/lib/archaeopteryx.rb +11 -0
  44. metadata +213 -0
@@ -0,0 +1,119 @@
1
+ require 'observer'
2
+ require 'musicalism'
3
+ require 'archaeopteryx/notifier'
4
+
5
+ module Archaeopteryx
6
+ class Clip
7
+ include Observable
8
+ include Archaeopteryx::Registrable
9
+
10
+ CHORD_ON_FIRST_STEP = ([1] + [0] * 15).freeze
11
+ MISSING_KEY_MESSAGE = "set the clip key first, e.g. key 'C major'".freeze
12
+
13
+ attr_reader :rythm_sequences, :name, :notifier
14
+
15
+ def initialize(name)
16
+ self.name = name
17
+ forget_definition
18
+ end
19
+
20
+ def key(name = nil)
21
+ return @key if name.nil?
22
+
23
+ @key = Musicalism::Key.coerce(name)
24
+ end
25
+
26
+ def note(pitch_or_degree, beat_resolution, probabilities, velocity: nil)
27
+ rythm_sequences << RythmSequence.new(pitch_name(pitch_or_degree), beat_resolution, probabilities, velocity)
28
+ end
29
+
30
+ def chord(chord_or_numeral, beat_resolution, probabilities, inversion: 0, velocity: nil)
31
+ chord = musicalism_chord(chord_or_numeral, inversion)
32
+ rythm_sequences << RythmSequence.new(pitch_names(chord), beat_resolution, probabilities, velocity)
33
+ end
34
+
35
+ def modulate(to:, via: nil, beat_resolution: 16, rhythm: CHORD_ON_FIRST_STEP, voice_leading: true)
36
+ self.bars = current_key.modulate_to(to, via: via, voice_leading: voice_leading).map do |chord|
37
+ [RythmSequence.new(pitch_names(chord), beat_resolution, rhythm)]
38
+ end
39
+ end
40
+
41
+ def follow(journey_name, &block)
42
+ self.followed_journey = [journey_name, block]
43
+ end
44
+
45
+ def rythm_sequences_for(bar_offset, song_bar = nil)
46
+ fixed_sequences = bars.empty? ? rythm_sequences : rythm_sequences + bars[bar_offset % bars.size]
47
+ fixed_sequences + journey_sequences_for(song_bar)
48
+ end
49
+
50
+ def load(&block)
51
+ forget_definition
52
+ changed
53
+ instance_eval(&block)
54
+ notify_observers(self)
55
+ end
56
+
57
+ private
58
+
59
+ attr_accessor :generator, :block, :bars, :followed_journey
60
+ attr_writer :rythm_sequences, :name, :notifier
61
+
62
+ def forget_definition
63
+ self.rythm_sequences = []
64
+ self.bars = []
65
+ self.followed_journey = nil
66
+ @key = nil
67
+ end
68
+
69
+ def journey_sequences_for(song_bar)
70
+ return [] unless followed_journey && song_bar
71
+
72
+ journey_name, block = followed_journey
73
+ bar = journey(journey_name).bar(song_bar)
74
+ sequences_played_in(bar.key) { instance_exec(bar.key, bar, &block) }
75
+ end
76
+
77
+ def sequences_played_in(journey_key)
78
+ fixed_sequences = rythm_sequences
79
+ fixed_key = @key
80
+ self.rythm_sequences = []
81
+ @key = journey_key
82
+ yield
83
+ rythm_sequences
84
+ ensure
85
+ self.rythm_sequences = fixed_sequences
86
+ @key = fixed_key
87
+ end
88
+
89
+ def journey(name)
90
+ Journey[name] || raise(ArgumentError, "Journey #{name} not found.")
91
+ end
92
+
93
+ def current_key
94
+ key || raise(ArgumentError, MISSING_KEY_MESSAGE)
95
+ end
96
+
97
+ def pitch_name(pitch_or_degree)
98
+ pitch_or_degree.is_a?(Integer) ? note_name(current_key.note(pitch_or_degree)) : pitch_or_degree
99
+ end
100
+
101
+ def musicalism_chord(chord_or_numeral, inversion)
102
+ return chord_or_numeral.inverse(inversion) if chord_or_numeral.is_a?(Musicalism::Chord)
103
+
104
+ current_key.chord(chord_or_numeral, inversion: inversion)
105
+ end
106
+
107
+ def pitch_names(chord)
108
+ chord.notes.map { |note| note_name(note) }
109
+ end
110
+
111
+ def note_name(note)
112
+ "#{note.pitch}#{note.octave}"
113
+ end
114
+
115
+ def log(msg)
116
+ Archaeopteryx::Application.config.logger.info msg
117
+ end
118
+ end
119
+ end
@@ -0,0 +1,7 @@
1
+ require 'application'
2
+ module Archaeopteryx
3
+ class ClipApplication < Application
4
+ def run
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,9 @@
1
+ module Archaeopteryx
2
+ module Commands
3
+ module ApplicationHelper
4
+ def application
5
+ @application ||= Kernel.const_get(File.basename(Dir.pwd).split(/_|-/).compact.map(&:capitalize).join).new
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,41 @@
1
+ require 'archaeopteryx/commands/application_helper'
2
+
3
+ module Archaeopteryx
4
+ module Commands
5
+ class Connections < Thor::Group
6
+ include ApplicationHelper
7
+ TABLE_HEADER = ['DEVICE NAME', 'PORT', 'MODEL', 'UID', 'STATUS', 'TRANSMITS MTC', 'RECEIVES MTC'].freeze
8
+
9
+ def display_available_connections
10
+ midi_ins = [TABLE_HEADER]
11
+ midi_outs = [TABLE_HEADER]
12
+ RCoreMidi::Device.all.each do |device|
13
+ if device.entities_with_midi_in.any?
14
+ device.entities_with_midi_in.each do |entity|
15
+ entity.sources.each do |source|
16
+ midi_ins << [device.name, entity.name, device.model, source.uid, device.offline ? 'offline' : 'online',
17
+ device.transmits_mtc, device.receives_mtc]
18
+ end
19
+ end
20
+ end
21
+
22
+ next unless device.entities_with_midi_out.any?
23
+
24
+ device.entities_with_midi_out.each do |entity|
25
+ entity.destinations.each do |destination|
26
+ midi_outs << [device.name, entity.name, device.model, destination.uid,
27
+ device.offline ? 'offline' : 'online']
28
+ end
29
+ end
30
+ end
31
+
32
+ say('Available MIDI Inputs:', Thor::Shell::Color::GREEN)
33
+ print_table midi_ins
34
+ say("\n")
35
+ say('Available MIDI Outputs:', Thor::Shell::Color::GREEN)
36
+ print_table midi_outs
37
+ say("\n")
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,15 @@
1
+ module Archaeopteryx
2
+ module Commands
3
+
4
+
5
+ class CreateInstrument < Clamp::Command
6
+
7
+ parameter 'NAME', 'Application Name', attribute_name: :instrument_name
8
+
9
+ def execute
10
+ puts "instrument?"
11
+ end
12
+ end
13
+
14
+ end
15
+ end
@@ -0,0 +1,44 @@
1
+ module Archaeopteryx
2
+ module Commands
3
+ class Generate < Thor
4
+ include Thor::Actions
5
+
6
+ CLIP_TEMPLATE = 'clip.rb'
7
+ INSTRUMENT_TEMPLATE = 'instrument.rb'
8
+
9
+ def self.source_root
10
+ File.expand_path('../templates', __FILE__)
11
+ end
12
+
13
+ desc 'clip', 'Generate boilerplate code to create a clip'
14
+ long_desc <<-LONG_DESC
15
+ This generators assists you in creating a new clip file
16
+
17
+ Archaeopteryx::Clip defines rythmic and/or melodic patterns.
18
+ They can then be loaded into one or multiple instruments
19
+ LONG_DESC
20
+
21
+ def clip(name)
22
+ assign_name!(name)
23
+ template "#{CLI::CLIPS_DIR}/#{CLIP_TEMPLATE}", "#{CLI::CLIPS_DIR}/#{name}.rb"
24
+ end
25
+
26
+ desc 'instrument', 'Generate boilerplate code to create an instrument'
27
+ method_options midi_channel: :numeric
28
+ def instrument
29
+ template "#{Commands::New::CLIPS_DIR}/#{CLIP_TEMPLATE}", "#{CLI::INSTRUMENT_DIR}/#{name}.rb"
30
+ end
31
+
32
+
33
+ private
34
+
35
+ def name
36
+ @name
37
+ end
38
+
39
+ def assign_name!(name)
40
+ @name = name
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,103 @@
1
+ require 'archaeopteryx/commands/application_helper'
2
+
3
+ module Archaeopteryx
4
+ module Commands
5
+ class New < Thor::Group
6
+ include Thor::Actions
7
+ include ApplicationHelper
8
+ TEMPLATES_DIR = File.expand_path(File.join(File.dirname(__FILE__), 'templates')).freeze
9
+
10
+ APPLICATION = 'application.rb'.freeze
11
+ CONNECTIONS = 'connections.yml'.freeze
12
+ ARCX = 'arcx'.freeze
13
+ LIVE = 'live.rb'.freeze
14
+ CLIP = 'clip.rb'
15
+ INSTRUMENT = 'instrument.rb'
16
+ GEMFILE = 'Gemfile'.freeze
17
+
18
+ def self.source_root
19
+ File.expand_path('templates', File.dirname(__FILE__))
20
+ end
21
+
22
+ argument :name, type: :string
23
+
24
+ def create_dirs
25
+ self.destination_root = "#{name}"
26
+ in_root do
27
+ Archaeopteryx::CLI::DIRS.each do |dir|
28
+ empty_directory dir
29
+ end
30
+ end
31
+ end
32
+
33
+ def create_gemfile
34
+ template GEMFILE
35
+ end
36
+
37
+ def create_clip
38
+ template "clips/#{CLIP}"
39
+ end
40
+
41
+ def create_instrument
42
+ template "instruments/#{INSTRUMENT}"
43
+ end
44
+
45
+ def create_application
46
+ template "config/#{APPLICATION}"
47
+ end
48
+
49
+ def create_connections
50
+ template "config/#{CONNECTIONS}"
51
+ end
52
+
53
+ def bin
54
+ chmod 'bin', 0o755 & ~File.umask, verbose: false
55
+ end
56
+
57
+ def create_arcx_bin
58
+ template "bin/#{ARCX}"
59
+ chmod "#{destination_root}/bin/#{ARCX}", 0o755 & ~File.umask, verbose: true
60
+ end
61
+
62
+ def create_live
63
+ template "#{LIVE}"
64
+ end
65
+
66
+ private
67
+
68
+ attr_accessor :application_name
69
+
70
+ def devices
71
+ @devices ||= RCoreMidi::Device.all
72
+ end
73
+
74
+ def iac_driver
75
+ devices.detect { |device| device.name == 'IAC Driver' }
76
+ end
77
+
78
+ def iac_driver_bus_one
79
+ @iac_driver_bus_one ||= iac_driver.entities.detect { |entity| entity.name == 'Bus 1' }
80
+ end
81
+
82
+ def midi_in
83
+ iac_driver_bus_one.endpoints.detect { |entity| entity.is_a?(RCoreMidi::Source) }
84
+ end
85
+
86
+ def midi_out
87
+ iac_driver_bus_one.endpoints.detect { |entity| entity.is_a?(RCoreMidi::Destination) }
88
+ end
89
+
90
+ def midi_channel
91
+ 0
92
+ end
93
+
94
+ def app_const
95
+ File.basename(destination_root).split(/_|-/).map(&:capitalize).join
96
+ end
97
+
98
+ def client_name
99
+ 'RCoreMIDI'
100
+ end
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,13 @@
1
+ module Archaeopteryx
2
+ module Commands
3
+ class Start < Thor::Group
4
+ include ApplicationHelper
5
+
6
+ class_option :daemon, type: :boolean, default: false, aliases: %w[-d], banner: 'Start process in the background'
7
+ def start
8
+ application.run(daemon_mode: options[:daemon])
9
+ exit(0)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,15 @@
1
+ module Archaeopteryx
2
+ module Commands
3
+ class Status < Thor::Group
4
+
5
+ def application
6
+ @application || Archaeopteryx::Application.new
7
+ end
8
+
9
+ def log_application_status
10
+ application.log_status
11
+ exit(0)
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ module Archaeopteryx
2
+ module Commands
3
+ class Stop < Thor::Group
4
+
5
+ def application
6
+ @application || Archaeopteryx::Application.new
7
+ end
8
+
9
+ def stop
10
+ application.stop
11
+ exit(0)
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'archaeopteryx', '~> <%= Archaeopteryx::VERSION %>'
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env <%= File.basename(Thor::Util.ruby_command) %>
2
+ APP_PATH = File.expand_path('..', __dir__)
3
+ require File.expand_path('config/application', APP_PATH)
4
+ require 'archaeopteryx/cli'
5
+
6
+ Archaeopteryx::CLI.start(ARGV)
@@ -0,0 +1,3 @@
1
+ Archaeopteryx::Clip.register :<%= name %> do
2
+ #note 'E5', 16, [1, 0, 0, 0] * 4
3
+ end
@@ -0,0 +1,8 @@
1
+ require 'bundler/setup'
2
+
3
+ Bundler.require :default
4
+
5
+ class <%= app_const %> < Archaeopteryx::Application
6
+ load_clips!
7
+ load_instruments!
8
+ end
@@ -0,0 +1,3 @@
1
+ - name: <%= client_name %>
2
+ midi_in: <%= midi_in.uid %>
3
+ midi_out: <%= midi_out.uid %>
@@ -0,0 +1,3 @@
1
+ Archaeopteryx::Instrument.register :<%= name %>, <%= midi_channel %> do
2
+ #note 'E5', [1, 0, 0, 0] * 4
3
+ end
@@ -0,0 +1,3 @@
1
+ require ::File.expand_path('../config/application', __FILE__)
2
+
3
+ <%= app_const %>.new.run
@@ -0,0 +1,12 @@
1
+ require 'thor'
2
+ require 'archaeopteryx/commands/generate'
3
+ require 'archaeopteryx/commands/new'
4
+ require 'archaeopteryx/commands/connections'
5
+ require 'archaeopteryx/commands/start'
6
+ require 'archaeopteryx/commands/stop'
7
+ require 'archaeopteryx/commands/status'
8
+
9
+ module Archaeopteryx
10
+ module Commands
11
+ end
12
+ end
@@ -0,0 +1,73 @@
1
+ module Archaeopteryx
2
+ module Daemonize
3
+
4
+ def daemonize
5
+ exit if fork
6
+ Process.setsid
7
+ exit if fork
8
+ yield
9
+ set_process_title
10
+ Dir.chdir "/"
11
+ end
12
+
13
+ def write_pid
14
+ unless root.pid_file.exist?
15
+ begin
16
+ root.pid_file.expand_path.open(::File::CREAT | ::File::EXCL | ::File::WRONLY){|f| f.write("#{Process.pid}") }
17
+ at_exit do
18
+ if root.pid_file.expand_path.exist?
19
+ root.pid_file.expand_path.delete
20
+ end
21
+ end
22
+ rescue Errno::EEXIST
23
+ check_pid
24
+ retry
25
+ end
26
+ end
27
+ end
28
+
29
+ def redirect_output
30
+ FileUtils.touch root.log_file
31
+ root.log_file.chmod(0644)
32
+ $stderr.reopen(root.log_file, 'a')
33
+ $stdout.reopen($stderr)
34
+ $stdout.sync = $stderr.sync = true
35
+ end
36
+
37
+ def set_process_title
38
+ Process.setproctitle('arcx live:%d - in %s' % [config.bpm, root])
39
+ end
40
+
41
+ def check_pid
42
+ if root.pid_file.exist?
43
+ case pid_status
44
+ when :running, :not_owned
45
+ config.logger.info "A server is already running. Check #{root.pid_file}"
46
+ exit(1)
47
+ when :dead
48
+ root.pid_file.delete
49
+ end
50
+ end
51
+ end
52
+
53
+ def pid_status
54
+ return :exited unless root.pid_file.exist?
55
+ pid = root.pid_file.read.to_i
56
+ return :dead if pid == 0
57
+ Process.kill(0, pid) # check process status
58
+ :running
59
+ rescue Errno::ESRCH
60
+ :dead
61
+ rescue Errno::EPERM
62
+ :not_owned
63
+ end
64
+
65
+ def trap_signals
66
+ trap 'QUIT', Proc.new {
67
+ listeners.stop
68
+ client.dispose
69
+ exit(0)
70
+ }
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,35 @@
1
+ module Archaeopteryx
2
+ class DurationCalculator
3
+
4
+ # 960 ppqn is logic pro's default
5
+ def initialize(bpm, ppqn = 96)
6
+ self.bpm = bpm
7
+ self.ppqn = ppqn
8
+ compute_ppqn_delta_time
9
+ end
10
+
11
+ # quarter note = 24 (in logic pro)
12
+ # eigthth note = 480 ticks
13
+ # seixteenth note = 240 ticks
14
+ def timestamps_for(note_index, resolution = 16)
15
+ # A probabilities array of length 16 means
16
+ # our current resolution is an eigthth note
17
+ # 48 ticks is our current resolution
18
+ on = (mpt * 6 * note_index).round
19
+ [on, (on + default_note_off_offset).round]
20
+ end
21
+
22
+ private
23
+ attr_accessor :bpm, :mpt, :ppqn
24
+
25
+ def compute_ppqn_delta_time
26
+ @mpt ||= (60_000_000_000 / bpm.to_f) / 24.to_f
27
+ end
28
+
29
+ # default note offset is a seixteenth
30
+ def default_note_off_offset
31
+ 6 * mpt
32
+ end
33
+
34
+ end
35
+ end
@@ -0,0 +1,60 @@
1
+ require 'archaeopteryx/track'
2
+ require 'observer'
3
+ module Archaeopteryx
4
+ class Instrument
5
+ include Archaeopteryx::Registrable
6
+
7
+ attr_reader :name
8
+
9
+ def initialize(name, channel, output: nil, &block)
10
+ self.name = name
11
+ self.channel = channel
12
+ self.block = block
13
+ self.destination = RCoreMidi::Destination.named(output) if output
14
+ end
15
+
16
+ def generate_bar(bar)
17
+ track.generate(bar).each { |note| note.destination = destination }
18
+ end
19
+
20
+ def log(msg)
21
+ Archaeopteryx::Application.config.logger.info msg
22
+ end
23
+
24
+ def play(bar, clip_name, enable_probability = false)
25
+ clp = clip(clip_name)
26
+ clp.add_observer(self)
27
+ if bar.is_a? Range
28
+ bar.each do |bar_index|
29
+ track.play(bar_index, clp, enable_probability, bar_index - bar.min)
30
+ end
31
+ else
32
+ track.play(bar, clp, enable_probability)
33
+ end
34
+ end
35
+
36
+ def load
37
+ instance_eval(&block)
38
+ end
39
+
40
+ def update(_reloaded_clip)
41
+ track.reset
42
+ load
43
+ end
44
+
45
+ private
46
+
47
+ attr_accessor :channel, :file, :block, :destination
48
+ attr_writer :tracks, :name
49
+
50
+ def track
51
+ @track ||= Track.new(channel)
52
+ end
53
+
54
+ def clip(name)
55
+ raise ArgumentError, "Clip #{name} not found." unless c = Clip[name]
56
+
57
+ c
58
+ end
59
+ end
60
+ end