110
69
sprintf(buf, "%s%d", names[note], octave);
114
cmd_load(midicallback *mididata, char *file_name)
116
if (mididata->smf != NULL)
117
smf_delete(mididata->smf);
119
mididata->selected_track = NULL;
121
mididata->smf = smf_load(file_name);
122
if (mididata->smf == NULL) {
123
g_critical("Couldn't load '%s'.", file_name);
125
mididata->smf = smf_new();
126
if (mididata->smf == NULL) {
127
g_critical("Cannot initialize smf_t.");
134
g_message("File '%s' loaded.", file_name);
135
g_message("%s.", smf_decode(mididata->smf));
137
mididata->selected_track = smf_get_track_by_number(mididata->smf, 1);
73
enharmonic (gint input, gint key)
76
local.pitch = (input / 12) - 5;
77
local.enshift = input % 12;
79
switch (local.enshift)
83
local.pitch = (key > 6) ? (-1 + local.pitch * 7) : (local.pitch * 7);
84
local.enshift = (key > 6) ? (1) : (0);
89
local.pitch = (key < -3) ? (1 + local.pitch * 7) : (local.pitch * 7);
90
local.enshift = (key < -3) ? (-1) : (1);
95
local.pitch = 1 + local.pitch * 7;
102
(key < -1) ? (2 + local.pitch * 7) : (1 + local.pitch * 7);
103
local.enshift = (key < -1) ? (-1) : (1);
109
(key < -6) ? (3 + local.pitch * 7) : (2 + local.pitch * 7);
110
local.enshift = (key < -6) ? (-1) : (0);
116
(key > 5) ? (2 + local.pitch * 7) : (3 + local.pitch * 7);
117
local.enshift = (key > 5) ? (1) : (0);
123
(key < -4) ? (4 + local.pitch * 7) : (3 + local.pitch * 7);
124
local.enshift = (key < -4) ? (-1) : (1);
129
local.pitch = 4 + local.pitch * 7;
136
(key < -2) ? (5 + local.pitch * 7) : (4 + local.pitch * 7);
137
local.enshift = (key < -2) ? (-1) : (1);
142
local.pitch = 5 + local.pitch * 7;
149
(key < 0) ? (6 + local.pitch * 7) : (5 + local.pitch * 7);
150
local.enshift = (key < 0) ? (-1) : (1);
156
(key < -5) ? (7 + local.pitch * 7) : (6 + local.pitch * 7);
157
local.enshift = (key < -5) ? (-1) : (0);
165
ConvertLength(gint ppqn, gint duration, notetype *pnotetype){
166
/*convert length to 2 = quarter, 1 = half, 0 = whole etc...... */
167
/* quarter = 384, half = 768, whole = 1536*/
172
gint dsq = (4 * ppqn);
173
g_debug("\nDuration = %d ticks\n", duration);
175
while ((dsq >> notetype) > duration)
178
leftover = duration - (dsq >> notetype);
179
if((dsq >> notetype+1)>0)
180
while (leftover >= (dsq >> (notetype +1))){
181
leftover -= (dsq >> (notetype +1));
185
pnotetype->notetype = notetype;
186
pnotetype->numofdots = numofdots;
190
* Insert time signature into current staff
194
dotimesig (gint numerator, gint denominator)
196
DenemoGUI *gui = Denemo.gui;
197
/*only does initial TS */
198
DenemoStaff *curstaffstruct = (DenemoStaff *) gui->si->currentstaff->data;
200
curstaffstruct->timesig.time1 = numerator;
201
curstaffstruct->timesig.time2 = denominator;
205
* Insert key signature into the current staff
209
dokeysig (gint isminor, gint key)
211
DenemoGUI *gui = Denemo.gui;
213
key = key - 256; /*get flat key num, see keysigdialog.cpp */
215
g_print("\nkey = %d\n", key);
217
DenemoStaff *curstaffstruct = (DenemoStaff *) gui->si->currentstaff->data;
218
curstaffstruct->keysig.number = key;
219
curstaffstruct->keysig.isminor = isminor;
220
dnm_setinitialkeysig (curstaffstruct, key, isminor);
226
DenemoGUI *gui = Denemo.gui;
227
gui->si->tempo = (gint) (6.0e7 / (double) tempo);
231
dotrackname (gchar *name)
233
DenemoGUI *gui = Denemo.gui;
234
DenemoStaff *curstaffstruct = (DenemoStaff *) gui->si->currentstaff->data;
236
curstaffstruct->denemo_name->str = g_strdup(name);
240
doinstrname (gchar* name)
242
DenemoGUI *gui = Denemo.gui;
243
DenemoStaff *curstaffstruct = (DenemoStaff *) gui->si->currentstaff->data;
245
curstaffstruct->midi_instrument->str = g_strdup(name);
249
insert_rest_into_score(notetype length)
251
DenemoGUI *gui = Denemo.gui;
254
switch (length.notetype)
257
insert_rest_0key (gui);
260
insert_rest_1key (gui);
263
insert_rest_2key (gui);
266
insert_rest_3key (gui);
269
insert_rest_4key (gui);
272
insert_rest_5key (gui);
275
insert_rest_6key (gui);
278
insert_rest_2key (gui);
284
for (i=0;i<length.numofdots;i++)
289
AddRest(gint ppqn, gint duration)
299
ConvertLength(ppqn, rest, &length);
300
insert_rest_into_score(length);
301
ticks = ConvertNoteType2ticks(ppqn, &length);
307
insert_note_into_score(gint pitch, notetype length)
309
DenemoGUI *gui = Denemo.gui;
310
DenemoStaff *curstaffstruct = (DenemoStaff *) gui->si->currentstaff->data;
313
/* 0-8 accepted bellow */
314
DenemoScriptParam param;
315
switch(length.notetype) {
317
InsertDur0(NULL, ¶m);
320
InsertDur1(NULL, ¶m);
323
InsertDur2(NULL, ¶m);
326
InsertDur3(NULL, ¶m);
329
InsertDur4(NULL, ¶m);
332
InsertDur5(NULL, ¶m);
335
InsertDur6(NULL, ¶m);
338
InsertDur7(NULL, ¶m);
341
InsertDur2(NULL, ¶m);
342
g_warning("Cannot handle size %d",length.notetype);
345
g_debug("DenemoScriptParam = %d",param.status);
346
/* get correct note name */
347
gint key = curstaffstruct->keysig.number;
348
harmonic enote = enharmonic (pitch, (gint) key);
349
gchar *name = mid_c_offsettolily (enote.pitch, enote.enshift);
351
/* Rename note to the correct note */
352
gchar *accidental = g_strdup_printf("(d-ChangeChordNotes \"%s\")", name);
353
call_out_to_guile(accidental);
356
for (i=0;i<length.numofdots;i++)
363
AddNote(gint ppqn, gint pitch, gint duration)
368
ConvertLength(ppqn, duration, &length);
369
insert_note_into_score(pitch, length);
373
cmd_load(gchar *file_name)
377
smf = smf_load(file_name);
379
g_critical("Couldn't load '%s'.", file_name);
382
g_message("File '%s' loaded.", file_name);
383
g_message("%s.", smf_decode(smf));
142
388
#define BUFFER_SIZE 1024
145
show_event(smf_event_t *event)
148
char *decoded, *type;
150
if (smf_event_is_metadata(event))
155
decoded = smf_event_decode(event);
157
if (decoded == NULL) {
158
decoded = malloc(BUFFER_SIZE);
159
if (decoded == NULL) {
160
g_critical("show_event: malloc failed.");
164
off += snprintf(decoded + off, BUFFER_SIZE - off, "Unknown event:");
166
for (i = 0; i < event->midi_buffer_length && i < 5; i++)
167
off += snprintf(decoded + off, BUFFER_SIZE - off, " 0x%x", event->midi_buffer[i]);
170
g_message("%d: %s: %s, %f seconds, %d pulses, %d delta pulses", event->event_number, type, decoded,
171
event->time_seconds, event->time_pulses, event->delta_time_pulses);
179
decode_metadata(const smf_event_t *event, midicallback *mididata)
391
decode_metadata(const smf_event_t *event)
181
393
int off = 0, mspqn, flats, isminor;
371
process_midi(smf_event_t *event, midicallback *mididata)
375
if (smf_event_is_metadata(event))
376
decode_metadata(event, mididata);
378
decode_midi_event(event, mididata);
384
readtrack(midicallback *mididata)
388
if (mididata->selected_track == NULL) {
389
g_critical("No track");
393
smf_rewind(mididata->smf);
624
process_midi(smf_event_t *event)
626
if (smf_event_is_metadata(event))
627
decode_metadata(event);
629
decode_midi_event(event);
635
process_track(smf_track_t *track)
638
smf_t *smf = track->smf;//tracks can't be processed except inside an smf_t so create one if needed
639
gboolean delete_smf_after = FALSE;
641
delete_smf_after = TRUE;
643
smf_add_track(smf, track);
646
while (event = smf_track_get_next_event(track))
648
if(delete_smf_after) {
649
smf_track_remove_from_smf(track);
655
call_out_to_guile("(d-AddAfter)");
659
readtrack(smf_t *smf)
662
smf_track_t *selected_track;
395
while (mididata->track <= mididata->smf->number_of_tracks){
396
/* process polyphonic voices */
397
if (mididata->notestack != NULL)
398
ProcessNoteStack(mididata);
399
mididata->selected_track = smf_get_track_by_number(mididata->smf, mididata->track);
400
while ((event = smf_track_get_next_event(mididata->selected_track)) != NULL) {
402
/* print midi event */
405
/* Do something with the event */
406
process_midi(event, mididata);
410
smf_rewind(mididata->smf);
415
/* TODO consolidate this into one function */
417
new_dnm_object(midicallback *mididata, nstack *currentnote, notetype length, gboolean is_note){
418
DenemoObject *mudela_obj_new;
419
mudela_obj_new = dnm_newchord (length.notetype, length.numofdots, length.tied);
421
harmonic enote = enharmonic ((int) mididata->currentnote->pitch,mididata->key);
422
dnm_addtone (mudela_obj_new, enote.pitch, enote.enshift, mididata->gui->si->cursorclef);
423
while (mididata->currentnote->chordnotes){
424
struct harmonic enote = enharmonic ((int)
425
mididata->currentnote->chordnotes->data,mididata->key);
426
dnm_addtone (mudela_obj_new, enote.pitch, enote.enshift, mididata->gui->si->cursorclef);
427
mididata->currentnote->chordnotes = g_list_remove_link(mididata->currentnote->chordnotes,
428
g_list_first (mididata->currentnote->chordnotes));
431
object_insert (mididata->gui, mudela_obj_new);
435
* Insert time signature into current staff
439
dotimesig (gint numerator, gint denominator, midicallback *mididata)
441
/*only does initial TS */
442
DenemoStaff *curstaffstruct = (DenemoStaff *) mididata->gui->si->currentstaff->data;
444
curstaffstruct->timesig.time1 = numerator;
445
curstaffstruct->timesig.time2 = denominator;
447
mididata->barlength = mididata->smf->ppqn * 4 * numerator / denominator;
451
* Insert key signature into the current staff
455
dokeysig (gint isminor, gint key, midicallback *mididata)
459
key = key - 256; /*get flat key num, see keysigdialog.cpp */
461
g_print("\nkey = %d\n", key);
464
DenemoStaff *curstaffstruct = (DenemoStaff *) mididata->gui->si->currentstaff->data;
465
curstaffstruct->keysig.number = key;
466
curstaffstruct->keysig.isminor = isminor;
467
dnm_setinitialkeysig (curstaffstruct, key, isminor);
471
dotempo (gint tempo, midicallback *mididata)
473
mididata->gui->si->tempo = (gint) (6.0e7 / (double) tempo);
477
dotrackname (gchar *name, midicallback *mididata)
479
DenemoStaff *curstaffstruct = (DenemoStaff *) mididata->gui->si->currentstaff->data;
481
curstaffstruct->denemo_name->str = g_strdup(name);
482
dnm_set_lily_name (curstaffstruct->denemo_name, curstaffstruct->lily_name);
486
doinstrname (gchar* name, midicallback *mididata)
488
DenemoStaff *curstaffstruct = (DenemoStaff *) mididata->gui->si->currentstaff->data;
490
curstaffstruct->midi_instrument->str = g_strdup(name);
494
stack (gint pitch, gint timeon, gint delta_time, gint duration, gint tracknum)
496
nstack *mystack = (nstack *)g_malloc0(sizeof(nstack));
497
mystack->pitch = pitch;
498
mystack->timeon = timeon;
499
mystack->on_delta_time = delta_time;
500
mystack->duration = duration;
501
mystack->tracknum = tracknum;
506
* Process note on command
509
donoteon (midicallback *mididata, gint pitchon, gint velocity, gint timeon)
511
gint delta_time = mididata->delta_time; /*is this needed????*/
513
/* add a note to the stack */
514
Get_Smf_Note_OFF(pitchon, timeon, delta_time, mididata);
516
g_print ("\npitchon = %d timeon = %d event = %d\n", (gint) pitchon, (gint) timeon, (gint) mididata->event_number);
521
* Process note off command
524
donoteoff (midicallback *mididata)
526
nstack *currentnote = mididata->currentnote;
527
if (currentnote != NULL){
528
/*process the note found*/
529
process_list(mididata);
530
g_free(mididata->currentnote);
532
mididata->currentnote = NULL;
536
ChordToneCheck(midicallback *mididata, gint pitch, gint timeon, gint delta_time, gint duration){
537
nstack *currentnote = mididata->currentnote;
538
gint tracknum = (int) mididata->track;
540
if ((currentnote->timeon == timeon) &&
541
(currentnote->duration == duration)){
542
g_print("\n***Same Duration and timeon***\n");
543
/* append note to GList */
544
currentnote->chordnotes = g_list_append(currentnote->chordnotes, (gpointer)pitch);
547
else if ((currentnote->timeon <= timeon) && (timeon <= ((int) currentnote->timeon + (int) currentnote->duration))){
548
g_print("\n***NOTE_ON colliding between a NOTE_ON and NOTE_OFF tracknum = %d***\n",tracknum);
549
/* append to voice glist */
550
nstack *noteon = stack(pitch, timeon, delta_time, duration, tracknum);
551
/* store noteon in mididata->notestack */
552
mididata->notestack = g_list_append(mididata->notestack, noteon);
667
for (track=1;track <= smf->number_of_tracks;track++){
668
selected_track = smf_get_track_by_number(smf, track);
669
process_track(selected_track);
670
if (track+1 <= smf->number_of_tracks)
559
Get_Smf_Note_OFF (gint pitch, gint timeon, gint delta_time, midicallback *mididata){
560
gint event_number = mididata->event_number;
563
gint tracknum = mididata->track;
564
gboolean chordtone = FALSE;
566
while ((event = smf_track_get_event_by_number(mididata->selected_track, event_number)) != NULL){
567
if (((event->midi_buffer[0] & SYS_EXCLUSIVE_MESSAGE1) == NOTE_OFF)
568
& (event->midi_buffer[1] == (int) pitch)){
569
duration = event->time_pulses - timeon;
570
if (mididata->currentnote != NULL)
571
chordtone = ChordToneCheck(mididata, pitch, timeon, delta_time, duration);
573
/* store note in mididata->currentnote */
574
mididata->currentnote = stack(pitch, timeon, delta_time, duration, tracknum);
576
g_print("\nFound corresponding note off to pitch %d timeon = %d duration = %d\n", (gint) pitch, timeon, (gint) duration);
585
enharmonic (gint input, gint key)
588
local.pitch = (input / 12) - 5;
589
local.enshift = input % 12;
591
switch (local.enshift)
595
local.pitch = (key > 6) ? (-1 + local.pitch * 7) : (local.pitch * 7);
596
local.enshift = (key > 6) ? (1) : (0);
601
local.pitch = (key < -3) ? (1 + local.pitch * 7) : (local.pitch * 7);
602
local.enshift = (key < -3) ? (-1) : (1);
607
local.pitch = 1 + local.pitch * 7;
614
(key < -1) ? (2 + local.pitch * 7) : (1 + local.pitch * 7);
615
local.enshift = (key < -1) ? (-1) : (1);
621
(key < -6) ? (3 + local.pitch * 7) : (2 + local.pitch * 7);
622
local.enshift = (key < -6) ? (-1) : (0);
628
(key > 5) ? (2 + local.pitch * 7) : (3 + local.pitch * 7);
629
local.enshift = (key > 5) ? (1) : (0);
635
(key < -4) ? (4 + local.pitch * 7) : (3 + local.pitch * 7);
636
local.enshift = (key < -4) ? (-1) : (1);
641
local.pitch = 4 + local.pitch * 7;
648
(key < -2) ? (5 + local.pitch * 7) : (4 + local.pitch * 7);
649
local.enshift = (key < -2) ? (-1) : (1);
654
local.pitch = 5 + local.pitch * 7;
661
(key < 0) ? (6 + local.pitch * 7) : (5 + local.pitch * 7);
662
local.enshift = (key < 0) ? (-1) : (1);
668
(key < -5) ? (7 + local.pitch * 7) : (6 + local.pitch * 7);
669
local.enshift = (key < -5) ? (-1) : (0);
676
ConvertNoteType2ticks(midicallback *mididata, notetype *gnotetype){
677
ConvertNoteType2ticks(gint ppqn, notetype *gnotetype){
678
gint PPQN = mididata->smf->ppqn;
679
679
gint notetype = (int) gnotetype->notetype;
680
680
gint numofdots = (int) gnotetype->numofdots;
681
gint dsq = (4 * PPQN);
681
gint dsq = (4 * ppqn);
684
684
ticks = dsq >> notetype;
685
685
while (i++ < numofdots)
686
ticks += dsq >> notetype + 1;
686
ticks += dsq >> (notetype + 1);
691
notetype ConvertLength(gint duration, midicallback *mididata){
692
/*convert length to 2 = quarter, 1 = half, 0 = whole etc...... */
693
/* quarter = 384, half = 768, whole = 1536*/
695
gint PPQN = mididata->smf->ppqn;
700
gint dsq = (4 * PPQN);
703
while ((dsq >> notetype) > duration)
706
leftover = duration - (dsq >> notetype);
708
while (leftover >= (dsq >> (notetype +1))){
709
leftover -= (dsq >> (notetype +1));
713
gnotetype.notetype = notetype;
714
gnotetype.numofdots = numofdots;
715
gnotetype.tied = leftover;
720
* check to see if a new staff needs to be added
722
void StaffCheck(midicallback *mididata){
723
DenemoScore *si = mididata->gui->si;
724
gint track = (int) mididata->currentnote->tracknum;
725
gint currentstaffnum = si->currentstaffnum;
727
if (track > currentstaffnum) /*if not first track add track */
729
si->currentstaffnum++;
730
si->currentstaff = g_list_first (si->thescore); /* set the first track to be copied */
731
newstaff (mididata->gui, ADDFROMLOAD, DENEMO_NONE); /* add track */
732
si->currentstaff = g_list_last (si->thescore); /* make last staff the current */
733
mididata->lastoff = 0;
735
mididata->bartime = 0;
736
si->currentmeasurenum = 1;
742
* to see if we need to add a new measure
744
void MeasureCheck(midicallback *mididata){
745
if (mididata->bartime >= mididata->barlength)
746
{ /* mididata->bartime >= barlenth will be true if there are rests or notes
747
going over end of measures. */
748
if (!mididata->gui->si->currentmeasure->next)
749
/* Add a measure and make it currentmeasure */
750
mididata->gui->si->currentmeasure =
751
dnm_addmeasures (mididata->gui->si, mididata->gui->si->currentmeasurenum, 1, 1);
753
mididata->gui->si->currentmeasure = mididata->gui->si->currentmeasure->next;
754
/* Now the stuff that needs to be done for each case */
755
mididata->gui->si->currentmeasurenum++;
756
mididata->gui->si->currentobject = NULL;
757
mididata->gui->si->cursor_x = 0;
758
memcpy (mididata->gui->si->cursoraccs, mididata->gui->si->nextmeasureaccs, SEVENGINTS);
759
memcpy (mididata->gui->si->curmeasureaccs, mididata->gui->si->nextmeasureaccs, SEVENGINTS);
760
mididata->gui->si->curmeasureclef = mididata->gui->si->cursorclef;
761
mididata->bartime = 0;
765
void RestCheck(midicallback *mididata){
767
gint PPQN = mididata->smf->ppqn;
768
gint starttime = (int) mididata->currentnote->timeon;
769
gint duration = (int) mididata->currentnote->duration;
770
gint on_delta_time = (int) mididata->currentnote->on_delta_time;
771
gint dsq = (4 * mididata->smf->ppqn);
773
if (starttime > mididata->lastoff){
774
rest = starttime - mididata->lastoff;
775
if (mididata->bartime + on_delta_time >= (mididata->barlength)){
776
while(mididata->barlength - mididata->bartime){
777
rest = mididata->barlength - mididata->bartime;
778
struct notetype length = ConvertLength(rest, mididata);
779
new_dnm_object(mididata, mididata->currentnote, length, FALSE);
780
mididata->bartime += ConvertNoteType2ticks(mididata, &length);
781
mididata->lastoff += ConvertNoteType2ticks(mididata, &length);
783
process_list(mididata);
784
rest = 0;/* I am not sure if this is the best choice here*/
787
struct notetype length = ConvertLength(rest, mididata);
788
new_dnm_object(mididata, mididata->currentnote, length, FALSE);
789
mididata->bartime = mididata->bartime + ConvertNoteType2ticks(mididata, &length);
790
mididata->lastoff += ConvertNoteType2ticks(mididata, &length);
797
* This takes a note and calculates if the note is to big to fit in the measure.
798
* If it is it fits what it can fit in the measure and ties it to the
799
* remainder in the next measure.
801
void TiedNoteCheck(midicallback *mididata){
802
gint duration = (int) mididata->currentnote->duration;
803
gint barlength = (int) mididata->barlength;
805
if ((mididata->bartime + duration) > barlength)
807
mididata->leftover = (int) (duration - (barlength - mididata->bartime)); /*tied over bar line */
808
duration = (int) (barlength - mididata->bartime); /*new value that fits in bar*/
809
mididata->currentnote->duration = duration;
812
mididata->leftover = (int) 0;
815
void ProcessNote(midicallback *mididata) {
816
DenemoScore *si = mididata->gui->si;
817
gint starttime = (int) mididata->currentnote->timeon;
818
gint lastoff = mididata->lastoff;
819
gint duration = (int) mididata->currentnote->duration;
820
gint pitch = (int) mididata->currentnote->pitch;
822
/*if a noteon event happends just after anouther note finishes just add the next note */
823
if (starttime == mididata->lastoff) {
824
notetype length = ConvertLength(duration, mididata);
825
length.tied = mididata->leftover;
826
new_dnm_object (mididata, mididata->currentnote, length, TRUE);
827
mididata->lastoff = starttime + duration;
828
mididata->bartime += duration;
830
if (mididata->leftover){
831
MeasureCheck(mididata);
832
notetype tied_length = ConvertLength(mididata->leftover, mididata);
833
new_dnm_object (mididata, mididata->currentnote, tied_length, TRUE);
834
mididata->lastoff += mididata->leftover;
835
mididata->bartime += mididata->leftover;
836
mididata->leftover = 0;
838
/* if the starttime of noteon is not were we left off we need to do something */
839
if ((starttime != mididata->lastoff) && (mididata->leftover !=0)){
840
mididata->leftover = 0;
841
mididata->currentnote->timeon = mididata->lastoff;
842
process_list(mididata);
847
ProcessNoteStack(midicallback *mididata){
848
while (mididata->notestack){
849
/* Assign the note to currentnote */
850
mididata->currentnote = mididata->notestack->data;
851
/* send the note on for processing */
852
process_list(mididata);
853
/* remove note from list */
854
mididata->notestack = g_list_remove_link(mididata->notestack,
855
g_list_first (mididata->notestack));
859
* Add notes to the current staff
862
process_list(midicallback *mididata)
864
/* check to see if we need to add a new staff */
865
StaffCheck(mididata);
866
/* check to see if we need to add a measure */
867
MeasureCheck(mididata);
870
/*check for notes tied across measure*/
871
TiedNoteCheck(mididata);
872
/* note processing stuff */
873
ProcessNote(mididata);
877
692
importMidi (gchar *filename, DenemoGUI *gui)
879
midicallback *mididata = (midicallback *)g_malloc0(sizeof(midicallback));
880
mididata->selected_track = NULL;
881
mididata->smf = NULL;
883
mididata->bartime = 0;
884
mididata->lastoff = 0;
886
695
gint ret = 0; // (-1 on failure)
888
697
/* delete old data in the score */
889
dnm_deletescore (NULL, gui);
698
deletescore (NULL, gui);
891
699
/* load the file */
892
ret = cmd_load(mididata, filename);
700
smf = cmd_load(filename);
893
703
/* Read Track Data */
706
/* Delete data...Does it free it? */