Class: DirectoryProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/DirectoryProcessor.rb

Overview

Processes one or more directories

  • Scan for Multisamples *

TODO: Refactor this into an application class

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDirectoryProcessor

Instantiate the DirectoryProcessor



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/DirectoryProcessor.rb', line 30

def initialize
  # root directories to look for samples under

  @dirs = []
   
   # optional message to place in the header of each SFZ file

  @message = false
   
   # keymapping setting, set to a Multisample contant.  

   # The currently supported values are:

   # * Multisample::XFADE (default )

   # * Multisample::STRICTKEYS

  @mapping = Multisample::XFADE
   
   # If true (which is the default), all SFZ files are generated in the

   # directory the script was called from.

  @generate_in_top_dir = true
  
  @transpose = 0
  @padding = 12
end

Instance Attribute Details

#dirsObject

Returns the value of attribute dirs.



26
27
28
# File 'lib/DirectoryProcessor.rb', line 26

def dirs
  @dirs
end

#generate_in_top_dirObject

Returns the value of attribute generate_in_top_dir.



26
27
28
# File 'lib/DirectoryProcessor.rb', line 26

def generate_in_top_dir
  @generate_in_top_dir
end

#mappingObject

Returns the value of attribute mapping.



26
27
28
# File 'lib/DirectoryProcessor.rb', line 26

def mapping
  @mapping
end

#messageObject

Returns the value of attribute message.



26
27
28
# File 'lib/DirectoryProcessor.rb', line 26

def message
  @message
end

#paddingObject

Returns the value of attribute padding.



26
27
28
# File 'lib/DirectoryProcessor.rb', line 26

def padding
  @padding
end

#transposeObject

Returns the value of attribute transpose.



26
27
28
# File 'lib/DirectoryProcessor.rb', line 26

def transpose
  @transpose
end

Instance Method Details

#get_unique_filename(name, path) ⇒ Object



136
137
138
139
140
141
142
143
144
# File 'lib/DirectoryProcessor.rb', line 136

def get_unique_filename( name, path )
  result = "#{name}.sfz"
  count = 0
  while( FileTest.exists?( result ) )
    result = "#{name}-#{count}.sfz"
    count = count + 1
  end
  result
end

#process_multisample_dir(path, top_path) ⇒ Object

starting from directory,



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/DirectoryProcessor.rb', line 75

def process_multisample_dir( path, top_path )
  named_hash = {}
  #numbered_hash = Hash.new

  
  puts 

  Dir.chdir( path )
  Dir.glob( "*#{Sample.suffix_glob}" ).each{ |file|

    # Start tracking potential instrument names

    if NamedSample.sample?( file )

      # Sanitize hashkeys

      file_basename =  File.basename( file )
    file_hashkey = sanitize_multisample_name( file_basename, path )

      #add Sample to a hashed Multisample

    multi = get_multisample( file_hashkey, path, named_hash )
    multi.add( NamedSample.new( file_basename ) )
    named_hash[ file_hashkey ] = multi
    end
  }

  # for each hash entry, make an SFZ

  for key in named_hash.keys.sort
    
    filename = key
  # generate the SFZ file from the Multisample

  
  extra_name = ""  
  if @generate_in_top_dir 
    Dir.chdir( top_path )
    relative_path = path.gsub( /^\\/, '' ).gsub( /^\//, '' )
    named_hash[ key ].default_path = true
    
      #filename = "#{top_path}/#{key}.sfz"

      # create a name based on subfolders to ensure uniqueness

    extra_name = relative_path.gsub( /\\/, "_" ).gsub( /\//, "_" ) + "_"
      extra_name.gsub!(/^(\.|-|_)+/, '')
      
      filename = "#{extra_name}#{filename}"
      
  end
    filename = filename.gsub( ' ', '_')
    filename = get_unique_filename( filename, path )
          
    puts "#{key}: #{filename}"
    multi = named_hash[ key ]
    #puts multi.to_sfz

    if( multi.valid? )
    f = File.new( filename, "w" )
    f.puts multi.to_sfz
    f.close
  else
    puts "NOT VALID\n\n\n"
  end

  end
end

#sanitize_multisample_name(string, path) ⇒ Object

Santizes a multisample



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/DirectoryProcessor.rb', line 151

def sanitize_multisample_name( string, path )
# puts "sanitizing '#{string}':"


# FIXME: Potential bug: this assumes the first portion of the string

# is the most significant indicator

  result = string.gsub( /\.#{Sample.suffix_regexp}$/, '' )
result = result.sub( NamedSample.regexp, '?' )
(result,) = result.split('?')

# sometimes, sample producers A

  if result.nil?
    result = File.basename( path )
  end

  # otherwise, strip it of common paired symbols

  result.sub!( /_\d\d(_)$/, '' )  # _01 + .wav and _01_.wav

  result.sub!( /[(\[{_]$/, '' )   # blahblha( + A#1

  result.sub!( /[-_]+$/, '' )
  #result.sub!( /^[-_]+/, '' )

  result.sub!( /\s*$/, '' )
result
end

#scan_dirsObject

Scan each directory for multisamples



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/DirectoryProcessor.rb', line 54

def scan_dirs     
multidirs = get_multisample_directories

# convert each multisample directory

pwd = Dir.getwd
   if multidirs.size > 0
    
    puts "converting..."
    for dir in multidirs
      Dir.chdir( pwd )
      process_multisample_dir( dir, pwd )
    end
    
  else
    raise NoMultisamplesInDirectoryException      
   end
end