Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Undoing cp snafu

  • Loading branch information...
commit cbb315667daf72968d69533bd734aafd9eb61331 1 parent 919c45e
Matt Mower authored September 28, 2011

Showing 1 changed file with 115 additions and 2 deletions. Show diff stats Hide diff stats

  1. 117  curvesmooth.rb
117  curvesmooth.rb
... ...
@@ -1,2 +1,115 @@
1  
-#!/bin/sh
2  
-ruby curvesmooth.rb $*
  1
+#!/usr/local/bin/ruby -w
  2
+#
  3
+# This script is intended to smooth an AudioMulch automation curve
  4
+#
  5
+
  6
+if ARGV.size < 1
  7
+  puts "Specify AMH file to modify"
  8
+  exit
  9
+end
  10
+
  11
+require 'rubygems'
  12
+
  13
+require 'trollop'
  14
+require 'nokogiri'
  15
+
  16
+opts = Trollop::options do
  17
+  opt :contraption, "Name of the contraption with automation to be smoothed", :type => String
  18
+  opt :property, "Name of the control property whose value(s) is to be smoothed", :type => String
  19
+end
  20
+
  21
+Trollop::die "Must specify contraption" unless opts[:contraption]
  22
+contraption_name = opts[:contraption]
  23
+
  24
+Trollop::die "Must specify property" unless opts[:property]
  25
+property_name = opts[:property]
  26
+
  27
+
  28
+class Array
  29
+  def unzip
  30
+    i = 0
  31
+    left, right = self.partition do |obj| 
  32
+      p = (i&1).zero?
  33
+      i += 1
  34
+      p
  35
+    end
  36
+    [left, right]
  37
+  end
  38
+end
  39
+
  40
+
  41
+class ModulationSmoother
  42
+  def initialize( file_name )
  43
+    @file_name = file_name
  44
+    @doc       = File.open( file_name ) { |f| Nokogiri::XML( f ) }
  45
+  end
  46
+
  47
+  def smooth( contraption_name, property_name )
  48
+    if contraption = @doc.at_xpath( "//contraption[@name='#{contraption_name}']" )
  49
+      if source = contraption.at_xpath( "//modulation-sources/property-sources[@property-name='#{property_name}']//range-timepoints" )
  50
+        source.content = format_points( echo_points( smooth_points( extract_points( source.text ) ) ) )
  51
+      else
  52
+        puts "No property source found: #{property_name}"
  53
+        false
  54
+      end
  55
+    else
  56
+      puts "No contraption found: #{contraption_name}"
  57
+      false
  58
+    end
  59
+  end
  60
+
  61
+  # A no-op tap if we need to print out some diagnostic info
  62
+  def echo_points( points )
  63
+    puts "POINTS = #{points.size}" if $DEBUG
  64
+    puts "FIRST = #{points.first.inspect}" if $DEBUG
  65
+    points
  66
+  end
  67
+
  68
+  def format_points( points )
  69
+    points.map { |tuple| tuple.join( " " ) }.join( "\r" )
  70
+  end
  71
+
  72
+  def extract_points( data )
  73
+    data.lines.map { |line| line.chomp.split( %r{\s+} ).map { |elem| Float(elem) } }
  74
+  end
  75
+
  76
+  def save
  77
+    File.open( "#{File.basename( @file_name, ".amh" )}_sm.amh", "w" ) do |file|
  78
+      file.write( @doc.to_xml.gsub( "&#13;", "\n" ) )
  79
+    end
  80
+  end
  81
+end
  82
+
  83
+
  84
+class AveragingSmoother < ModulationSmoother
  85
+  
  86
+  def smooth_points( points )
  87
+    puts "POINTS = #{points.size}" if $DEBUG
  88
+    if points.size > 3
  89
+      [points.first] + basic_smooth( points[1..-2] ) + [points.last]
  90
+    else
  91
+      points
  92
+    end
  93
+  end
  94
+
  95
+  def basic_smooth( points )
  96
+    combine_points( points ).map do |p1,p2| 
  97
+      time1, min1, max1 = p1
  98
+      time2, min2, max2 = p2
  99
+      # [(time1+time2)/2,(min1+min2)/2,(max1+max2)/2]
  100
+      [time1,(min1+min2)/2,(max1+max2)/2]
  101
+    end
  102
+  end
  103
+
  104
+  def combine_points( points )
  105
+    l,r = points.unzip
  106
+    l.zip( r )
  107
+  end
  108
+
  109
+end
  110
+
  111
+smoother = AveragingSmoother.new( ARGV.first )
  112
+if smoother.smooth( contraption_name, property_name )
  113
+  smoother.save
  114
+end
  115
+

0 notes on commit cbb3156

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