int aq_soft_flush(void) { int rc; while(head) { if(head->len < bucket_size) { /* Add silence code */ memset (head->data + head->len, 0, bucket_size - head->len); head->len = bucket_size; } if(aq_fill_one() == -1) return RC_ERROR; trace_loop(); rc = check_apply_control(); if(RC_IS_SKIP_FILE(rc)) { play_mode->acntl(PM_REQ_DISCARD, NULL); flush_buckets(); return rc; } } play_mode->acntl(PM_REQ_OUTPUT_FINISH, NULL); return RC_NONE; }
static int wait_event_time(void *p) { MidiEvent *ev = (MidiEvent *)p; int rc, ch; for(;;){ if(ev->time - (current_tick()*play_mode->rate+30)/60 < 0) break; trace_loop(); YieldToAnyThread(); rc = check_apply_control(); if(RC_IS_SKIP_FILE(rc)){ prescan = 0; for(ch = 0; ch < MAX_CHANNELS; ch++){ if(note_channel[ch] != NULL){ NADisposeNoteChannel(gNoteAllocator, note_channel[ch]); note_channel[ch] = NULL; } channel[ch].bank_lsb = 0; channel[ch].bank_msb = 0; } trace_flush(); return rc; } } return RC_NONE; }
static int acntl(int request, void *arg) { int rc, ch; switch(request) { case PM_REQ_MIDI: if(!prescan) play_event_prescan(arg); rc = wait_event_time(arg); if(RC_IS_SKIP_FILE(rc)) return rc; qt_play_event(arg); return RC_NONE; case PM_REQ_INST_NAME: ch = (int)*(char **)arg; *(char **)arg = instrument_name[ch]; return 0; case PM_REQ_GETSAMPLES: *(int32 *)arg = current_samples(); return 0; case PM_REQ_PLAY_START: init_variable(); case PM_REQ_DISCARD: case PM_REQ_FLUSH: for(ch = 0; ch < MAX_CHANNELS; ch++){ if(note_channel[ch] != NULL){ NADisposeNoteChannel(gNoteAllocator, note_channel[ch]); note_channel[ch] = NULL; } channel[ch].bank_lsb = 0; channel[ch].bank_msb = 0; } trace_flush(); return 0; } return -1; }
int aq_flush(int discard) { int rc; int more_trace; /* to avoid infinite loop */ double t, timeout_expect; aq_add_count = 0; init_effect(); if(discard) { trace_flush(); if(play_mode->acntl(PM_REQ_DISCARD, NULL) != -1) { flush_buckets(); return RC_NONE; } ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "ERROR: Can't discard audio buffer"); } if(!IS_STREAM_TRACE) { play_mode->acntl(PM_REQ_FLUSH, NULL); play_counter = play_offset_counter = 0; return RC_NONE; } rc = aq_soft_flush(); if(RC_IS_SKIP_FILE(rc)) return rc; more_trace = 1; t = get_current_calender_time(); timeout_expect = t + (double)aq_filled() / play_mode->rate; while(more_trace || aq_filled() > 0) { rc = check_apply_control(); if(RC_IS_SKIP_FILE(rc)) { play_mode->acntl(PM_REQ_DISCARD, NULL); flush_buckets(); return rc; } more_trace = trace_loop(); t = get_current_calender_time(); if(t >= timeout_expect - 0.1) break; if(!more_trace) usleep((unsigned long)((timeout_expect - t) * 1000000)); else aq_wait_ticks(); } trace_flush(); play_mode->acntl(PM_REQ_FLUSH, NULL); flush_buckets(); return RC_NONE; }