''' Copyright 2010 Expert Sleepers. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY EXPERT SLEEPERS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL EXPERT SLEEPERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. The views and conclusions contained in the software and documentation are those of the authors and should not be interpreted as representing official policies, either expressed or implied, of Expert Sleepers. ''' ''' Script to rewrite calibration data according to a microtonal scale. v1.0.0 (14/1/2010) ''' import sys, math, copy import CalibrationData as cd def noteName( note ): f, i = math.modf( note/12.0 ) i = int(i) octave = i-1 n = int( note - 12*i ) return [ 'C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B' ][ n ] + str(octave) def retune( cd, baseNote, details ): # baseNote = note number of root of the scale # e.g. 60 is a C # centsTable = table of relative tunings from the root centsTable = [ 0 ] if details == None or details == 'just12': # just intonation (http://en.wikipedia.org/wiki/Just_intonation) # with the missing semitones needed for a chromatic scale centsTable = [ 0, 112, 204, 316, 386, 498, 610, 702, 814, 884, 996, 1088 ] elif details == 'just7': # just intonation (http://en.wikipedia.org/wiki/Just_intonation) centsTable = [ 0, 204, 386, 498, 702, 884, 1088 ] elif details == 'srutis': # 22 Srutis (http://en.wikipedia.org/wiki/Just_intonation) centsTable = [ 0, 90, 112, 182, 204, 294, 316, 386, 408, 520, 498, 590, 610, 702, 792, 814, 884, 906, 996, 1018, 1088, 1110 ] else: # interpret details as a string list of cent values centsTable = [ float(x) for x in details.split( ',' ) ] scaleLen = len(centsTable) print 'tuning to a %d note scale based on %s' % ( scaleLen, noteName(baseNote) ) rootNote = baseNote rootShift = 0 while rootNote > 0: rootNote -= scaleLen rootShift += 1 valid = [ i for i, v in enumerate( cd._valid ) if v ] firstValid = valid[0] lastValid = valid[-1] valid = copy.copy( cd._valid ) values = copy.copy( cd._values ) for i in range( firstValid, lastValid+1 ): note = i - rootNote f, oct = math.modf( note/float(scaleLen) ) oct = int(oct) n = int( note - scaleLen * oct ) cents = centsTable[ n ] relativeOctave = oct - rootShift temperedNote = baseNote + 12 * relativeOctave + 0.01 * cents if temperedNote < 0 or temperedNote > lastValid: valid[ i ] = False continue valid[ i ] = True values[ i ] = cd.value( temperedNote ) cd._valid = valid cd._values = values cd = cd.CalibrationData() filename = sys.argv[1] cd.load( filename ) baseNote = 60 if len(sys.argv) > 3: baseNote = int( sys.argv[3] ) details = None if len(sys.argv) > 4: details = sys.argv[4] retune( cd, baseNote, details ) cd.save( sys.argv[2] ) print 'wrote', sys.argv[2]