Class: App

Inherits:
Object show all
Defined in:
lib/audio.rb,
lib/music_coder.rb

Overview

require ‘debugger’ The top level of the program. This static class contains static methods and static attributes. Gem site: rubygems.org/gems/music_coder

Constant Summary collapse

EXT =
".aiff"
TMP_DIR =
"tmp/"
OUT_SND_DIR =
'sound/'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.audio_file_settingsObject

Returns the value of attribute audio_file_settings.



72
73
74
# File 'lib/music_coder.rb', line 72

def audio_file_settings
  @audio_file_settings
end

.checksObject

perf testing



76
77
78
# File 'lib/music_coder.rb', line 76

def checks
  @checks
end

.doneObject

how many hits are done, how many are todo



74
75
76
# File 'lib/music_coder.rb', line 74

def done
  @done
end

.fileoptionsObject

Returns the value of attribute fileoptions.



70
71
72
# File 'lib/music_coder.rb', line 70

def fileoptions
  @fileoptions
end

.infileObject

file containing input (without .rb extension)



63
64
65
# File 'lib/music_coder.rb', line 63

def infile
  @infile
end

.lastgenObject

Time of the last output computation



65
66
67
# File 'lib/music_coder.rb', line 65

def lastgen
  @lastgen
end

.loggerObject

Logger



78
79
80
# File 'lib/music_coder.rb', line 78

def logger
  @logger
end

.mixes_numObject

Returns the value of attribute mixes_num.



71
72
73
# File 'lib/music_coder.rb', line 71

def mixes_num
  @mixes_num
end

.outObject

write to this to be made



67
68
69
# File 'lib/music_coder.rb', line 67

def out
  @out
end

.outpathObject

Returns the value of attribute outpath.



69
70
71
# File 'lib/music_coder.rb', line 69

def outpath
  @outpath
end

.start_tObject

Returns the value of attribute start_t.



68
69
70
# File 'lib/music_coder.rb', line 68

def start_t
  @start_t
end

.totalObject

how many hits are done, how many are todo



74
75
76
# File 'lib/music_coder.rb', line 74

def total
  @total
end

Class Method Details

.clear_dir(dir_path) ⇒ Object



106
107
108
# File 'lib/music_coder.rb', line 106

def self.clear_dir dir_path
  Dir.foreach(dir_path) {|f| fn = File.join(dir_path, f); File.delete(fn) if f != '.' && f != '..'}
end

.clear_readyObject

clears objects ready to write to file.



111
112
113
114
# File 'lib/music_coder.rb', line 111

def self.clear_ready
  App.clear_rendered
  App.out.snddists = []
end

.clear_renderedObject



116
117
118
119
# File 'lib/music_coder.rb', line 116

def self.clear_rendered
  App.clear_dir App::TMP_DIR
  App.out.filelist = FileList.new
end

.close_write(fi) ⇒ Object



23
24
25
# File 'lib/audio.rb', line 23

def App.close_write(fi)
  fi.close
end

.deep_copy(o) ⇒ Object

To copy ruby classes not pass them by reference



176
177
178
# File 'lib/music_coder.rb', line 176

def self.deep_copy(o)
  Marshal.load(Marshal.dump(o))
end

.generateObject

Generates audio from input file.



160
161
162
# File 'lib/music_coder.rb', line 160

def self.generate
  input
end

.generate_new?(file = nil) ⇒ Boolean

has there been a change in the input file since last generation?

file

the name without .rb extnesion or ./ before it

lastgen

the time of last generation

Returns:

  • (Boolean)


167
168
169
170
171
172
173
# File 'lib/music_coder.rb', line 167

def self.generate_new?(file = nil)
  file = infile if file.nil?
  now = File.ctime("../"+file+".rb")
  is_new = (lastgen != now)
  self.lastgen = now
  return is_new ? true : false
end

.load_allObject

Reload all files in case of update.



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/music_coder.rb', line 83

