playmidi.cpp (23191B)
1 /* 2 3 TiMidity -- Experimental MIDI to WAVE converter 4 Copyright (C) 1995 Tuukka Toivonen <toivonen@clinet.fi> 5 6 This program is free software; you can redistribute it and/or modify 7 it under the terms of the GNU General Public License as published by 8 the Free Software Foundation; either version 2 of the License, or 9 (at your option) any later version. 10 11 This program is distributed in the hope that it will be useful, 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU General Public License for more details. 15 16 You should have received a copy of the GNU General Public License 17 along with this program; if not, write to the Free Software 18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 19 20 playmidi.c -- random stuff in need of rearrangement 21 22 */ 23 24 #include <stdio.h> 25 #include <stdlib.h> 26 #include <string.h> 27 28 #include "config.h" 29 #include "common.h" 30 #include "instrum.h" 31 #include "playmidi.h" 32 #include "readmidi.h" 33 #include "output.h" 34 #include "mix.h" 35 #include "controls.h" 36 #include "timidity.h" 37 38 #include "tables.h" 39 40 #include "structs.h" 41 42 //void Real_Tim_Free( void *pt ); 43 44 Channel channel[16]; 45 Voice voice[MAX_VOICES]; 46 47 int 48 voices=DEFAULT_VOICES; 49 50 int32_t 51 control_ratio=0, 52 amplification=DEFAULT_AMPLIFICATION; 53 54 float 55 master_volume; 56 57 int32_t drumchannels=DEFAULT_DRUMCHANNELS; 58 int adjust_panning_immediately=0; 59 60 static int midi_playing = 0; 61 static int32_t lost_notes, cut_notes; 62 static int32_t *buffer_pointer; 63 static int32_t buffered_count; 64 extern int32_t *common_buffer; 65 66 static MidiEvent *event_list, *current_event; 67 static int32_t sample_count, current_sample; 68 69 static void adjust_amplification(void) 70 { 71 master_volume = (float)(amplification) / (float)100.0; 72 } 73 74 static void reset_voices(void) 75 { 76 int i; 77 for (i=0; i<MAX_VOICES; i++) 78 voice[i].status=VOICE_FREE; 79 } 80 81 /* Process the Reset All Controllers event */ 82 static void reset_controllers(int c) 83 { 84 channel[c].volume=90; /* Some standard says, although the SCC docs say 0. */ 85 channel[c].expression=127; /* SCC-1 does this. */ 86 channel[c].sustain=0; 87 channel[c].pitchbend=0x2000; 88 channel[c].pitchfactor=0; /* to be computed */ 89 } 90 91 static void redraw_controllers(int c) 92 { 93 ctl->volume(c, channel[c].volume); 94 ctl->expression(c, channel[c].expression); 95 ctl->sustain(c, channel[c].sustain); 96 ctl->pitch_bend(c, channel[c].pitchbend); 97 } 98 99 static void reset_midi(void) 100 { 101 int i; 102 for (i=0; i<16; i++) 103 { 104 reset_controllers(i); 105 /* The rest of these are unaffected by the Reset All Controllers event */ 106 channel[i].program=default_program; 107 channel[i].panning=NO_PANNING; 108 channel[i].pitchsens=2; 109 channel[i].bank=0; /* tone bank or drum set */ 110 } 111 reset_voices(); 112 } 113 114 static void select_sample(int v, Instrument *ip) 115 { 116 int32_t f, cdiff, diff; 117 int s,i; 118 Sample *sp, *closest; 119 120 s=ip->samples; 121 sp=ip->sample; 122 123 if (s==1) 124 { 125 voice[v].sample=sp; 126 return; 127 } 128 129 f=voice[v].orig_frequency; 130 for (i=0; i<s; i++) 131 { 132 if (sp->low_freq <= f && sp->high_freq >= f) 133 { 134 voice[v].sample=sp; 135 return; 136 } 137 sp++; 138 } 139 140 /* 141 No suitable sample found! We'll select the sample whose root 142 frequency is closest to the one we want. (Actually we should 143 probably convert the low, high, and root frequencies to MIDI note 144 values and compare those.) */ 145 146 cdiff=0x7FFFFFFF; 147 closest=sp=ip->sample; 148 for(i=0; i<s; i++) 149 { 150 diff=sp->root_freq - f; 151 if (diff<0) diff=-diff; 152 if (diff<cdiff) 153 { 154 cdiff=diff; 155 closest=sp; 156 } 157 sp++; 158 } 159 voice[v].sample=closest; 160 return; 161 } 162 163 static void recompute_freq(int v) 164 { 165 int 166 sign=(voice[v].sample_increment < 0), /* for bidirectional loops */ 167 pb=channel[voice[v].channel].pitchbend; 168 double a; 169 170 if (!voice[v].sample->sample_rate) 171 return; 172 173 if (voice[v].vibrato_control_ratio) 174 { 175 /* This instrument has vibrato. Invalidate any precomputed 176 sample_increments. */ 177 178 int i=VIBRATO_SAMPLE_INCREMENTS; 179 while (i--) 180 voice[v].vibrato_sample_increment[i]=0; 181 } 182 183 if (pb==0x2000 || pb<0 || pb>0x3FFF) 184 voice[v].frequency=voice[v].orig_frequency; 185 else 186 { 187 pb-=0x2000; 188 if (!(channel[voice[v].channel].pitchfactor)) 189 { 190 /* Damn. Somebody bent the pitch. */ 191 int32_t i=pb*channel[voice[v].channel].pitchsens; 192 if (pb<0) 193 i=-i; 194 channel[voice[v].channel].pitchfactor= 195 (float)(bend_fine[(i>>5) & 0xFF] * bend_coarse[i>>13]); 196 } 197 if (pb>0) 198 voice[v].frequency= 199 (int32_t)(channel[voice[v].channel].pitchfactor * 200 (double)(voice[v].orig_frequency)); 201 else 202 voice[v].frequency= 203 (int32_t)((double)(voice[v].orig_frequency) / 204 channel[voice[v].channel].pitchfactor); 205 } 206 207 a = FSCALE(((double)(voice[v].sample->sample_rate) * 208 (double)(voice[v].frequency)) / 209 ((double)(voice[v].sample->root_freq) * 210 (double)(play_mode->rate)), 211 FRACTION_BITS); 212 213 if (sign) 214 a = -a; /* need to preserve the loop direction */ 215 216 voice[v].sample_increment = (int32_t)(a); 217 } 218 219 static void recompute_amp(int v) 220 { 221 int32_t tempamp; 222 223 /* TODO: use fscale */ 224 225 tempamp= (voice[v].velocity * 226 channel[voice[v].channel].volume * 227 channel[voice[v].channel].expression); /* 21 bits */ 228 229 if (!(play_mode->encoding & PE_MONO)) 230 { 231 if (voice[v].panning > 60 && voice[v].panning < 68) 232 { 233 voice[v].panned=PANNED_CENTER; 234 235 voice[v].left_amp= 236 FSCALENEG((double)(tempamp) * voice[v].sample->volume * master_volume, 237 21); 238 } 239 else if (voice[v].panning<5) 240 { 241 voice[v].panned = PANNED_LEFT; 242 243 voice[v].left_amp= 244 FSCALENEG((double)(tempamp) * voice[v].sample->volume * master_volume, 245 20); 246 } 247 else if (voice[v].panning>123) 248 { 249 voice[v].panned = PANNED_RIGHT; 250 251 voice[v].left_amp= /* left_amp will be used */ 252 FSCALENEG((double)(tempamp) * voice[v].sample->volume * master_volume, 253 20); 254 } 255 else 256 { 257 voice[v].panned = PANNED_MYSTERY; 258 259 voice[v].left_amp= 260 FSCALENEG((double)(tempamp) * voice[v].sample->volume * master_volume, 261 27); 262 voice[v].right_amp=voice[v].left_amp * (voice[v].panning); 263 voice[v].left_amp *= (float)(127-voice[v].panning); 264 } 265 } 266 else 267 { 268 voice[v].panned=PANNED_CENTER; 269 270 voice[v].left_amp= 271 FSCALENEG((double)(tempamp) * voice[v].sample->volume * master_volume, 272 21); 273 } 274 } 275 276 static void start_note(MidiEvent *e, int i) 277 { 278 Instrument *ip; 279 int j; 280 281 if (ISDRUMCHANNEL(e->channel)) 282 { 283 if (!(ip=drumset[channel[e->channel].bank]->tone[e->a].instrument)) 284 { 285 if (!(ip=drumset[0]->tone[e->a].instrument)) 286 return; /* No instrument? Then we can't play. */ 287 } 288 if (ip->samples != 1) 289 { 290 ctl->cmsg(CMSG_WARNING, VERB_VERBOSE, 291 "Strange: percussion instrument with %d samples!", ip->samples); 292 } 293 294 if (ip->sample->note_to_use) /* Do we have a fixed pitch? */ 295 voice[i].orig_frequency=freq_table[(int)(ip->sample->note_to_use)]; 296 else 297 voice[i].orig_frequency=freq_table[e->a & 0x7F]; 298 299 /* drums are supposed to have only one sample */ 300 voice[i].sample=ip->sample; 301 } 302 else 303 { 304 if (channel[e->channel].program==SPECIAL_PROGRAM) 305 ip=default_instrument; 306 else if (!(ip=tonebank[channel[e->channel].bank]-> 307 tone[channel[e->channel].program].instrument)) 308 { 309 if (!(ip=tonebank[0]->tone[channel[e->channel].program].instrument)) 310 return; /* No instrument? Then we can't play. */ 311 } 312 313 if (ip->sample->note_to_use) /* Fixed-pitch instrument? */ 314 voice[i].orig_frequency=freq_table[(int)(ip->sample->note_to_use)]; 315 else 316 voice[i].orig_frequency=freq_table[e->a & 0x7F]; 317 select_sample(i, ip); 318 } 319 320 voice[i].status=VOICE_ON; 321 voice[i].channel=e->channel; 322 voice[i].note=e->a; 323 voice[i].velocity=e->b; 324 voice[i].sample_offset=0; 325 voice[i].sample_increment=0; /* make sure it isn't negative */ 326 327 voice[i].tremolo_phase=0; 328 voice[i].tremolo_phase_increment=voice[i].sample->tremolo_phase_increment; 329 voice[i].tremolo_sweep=voice[i].sample->tremolo_sweep_increment; 330 voice[i].tremolo_sweep_position=0; 331 332 voice[i].vibrato_sweep=voice[i].sample->vibrato_sweep_increment; 333 voice[i].vibrato_sweep_position=0; 334 voice[i].vibrato_control_ratio=voice[i].sample->vibrato_control_ratio; 335 voice[i].vibrato_control_counter=voice[i].vibrato_phase=0; 336 for (j=0; j<VIBRATO_SAMPLE_INCREMENTS; j++) 337 voice[i].vibrato_sample_increment[j]=0; 338 339 if (channel[e->channel].panning != NO_PANNING) 340 voice[i].panning=channel[e->channel].panning; 341 else 342 voice[i].panning=voice[i].sample->panning; 343 344 recompute_freq(i); 345 recompute_amp(i); 346 if (voice[i].sample->modes & MODES_ENVELOPE) 347 { 348 /* Ramp up from 0 */ 349 voice[i].envelope_stage=0; 350 voice[i].envelope_volume=0; 351 voice[i].control_counter=0; 352 recompute_envelope(i); 353 apply_envelope_to_amp(i); 354 } 355 else 356 { 357 voice[i].envelope_increment=0; 358 apply_envelope_to_amp(i); 359 } 360 ctl->note(i); 361 } 362 363 static void kill_note(int i) 364 { 365 voice[i].status=VOICE_DIE; 366 ctl->note(i); 367 } 368 369 /* Only one instance of a note can be playing on a single channel. */ 370 static void note_on(MidiEvent *e) 371 { 372 int i=voices, lowest=-1; 373 int32_t lv=0x7FFFFFFF, v; 374 375 while (i--) 376 { 377 if (voice[i].status == VOICE_FREE) 378 lowest=i; /* Can't get a lower volume than silence */ 379 else if (voice[i].channel==e->channel && 380 (voice[i].note==e->a || channel[voice[i].channel].mono)) 381 kill_note(i); 382 } 383 384 if (lowest != -1) 385 { 386 /* Found a free voice. */ 387 start_note(e,lowest); 388 return; 389 } 390 391 /* Look for the decaying note with the lowest volume */ 392 i=voices; 393 while (i--) 394 { 395 if ((voice[i].status!=VOICE_ON) && 396 (voice[i].status!=VOICE_DIE)) 397 { 398 v=voice[i].left_mix; 399 if ((voice[i].panned==PANNED_MYSTERY) && (voice[i].right_mix>v)) 400 v=voice[i].right_mix; 401 if (v<lv) 402 { 403 lv=v; 404 lowest=i; 405 } 406 } 407 } 408 409 if (lowest != -1) 410 { 411 /* This can still cause a click, but if we had a free voice to 412 spare for ramping down this note, we wouldn't need to kill it 413 in the first place... Still, this needs to be fixed. Perhaps 414 we could use a reserve of voices to play dying notes only. */ 415 416 cut_notes++; 417 voice[lowest].status=VOICE_FREE; 418 ctl->note(lowest); 419 start_note(e,lowest); 420 } 421 else 422 lost_notes++; 423 } 424 425 static void finish_note(int i) 426 { 427 if (voice[i].sample->modes & MODES_ENVELOPE) 428 { 429 /* We need to get the envelope out of Sustain stage */ 430 voice[i].envelope_stage=3; 431 voice[i].status=VOICE_OFF; 432 recompute_envelope(i); 433 apply_envelope_to_amp(i); 434 ctl->note(i); 435 } 436 else 437 { 438 /* Set status to OFF so resample_voice() will let this voice out 439 of its loop, if any. In any case, this voice dies when it 440 hits the end of its data (ofs>=data_length). */ 441 voice[i].status=VOICE_OFF; 442 } 443 } 444 445 static void note_off(MidiEvent *e) 446 { 447 int i=voices; 448 while (i--) 449 if (voice[i].status==VOICE_ON && 450 voice[i].channel==e->channel && 451 voice[i].note==e->a) 452 { 453 if (channel[e->channel].sustain) 454 { 455 voice[i].status=VOICE_SUSTAINED; 456 ctl->note(i); 457 } 458 else 459 finish_note(i); 460 return; 461 } 462 } 463 464 /* Process the All Notes Off event */ 465 static void all_notes_off(int c) 466 { 467 int i=voices; 468 ctl->cmsg(CMSG_INFO, VERB_DEBUG, "All notes off on channel %d", c); 469 while (i--) 470 if (voice[i].status==VOICE_ON && 471 voice[i].channel==c) 472 { 473 if (channel[c].sustain) 474 { 475 voice[i].status=VOICE_SUSTAINED; 476 ctl->note(i); 477 } 478 else 479 finish_note(i); 480 } 481 } 482 483 /* Process the All Sounds Off event */ 484 static void all_sounds_off(int c) 485 { 486 int i=voices; 487 while (i--) 488 if (voice[i].channel==c && 489 voice[i].status != VOICE_FREE && 490 voice[i].status != VOICE_DIE) 491 { 492 kill_note(i); 493 } 494 } 495 496 static void adjust_pressure(MidiEvent *e) 497 { 498 int i=voices; 499 while (i--) 500 if (voice[i].status==VOICE_ON && 501 voice[i].channel==e->channel && 502 voice[i].note==e->a) 503 { 504 voice[i].velocity=e->b; 505 recompute_amp(i); 506 apply_envelope_to_amp(i); 507 return; 508 } 509 } 510 511 static void adjust_panning(int c) 512 { 513 int i=voices; 514 while (i--) 515 if ((voice[i].channel==c) && 516 (voice[i].status==VOICE_ON || voice[i].status==VOICE_SUSTAINED)) 517 { 518 voice[i].panning=channel[c].panning; 519 recompute_amp(i); 520 apply_envelope_to_amp(i); 521 } 522 } 523 524 static void drop_sustain(int c) 525 { 526 int i=voices; 527 while (i--) 528 if (voice[i].status==VOICE_SUSTAINED && voice[i].channel==c) 529 finish_note(i); 530 } 531 532 static void adjust_pitchbend(int c) 533 { 534 int i=voices; 535 while (i--) 536 if (voice[i].status!=VOICE_FREE && voice[i].channel==c) 537 { 538 recompute_freq(i); 539 } 540 } 541 542 static void adjust_volume(int c) 543 { 544 int i=voices; 545 while (i--) 546 if (voice[i].channel==c && 547 (voice[i].status==VOICE_ON || voice[i].status==VOICE_SUSTAINED)) 548 { 549 recompute_amp(i); 550 apply_envelope_to_amp(i); 551 } 552 } 553 554 static void seek_forward( int32_t until_time) 555 { 556 reset_voices(); 557 while (current_event->time < until_time) 558 { 559 switch(current_event->type) 560 { 561 /* All notes stay off. Just handle the parameter changes. */ 562 563 case ME_PITCH_SENS: 564 channel[current_event->channel].pitchsens= 565 current_event->a; 566 channel[current_event->channel].pitchfactor=0; 567 break; 568 569 case ME_PITCHWHEEL: 570 channel[current_event->channel].pitchbend= 571 current_event->a + current_event->b * 128; 572 channel[current_event->channel].pitchfactor=0; 573 break; 574 575 case ME_MAINVOLUME: 576 channel[current_event->channel].volume=current_event->a; 577 break; 578 579 case ME_PAN: 580 channel[current_event->channel].panning=current_event->a; 581 break; 582 583 case ME_EXPRESSION: 584 channel[current_event->channel].expression=current_event->a; 585 break; 586 587 case ME_PROGRAM: 588 if (ISDRUMCHANNEL(current_event->channel)) 589 /* Change drum set */ 590 channel[current_event->channel].bank=current_event->a; 591 else 592 channel[current_event->channel].program=current_event->a; 593 break; 594 595 case ME_SUSTAIN: 596 channel[current_event->channel].sustain=current_event->a; 597 break; 598 599 case ME_RESET_CONTROLLERS: 600 reset_controllers(current_event->channel); 601 break; 602 603 case ME_TONE_BANK: 604 channel[current_event->channel].bank=current_event->a; 605 break; 606 607 case ME_EOT: 608 current_sample=current_event->time; 609 return; 610 } 611 current_event++; 612 } 613 /*current_sample=current_event->time;*/ 614 if (current_event != event_list) 615 current_event--; 616 current_sample=until_time; 617 } 618 619 static void skip_to( int32_t until_time) 620 { 621 if (current_sample > until_time) 622 current_sample=0; 623 624 reset_midi(); 625 buffered_count=0; 626 buffer_pointer=common_buffer; 627 current_event=event_list; 628 629 if (until_time) 630 seek_forward(until_time); 631 ctl->reset(); 632 } 633 634 static int apply_controls(void) 635 { 636 int rc, i, did_skip=0; 637 int val; 638 /* ASCII renditions of CD player pictograms indicate approximate effect */ 639 do 640 switch(rc=ctl->read(&val)) 641 { 642 case RC_QUIT: /* [] */ 643 case RC_LOAD_FILE: 644 case RC_NEXT: /* >>| */ 645 case RC_REALLY_PREVIOUS: /* |<< */ 646 return rc; 647 648 case RC_CHANGE_VOLUME: 649 if (val>0 || amplification > -val) 650 amplification += val; 651 else 652 amplification=0; 653 if (amplification > MAX_AMPLIFICATION) 654 amplification=MAX_AMPLIFICATION; 655 adjust_amplification(); 656 for (i=0; i<voices; i++) 657 if (voice[i].status != VOICE_FREE) 658 { 659 recompute_amp(i); 660 apply_envelope_to_amp(i); 661 } 662 ctl->master_volume(amplification); 663 break; 664 665 case RC_PREVIOUS: /* |<< */ 666 if (current_sample < 2*play_mode->rate) 667 return RC_REALLY_PREVIOUS; 668 return RC_RESTART; 669 670 case RC_RESTART: /* |<< */ 671 skip_to(0); 672 did_skip=1; 673 break; 674 675 case RC_JUMP: 676 if (val >= sample_count) 677 return RC_NEXT; 678 skip_to(val); 679 return rc; 680 681 case RC_FORWARD: /* >> */ 682 if (val+current_sample >= sample_count) 683 return RC_NEXT; 684 skip_to(val+current_sample); 685 did_skip=1; 686 break; 687 688 case RC_BACK: /* << */ 689 if (current_sample > val) 690 skip_to(current_sample-val); 691 else 692 skip_to(0); /* We can't seek to end of previous song. */ 693 did_skip=1; 694 break; 695 } 696 while (rc!= RC_NO_RETURN_VALUE); 697 698 /* Advertise the skip so that we stop computing the audio buffer */ 699 if (did_skip) 700 return RC_JUMP; 701 else 702 return rc; 703 } 704 705 static void do_compute_data( int32_t count) 706 { 707 int i; 708 memset(buffer_pointer, 0, 709 (play_mode->encoding & PE_MONO) ? (count * 4) : (count * 8)); 710 for (i=0; i<voices; i++) 711 { 712 if(voice[i].status != VOICE_FREE) 713 mix_voice(buffer_pointer, i, count); 714 } 715 current_sample += count; 716 } 717 718 /* count=0 means flush remaining buffered data to output device, then 719 flush the device itself */ 720 static int compute_data(void *stream, int32_t count, int* bytes_written) 721 { 722 int rc, channels; 723 724 if ( play_mode->encoding & PE_MONO ) 725 channels = 1; 726 else 727 channels = 2; 728 729 if (!count) 730 { 731 if (buffered_count) { 732 s32tobuf(stream, common_buffer, channels*buffered_count); 733 if (bytes_written && (play_mode->encoding & PE_16BIT)) 734 *bytes_written += channels * buffered_count * 2; 735 else 736 *bytes_written += channels * buffered_count; 737 738 //No need anymore 739 //play_mode->output_data(stream, channels*buffered_count, bytes_written); 740 } 741 buffer_pointer=common_buffer; 742 buffered_count=0; 743 return RC_NO_RETURN_VALUE; 744 } 745 746 while ((count+buffered_count) >= AUDIO_BUFFER_SIZE) 747 { 748 do_compute_data(AUDIO_BUFFER_SIZE-buffered_count); 749 count -= AUDIO_BUFFER_SIZE-buffered_count; 750 s32tobuf(stream, common_buffer, channels*AUDIO_BUFFER_SIZE); 751 if (bytes_written && (play_mode->encoding & PE_16BIT)) 752 *bytes_written += channels * AUDIO_BUFFER_SIZE * 2; 753 else 754 *bytes_written += channels * AUDIO_BUFFER_SIZE; 755 756 //play_mode->output_data(stream, channels*AUDIO_BUFFER_SIZE, bytes_written); 757 buffer_pointer=common_buffer; 758 buffered_count=0; 759 760 ctl->current_time(current_sample); 761 if ((rc=apply_controls())!=RC_NO_RETURN_VALUE) 762 return rc; 763 } 764 if (count>0) 765 { 766 do_compute_data(count); 767 buffered_count += count; 768 buffer_pointer += (play_mode->encoding & PE_MONO) ? count : count*2; 769 } 770 return RC_NO_RETURN_VALUE; 771 } 772 773 int Timidity_PlaySome(void *stream, int samples, int* bytes_written) 774 { 775 int rc = RC_NO_RETURN_VALUE; 776 int32_t end_sample; 777 bool endSong = false; 778 779 if (bytes_written) 780 *bytes_written = 0; 781 782 if ( ! midi_playing ){ 783 return RC_NO_RETURN_VALUE; 784 } 785 end_sample = current_sample+samples; 786 787 while ( current_sample < end_sample ){ 788 789 /* Handle all events that should happen at this time */ 790 while ( !endSong && current_event->time <= current_sample){ 791 switch(current_event->type){ 792 793 /* Effects affecting a single note */ 794 case ME_NOTEON: 795 if (!(current_event->b)) /* Velocity 0? */ 796 note_off(current_event); 797 else 798 note_on(current_event); 799 break; 800 801 case ME_NOTEOFF: 802 note_off(current_event); 803 break; 804 805 case ME_KEYPRESSURE: 806 adjust_pressure(current_event); 807 break; 808 809 /* Effects affecting a single channel */ 810 811 case ME_PITCH_SENS: 812 channel[current_event->channel].pitchsens=current_event->a; 813 channel[current_event->channel].pitchfactor=0; 814 break; 815 816 case ME_PITCHWHEEL: 817 channel[current_event->channel].pitchbend= 818 current_event->a + current_event->b * 128; 819 channel[current_event->channel].pitchfactor=0; 820 /* Adjust pitch for notes already playing */ 821 adjust_pitchbend(current_event->channel); 822 ctl->pitch_bend(current_event->channel, 823 channel[current_event->channel].pitchbend); 824 break; 825 826 case ME_MAINVOLUME: 827 channel[current_event->channel].volume=current_event->a; 828 adjust_volume(current_event->channel); 829 ctl->volume(current_event->channel, current_event->a); 830 break; 831 832 case ME_PAN: 833 channel[current_event->channel].panning=current_event->a; 834 if (adjust_panning_immediately) 835 adjust_panning(current_event->channel); 836 ctl->panning(current_event->channel, current_event->a); 837 break; 838 839 case ME_EXPRESSION: 840 channel[current_event->channel].expression=current_event->a; 841 adjust_volume(current_event->channel); 842 ctl->expression(current_event->channel, current_event->a); 843 break; 844 845 case ME_PROGRAM: 846 if (ISDRUMCHANNEL(current_event->channel)){ 847 /* Change drum set */ 848 channel[current_event->channel].bank=current_event->a; 849 } 850 else 851 { 852 channel[current_event->channel].program=current_event->a; 853 } 854 ctl->program(current_event->channel, current_event->a); 855 break; 856 857 case ME_SUSTAIN: 858 channel[current_event->channel].sustain=current_event->a; 859 if (!current_event->a) 860 drop_sustain(current_event->channel); 861 ctl->sustain(current_event->channel, current_event->a); 862 break; 863 864 case ME_RESET_CONTROLLERS: 865 reset_controllers(current_event->channel); 866 redraw_controllers(current_event->channel); 867 break; 868 869 case ME_ALL_NOTES_OFF: 870 all_notes_off(current_event->channel); 871 break; 872 873 case ME_ALL_SOUNDS_OFF: 874 all_sounds_off(current_event->channel); 875 break; 876 877 case ME_TONE_BANK: 878 channel[current_event->channel].bank=current_event->a; 879 break; 880 881 case ME_EOT: 882 /* Give the last notes a couple of seconds to decay */ 883 //ctl->cmsg(CMSG_INFO, VERB_VERBOSE, 884 // "Playing time: ~%d seconds, ", current_sample/play_mode->rate+2); 885 //ctl->cmsg(CMSG_INFO, VERB_VERBOSE, 886 // "Notes cut: %d, ", cut_notes); 887 //ctl->cmsg(CMSG_INFO, VERB_VERBOSE, 888 // "Notes lost totally: %d", lost_notes); 889 890 midi_playing = 0; 891 rc = RC_TUNE_END; 892 endSong = true; 893 894 current_event--; 895 } 896 897 current_event++; 898 } 899 if (current_event->time > end_sample) 900 rc=compute_data(stream, end_sample-current_sample, bytes_written); 901 else 902 rc=compute_data(stream, current_event->time-current_sample, bytes_written); 903 ctl->refresh(); 904 if ( endSong || ((rc!=RC_NO_RETURN_VALUE) && (rc!=RC_JUMP))) 905 break; 906 } 907 908 if ( endSong ) { 909 return RC_TUNE_END; 910 } 911 return rc; 912 } 913 914 915 void Timidity_SetVolume(int volume) 916 { 917 int i; 918 if (volume > MAX_AMPLIFICATION) 919 amplification=MAX_AMPLIFICATION; 920 else 921 if (volume < 0) 922 amplification=0; 923 else 924 amplification=volume; 925 adjust_amplification(); 926 for (i=0; i<voices; i++) 927 if (voice[i].status != VOICE_FREE) 928 { 929 recompute_amp(i); 930 apply_envelope_to_amp(i); 931 } 932 ctl->master_volume(amplification); 933 } 934 935 MidiSong *Timidity_LoadSong(char *midifile) 936 { 937 MidiSong *song; 938 int32_t events; 939 idFile * fp; 940 941 /* Allocate memory for the song */ 942 song = (MidiSong *)safe_malloc(sizeof(*song)); 943 memset(song, 0, sizeof(*song)); 944 945 /* Open the file */ 946 fp = open_file(midifile, 1, OF_VERBOSE); 947 if ( fp != NULL ) { 948 song->events=read_midi_file(fp, &events, &song->samples); 949 close_file(fp); 950 } 951 952 /* Make sure everything is okay */ 953 if (!song->events) { 954 Real_Tim_Free(song); 955 song = NULL; 956 ctl->cmsg(CMSG_WARNING, VERB_NORMAL, "Song had null events! Returning NULL."); 957 } 958 return(song); 959 } 960 961 MidiSong *Timidity_LoadSongMem(unsigned char* buffer, size_t length) 962 { 963 MidiSong *song; 964 int32_t events; 965 966 song = (MidiSong *)safe_malloc(sizeof(*song)); 967 memset(song, 0, sizeof(*song)); 968 969 song->events = read_midi_buffer(buffer, length, &events, &song->samples); 970 971 if (!song->events) { 972 Real_Tim_Free(song); 973 song = NULL; 974 ctl->cmsg(CMSG_WARNING, VERB_NORMAL, "Song had null events! Returning NULL."); 975 } 976 return(song); 977 } 978 979 void Timidity_Start(MidiSong *song) 980 { 981 load_missing_instruments(); 982 adjust_amplification(); 983 sample_count = song->samples; 984 event_list = song->events; 985 lost_notes=cut_notes=0; 986 987 skip_to(0); 988 midi_playing = 1; 989 } 990 991 int Timidity_Active(void) 992 { 993 return(midi_playing); 994 } 995 996 void Timidity_Stop(void) 997 { 998 midi_playing = 0; 999 } 1000 1001 void Timidity_FreeSong(MidiSong *song) 1002 { 1003 //if (free_instruments_afterwards) 1004 //free_instruments(); 1005 1006 Real_Tim_Free(song->events); 1007 Real_Tim_Free(song); 1008 } 1009 1010 extern sample_t *resample_buffer; 1011 extern int32_t *common_buffer; 1012 1013 void Timidity_Shutdown(void) { 1014 free_instruments(); 1015 1016 Real_Tim_Free( resample_buffer ); 1017 Real_Tim_Free( common_buffer ); 1018 }