Automatically compute divisions on import

This commit is contained in:
Matthias Neeracher 2007-10-10 06:36:31 +00:00
parent ed7870b336
commit 0755217a57
3 changed files with 37 additions and 27 deletions

View File

@ -12,7 +12,6 @@ require File.dirname($0)+'/vl'
MEAS = [] MEAS = []
PROP = { PROP = {
'divisions' => 3,
'key' => 0, 'key' => 0,
'mode' => 1, 'mode' => 1,
'timeNum' => 4, 'timeNum' => 4,
@ -337,7 +336,6 @@ if groove.size > 1
end end
RAWNOTES = [] RAWNOTES = []
ONSETS = [0,0,0,0,0,0,0,0,0,0,0,0]
if biab.sub!(/^.*?\xA0\xB0\xC1/m, '') if biab.sub!(/^.*?\xA0\xB0\xC1/m, '')
noteCount = biab.length / 12 if noteCount == 0 noteCount = biab.length / 12 if noteCount == 0
(0...noteCount).each do |i| (0...noteCount).each do |i|
@ -349,28 +347,9 @@ if biab.sub!(/^.*?\xA0\xB0\xC1/m, '')
end end
onset = (onset / 10.0).round onset = (onset / 10.0).round
RAWNOTES.push([onset, pitch]) RAWNOTES.push([onset, pitch])
ONSETS[onset % 12] += 1
end end
end end
if ONSETS[1]+ONSETS[5]+ONSETS[7]+ONSETS[11] == 0
if ONSETS[3]+ONSETS[9] == 0
if ONSETS[2]+ONSETS[4]+ONSETS[8]+ONSETS[10] == 0
PROP['divisions'] = 2
elsif ONSETS[2]+ONSETS[6]+ONSETS[10] == 0
PROP['divisions'] = 3
else
PROP['divisions'] = 6
end
elsif ONSETS[2]+ONSETS[4]+ONSETS[8]+ONSETS[10] == 0
PROP['divisions'] = 4
else
PROP['divisions'] = 12
end
else
PROP['divisions'] = 12
end
p RAWNOTES, CHORDS, STEPS, ROOTS if DEBUG p RAWNOTES, CHORDS, STEPS, ROOTS if DEBUG
CHORDS.pop CHORDS.pop

View File

@ -33,7 +33,6 @@ notes = []
lyrics = [] lyrics = []
stanzas = [] stanzas = []
lastDur = 1 lastDur = 1
divisions = 2
tied = false tied = false
repeat = 0 repeat = 0
timeNum = 4 timeNum = 4
@ -83,6 +82,9 @@ def lyPitch(pitch, base=-1)
return p return p
end end
$timesNum = 1
$timesDen = 1
def lyDur(dur) def lyDur(dur)
dur =~ /^(\d+)(\.*)(?:\*(\d+).(\d+))?/ dur =~ /^(\d+)(\.*)(?:\*(\d+).(\d+))?/
num = 1 num = 1
@ -97,7 +99,7 @@ def lyDur(dur)
num *= $3.to_i num *= $3.to_i
den *= $4.to_i den *= $4.to_i
end end
return [num,den] return [num*$timesNum,den*$timesDen]
end end
STEPS = { STEPS = {
@ -226,7 +228,7 @@ while tokens.length > 0
(\d+ # 1, 2, 4, 8, 16 ... (\d+ # 1, 2, 4, 8, 16 ...
\.*(?:\*\d+/\d+)? # ., *3/4 \.*(?:\*\d+/\d+)? # ., *3/4
)? )?
(?:\:([-+^:.a-z\d]+))? # :maj9.7-^2 (?:\:([-+^:.a-z\d]+)?)? # :maj9.7-^2
(?:/\+( # /+ (?:/\+( # /+
[a-g](?:[ei]?s)? # Root: a, bes, fis, as [a-g](?:[ei]?s)? # Root: a, bes, fis, as
))? ))?
@ -290,6 +292,11 @@ while tokens.length > 0
voltas = 0 voltas = 0
curVoltas = nil curVoltas = nil
notes.push({'begin-ending' => true}) notes.push({'begin-ending' => true})
elsif token == '\times' && tokens[0] =~ %r|^(\d+)/(\d+)|
$timesNum = $1.to_i
$timesDen = $2.to_i
stack.push([block, level, "times"])
level = nestLevel
end end
when '\lyricmode' when '\lyricmode'
if token == '--' if token == '--'
@ -360,6 +367,9 @@ while tokens.length > 0
stack.push([block, level, "endings"]) stack.push([block, level, "endings"])
level = nestLevel level = nestLevel
end end
elsif type == "times"
$timesNum = 1
$timesDen = 1
end end
end end
when '\chords', '\header', '\paper', '\lyricmode' when '\chords', '\header', '\paper', '\lyricmode'
@ -513,7 +523,6 @@ end
OUTPUT['measures'] = measures OUTPUT['measures'] = measures
OUTPUT['properties'] = [{ OUTPUT['properties'] = [{
'divisions' => divisions,
'key' => key, 'key' => key,
'mode' => mode == '\minor' ? -1 : 1, 'mode' => mode == '\minor' ? -1 : 1,
'timeNum' => timeNum, 'timeNum' => timeNum,

View File

@ -209,7 +209,7 @@ void VLPlistVisitor::VisitChord(VLChord & c)
} }
} }
- (void)readMelody:(NSArray *)melody inMeasure:(size_t)measNo - (void)readMelody:(NSArray *)melody inMeasure:(size_t)measNo onsets:(int *)onsets
{ {
VLFraction at(0); VLFraction at(0);
VLFraction tiedStart(0); VLFraction tiedStart(0);
@ -261,6 +261,11 @@ void VLPlistVisitor::VisitChord(VLChord & c)
tiedNote = note; tiedNote = note;
song->AddNote(note, measNo, at); song->AddNote(note, measNo, at);
if (!(note.fTied & VLNote::kTiedWithPrev)) {
VLFraction inQuarter = at % VLFraction(1,4);
++onsets[inQuarter.fNum * 48 / inQuarter.fDenom];
}
advanceAt: advanceAt:
at += note.fDuration; at += note.fDuration;
} }
@ -293,6 +298,7 @@ advanceAt:
std::vector<size_t> repeatStack; std::vector<size_t> repeatStack;
size_t measNo = 0; size_t measNo = 0;
int onsets[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
for (NSEnumerator * me = [measures objectEnumerator]; for (NSEnumerator * me = [measures objectEnumerator];
NSDictionary * mdict = [me nextObject]; NSDictionary * mdict = [me nextObject];
++measNo ++measNo
@ -300,7 +306,7 @@ advanceAt:
if (NSNumber * mNo = [mdict objectForKey:@"measure"]) if (NSNumber * mNo = [mdict objectForKey:@"measure"])
measNo = static_cast<size_t>([mNo intValue]); measNo = static_cast<size_t>([mNo intValue]);
[self readMelody:[mdict objectForKey:@"melody"] inMeasure:measNo]; [self readMelody:[mdict objectForKey:@"melody"] inMeasure:measNo onsets:onsets];
[self readChords:[mdict objectForKey:@"chords"] inMeasure:measNo]; [self readChords:[mdict objectForKey:@"chords"] inMeasure:measNo];
if ([[mdict objectForKey:@"tocoda"] boolValue]) if ([[mdict objectForKey:@"tocoda"] boolValue])
@ -353,6 +359,22 @@ advanceAt:
size_t empty = song->EmptyEnding(); size_t empty = song->EmptyEnding();
while (empty-- > 1) while (empty-- > 1)
song->fMeasures.pop_back(); song->fMeasures.pop_back();
if (!song->fProperties.back().fDivisions) {
if (!(onsets[1]+onsets[5]+onsets[7]+onsets[11]))
if (!(onsets[3]+onsets[9]))
if (!(onsets[2]+onsets[4]+onsets[8]+onsets[10]))
song->fProperties.back().fDivisions = 2;
else if (!(onsets[2]+onsets[6]+onsets[10]))
song->fProperties.back().fDivisions = 3;
else
song->fProperties.back().fDivisions = 6;
else if (!(onsets[2]+onsets[4]+onsets[8]+onsets[10]))
song->fProperties.back().fDivisions = 4;
else
song->fProperties.back().fDivisions = 12;
else
song->fProperties.back().fDivisions = 12;
}
} }
- (void)readPropertiesFromPlist:(NSArray *)properties - (void)readPropertiesFromPlist:(NSArray *)properties