def self.load_all
  load File.dirname(__FILE__) + '/tone.rb'
  load File.dirname(__FILE__) + '/logger.rb'
  load File.dirname(__FILE__) + '/fader.rb'
  load File.dirname(__FILE__) + '/composer.rb'
  load File.dirname(__FILE__) + '/math_utils.rb'
  load File.dirname(__FILE__) + '/wave.rb'
  load File.dirname(__FILE__) + '/wave_data.rb'
  load File.dirname(__FILE__) + '/mixer.rb'
  load File.dirname(__FILE__) + '/audio_output.rb'
  load File.dirname(__FILE__) + '/tone_part.rb'
  load File.dirname(__FILE__) + '/tone_seq.rb'
  load File.dirname(__FILE__) + '/audio.rb'
  load File.dirname(__FILE__) + '/file_list.rb'
  load File.dirname(__FILE__) + '/snd_dist.rb'
  load File.dirname(__FILE__) + '/api/note.rb'
  load File.dirname(__FILE__) + '/api/api.rb'
  load File.dirname(__FILE__) + '/api/dist.rb'
  load File.dirname(__FILE__) + '/api/hit_sq.rb'
  load File.dirname(__FILE__) + '/api/snd.rb'
  load File.dirname(__FILE__) + '/api/mapper.rb'
end

.m_from_array(data) ⇒ Object



9
10
11
12
# File 'lib/audio.rb', line 9

def self.m_from_array data
  write_matrix = GSLng::Matrix.from_array(data)
  write_matrix.transpose
end

.main_loopObject

The top level of program calls this



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/music_coder.rb', line 122

def self.main_loop
  puts "Running application code"
  self.infile = 'input'
  App.clear_dir App.outpath+"sound/"
  self.out = AudioOutput.new
  # App.clear_ready
  self.out.outfile="#{App.outpath}sound/output.aiff"
  while true do
    if generate_new?
        load_all
        puts infile + ".rb change detected."
      begin
        load '../'+infile+'.rb'
        files = *(1..App.mixes_num)
        files.each {|let| 
          
          self.out.outfile="#{App.outpath}sound/#{let}#{App::EXT}"
          puts "+++BEGIN #{let}#{App::EXT} +++"
          App.start_t = Time.new
          self.generate
          
          }
      rescue Exception => ex
        puts "!!! - Mistake in input file!"
        puts "!!! - " + ex.message
        puts ex.backtrace.join("\n")
      end
      puts "...waiting for input changes..."
    end
  sleep(0.8)
  end
end

.open_w_audiofile(file) ⇒ Object



4
5
6
7
8
# File 'lib/audio.rb', line 4

def App.open_w_audiofile file
  raise "no file name to open for writting. " if file.nil? || App.fileoptions.nil?
#    puts "#{file} #{App.fileoptions}"
  Sndfile::File.open(file, App.fileoptions)
end

.open_write(filen) ⇒ Object



19
20
21
# File 'lib/audio.rb', line 19

def App.open_write(filen)
  File.open(filen, 'w')
end

.read_chunk_of(array, start = 0, chunk_size) ⇒ Object



41
42
43
# File 'lib/audio.rb', line 41

def App.read_chunk_of(array, start=0, chunk_size)
  array[start..start+chunk_size-1]
end

.split_array(array, size) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/audio.rb', line 27

def App.split_array(array, size)
  upto=0
  ammont=array.count
  jump=size
  out = []
  while upto < ammont do 
    # puts "upto #{upto} amount #{ammont}"
    jump = ammont - upto if upto+jump >= ammont
    # puts "array range #{upto} to #{upto+jump-1}"
    out << array[upto..upto+jump-1]
    upto+=jump
  end
  out
end

.time_sinceObject



155
156
157
# File 'lib/music_coder.rb', line 155

def self.time_since
  (Time.new - App.start_t)
end

.write_to_audiofile(fout, array) ⇒ Object



14
15
16
17
# File 'lib/audio.rb', line 14

def App.write_to_audiofile fout, array
  fout.write App.m_from_array array
  GC.start
end