2 TiMidity++ -- MIDI to WAVE converter and player
3 Copyright (C) 1999-2002 Masanao Izumo <mo@goice.co.jp>
4 Copyright (C) 1995 Tuukka Toivonen <tt@cgs.fi>
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 Written by Masanao Izumo <mo@goice.co.jp>
26 #endif /* HAVE_CONFIG_H */
29 #include <sys/types.h>
35 #endif /* HAVE_UNISTD_H */
49 #include "miditrace.h"
54 #define TEST_SPARE_RATE 0.9
55 #define MAX_BUCKET_TIME 0.5
57 #define MAX_FILLED_TIME 3000.0 // def 2.0
60 static int32 device_qsize;
62 static int32 Bps = 1; /* Bytes per sample frame */
63 static int32 bucket_size;
64 static int32 nbuckets = 0;
65 static double bucket_time;
66 int32 aq_fill_buffer_flag = 0;
67 static int32 aq_start_count;
68 static int32 aq_add_count;
70 static int32 play_counter, play_offset_counter;
71 static double play_start_time;
73 typedef struct _AudioBucket
77 struct _AudioBucket *next;
80 static AudioBucket *base_buckets = NULL;
81 static AudioBucket *allocated_bucket_list = NULL;
82 static AudioBucket *head = NULL;
83 static AudioBucket *tail = NULL;
85 static void alloc_soft_queue(void);
86 static void set_bucket_size(int32 size);
87 static int32 add_play_bucket(const uint8 *buf, int32 n);
88 static void reuse_audio_bucket(AudioBucket *bucket);
89 static AudioBucket *next_allocated_bucket(void);
90 static void flush_buckets(void);
91 static int32 aq_fill_one(void);
92 static void aq_wait_ticks(void);
93 static int32 estimate_queue_size(void);
96 extern void init_effect(void);
97 extern void do_effect(DATA_T *buf, int32 count);
101 int aq_calc_fragsize(void)
106 bps = get_encoding_sample_size(play_mode->encoding);
107 bs = audio_buffer_size * bps;
108 dq = play_mode->rate * MAX_FILLED_TIME * bps;
112 bt = (double)bs / bps * div_playmode_rate;
113 while(bt > MAX_BUCKET_TIME)
116 bt = (double)bs / bps * div_playmode_rate;
126 /* Initialize Bps, bucket_size, device_qsize, and bucket_time */
128 Bps = get_encoding_sample_size(play_mode->encoding);
129 general_output_convert_setup();
131 if (play_mode->acntl(PM_REQ_GETFRAGSIZ, &frag_size) == -1)
132 frag_size = audio_buffer_size * Bps;
133 set_bucket_size(frag_size);
134 bucket_time = (double)bucket_size / Bps * div_playmode_rate;
138 if (play_mode->acntl(PM_REQ_GETQSIZ, &device_qsize) == -1)
139 device_qsize = estimate_queue_size();
140 if (bucket_size * 2 > device_qsize) {
141 ctl->cmsg(CMSG_WARNING, VERB_VERBOSE,
142 "Warning: Audio buffer is too small.");
145 device_qsize -= device_qsize % Bps; /* Round Bps */
146 ctl->cmsg(CMSG_INFO, VERB_DEBUG,
147 "Audio device queue size: %d bytes", device_qsize);
148 ctl->cmsg(CMSG_INFO, VERB_DEBUG,
149 "Write bucket size: %d bytes (%d msec)",
150 bucket_size, (int)(bucket_time * 1000 + 0.5));
164 void aq_set_soft_queue(double soft_buff_time, double fill_start_time)
166 static double last_soft_buff_time, last_fill_start_time;
169 /* for re-initialize */
170 if(soft_buff_time < 0)
171 soft_buff_time = last_soft_buff_time;
172 if(fill_start_time < 0)
173 fill_start_time = last_fill_start_time;
175 nb = (int)(soft_buff_time / bucket_time);
179 aq_start_count = (int32)(fill_start_time * play_mode->rate);
180 aq_fill_buffer_flag = (aq_start_count > 0);
188 last_soft_buff_time = soft_buff_time;
189 last_fill_start_time = fill_start_time;
192 /* Estimates the size of audio device queue.
193 * About sun audio, there are long-waiting if buffer of device audio is full.
194 * So it is impossible to completely estimate the size.
196 static int32 estimate_queue_size(void)
199 double tb, init_time, chunktime;
200 int32 qbytes, max_qbytes;
204 nullsound = (uint8*) aligned_malloc(bucket_size, ALIGN_SIZE);
206 nullsound = (uint8*) safe_malloc(bucket_size);
208 memset(nullsound, 0, bucket_size);
209 if (play_mode->encoding & (PE_ULAW | PE_ALAW))
210 general_output_convert((DATA_T*)nullsound, bucket_size / Bps);
211 tb = play_mode->rate * Bps * TEST_SPARE_RATE;
213 max_qbytes = play_mode->rate * MAX_FILLED_TIME * Bps;
216 chunktime = (double)bucket_size / Bps * div_playmode_rate;
219 init_time = get_current_calender_time(); /* Start */
224 start = get_current_calender_time();
225 if(start - init_time > 1.0) /* ?? */
227 ctl->cmsg(CMSG_WARNING, VERB_DEBUG,
228 "Warning: Audio test is terminated");
231 play_mode->output_data(nullsound, bucket_size);
232 diff = get_current_calender_time() - start;
234 if(diff > chunktime * DIV_2 || chunktime < diff)
235 // if(qbytes > 1024*512) // ??
237 qbytes += (int32)((chunktime - diff) * tb);
239 if(qbytes > max_qbytes)
245 play_mode->acntl(PM_REQ_DISCARD, NULL);
247 if(bucket_size * 2 > qbytes)
251 ctl->cmsg(CMSG_ERROR, VERB_NOISY,
252 "Can't estimate audio queue length");
253 set_bucket_size(audio_buffer_size * Bps);
255 return 2 * audio_buffer_size * Bps;
258 ctl->cmsg(CMSG_WARNING, VERB_DEBUG,
259 "Retry to estimate audio queue length (%d times)",
261 set_bucket_size(divi_2(bucket_size));
267 aligned_free(nullsound);
275 /* Send audio data to play_mode->output_data() */
276 static int aq_output_data(uint8 *buff, int nbytes)
280 play_counter += nbytes / Bps;
287 if(play_mode->output_data(buff, i) == -1)
296 extern void do_effect2(uint8 *buf, int32 count); // effect.c
298 int aq_add(DATA_T *samples, int32 count)
303 if(!(play_mode->flag & PF_PCM_STREAM))
308 if(!aq_fill_buffer_flag)
309 return aq_fill_nonblocking();
313 aq_add_count += count;
314 nbytes = general_output_convert(samples, count);
315 buff = (uint8 *)samples;
316 do_effect2(buff, count);
318 if(device_qsize == 0 || nbuckets == 0)
319 return play_mode->output_data(buff, nbytes);
321 aq_fill_buffer_flag = (aq_add_count <= aq_start_count);
323 if(!aq_fill_buffer_flag && aq_fill_nonblocking() == -1)
326 if(!ctl->trace_playing)
328 while((i = add_play_bucket(buff, nbytes)) < nbytes)
332 if(head && head->len == bucket_size)
334 if(aq_fill_one() == -1)
337 aq_fill_buffer_flag = 0;
343 while((i = add_play_bucket(buff, nbytes)) < nbytes)
345 /* Software buffer is full.
346 * Write one bucket to audio device.
352 if(aq_fill_nonblocking() == -1)
354 aq_fill_buffer_flag = 0;
359 static void set_bucket_size(int32 size)
361 // if (size == bucket_size)
364 // if (nbuckets != 0)
368 /* alloc_soft_queue() (re-)initializes audio buckets. */
369 static void alloc_soft_queue(void)
373 int32 nb = nbuckets > 1 ? nbuckets : 1;
376 ///r def safe_malloc(
377 base_buckets = (AudioBucket*) safe_large_malloc(nb * sizeof(AudioBucket));
378 base = (uint8*) safe_large_malloc(nb * bucket_size);
379 base_buckets[0].len = 0;
380 base_buckets[0].next = 0;
381 for (i = 0; i < nb; i++)
382 base_buckets[i].data = base + i * bucket_size;
386 void free_soft_queue(void)
390 free(base_buckets[0].data);
396 /* aq_fill_one() transfers one audio bucket to device. */
397 static int aq_fill_one(void)
403 if(aq_output_data(head->data, bucket_size) == -1)
407 reuse_audio_bucket(tmp);
411 /* aq_fill_nonblocking() transfers some audio buckets to device.
412 * This function is non-blocking. But it is possible to block because
413 * of miss-estimated aq_fillable() calculation.
415 int aq_fill_nonblocking(void)
420 if(!head || head->len != bucket_size || !IS_STREAM_TRACE)
423 nfills = (aq_fillable() * Bps) / bucket_size;
424 for(i = 0; i < nfills; i++)
426 if(!head || head->len != bucket_size)
428 if(aq_output_data(head->data, bucket_size) == -1)
432 reuse_audio_bucket(tmp);
437 int32 aq_samples(void)
442 if(play_mode->acntl(PM_REQ_GETSAMPLES, &s) != -1)
444 /* Reset counter & timer */
447 play_start_time = get_current_calender_time();
448 play_offset_counter = s;
457 realtime = get_current_calender_time();
458 if(play_counter == 0)
460 play_start_time = realtime;
461 return play_offset_counter;
463 es = play_mode->rate * (realtime - play_start_time);
465 if(es >= play_counter)
468 * Audio device queue may be empty!
471 play_offset_counter += play_counter;
473 play_start_time = realtime;
474 return play_offset_counter;
477 return (int32)es + play_offset_counter;
480 int32 aq_filled(void)
488 if(play_mode->acntl(PM_REQ_GETFILLED, &filled) != -1)
491 realtime = get_current_calender_time();
492 if(play_counter == 0)
494 play_start_time = realtime;
497 es = play_mode->rate * (realtime - play_start_time);
498 if(es >= play_counter)
500 /* out of play counter */
501 play_offset_counter += play_counter;
503 play_start_time = realtime;
506 return play_counter - (int32)es;
509 int32 aq_soft_filled(void)
515 for(cur = head; cur != NULL; cur = cur->next)
520 int32 aq_fillable(void)
525 if(play_mode->acntl(PM_REQ_GETFILLABLE, &fillable) != -1)
527 return device_qsize / Bps - aq_filled();
530 double aq_filled_ratio(void)
536 ratio = (double)aq_filled() * Bps / device_qsize;
538 return 1.0; /* for safety */
542 int aq_get_dev_queuesize(void)
546 return device_qsize / Bps;
549 int aq_soft_flush(void)
555 if(head->len < bucket_size)
557 /* Add silence code */
558 memset (head->data + head->len, 0, bucket_size - head->len);
559 head->len = bucket_size;
561 if(aq_fill_one() == -1)
564 rc = check_apply_control();
565 if(RC_IS_SKIP_FILE(rc))
567 play_mode->acntl(PM_REQ_DISCARD, NULL);
572 play_mode->acntl(PM_REQ_OUTPUT_FINISH, NULL);
576 int aq_flush(int discard)
581 /* to avoid infinite loop */
582 double t, timeout_expect;
590 if(play_mode->acntl(PM_REQ_DISCARD, NULL) != -1)
595 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
596 "ERROR: Can't discard audio buffer");
601 play_mode->acntl(PM_REQ_FLUSH, NULL);
602 play_counter = play_offset_counter = 0;
606 rc = aq_soft_flush();
608 if(RC_IS_SKIP_FILE(rc)){
613 t = get_current_calender_time();
614 timeout_expect = t + (double)aq_filled() * div_playmode_rate;
616 while(more_trace || aq_filled() > 0)
618 rc = check_apply_control();
619 if(RC_IS_SKIP_FILE(rc))
621 play_mode->acntl(PM_REQ_DISCARD, NULL);
625 more_trace = trace_loop();
627 t = get_current_calender_time();
628 if(t >= timeout_expect - 0.1)
632 const uint32 max_wait = 200 * 1000;
633 uint32 wait = (timeout_expect - t) * 100000;
634 // usleep((uint32)((double)aq_filled() * div_playmode_rate * 1000000.0)); // while aq_filled() > 0
635 // usleep((unsigned long)(200000)); //
\82Ä
\82«
\82Æ
\81[
\82È
\8cÅ
\92è
\92l 20ms
636 usleep(wait < max_wait ? wait : max_wait);
641 play_mode->acntl(PM_REQ_FLUSH, NULL);
647 static void aq_wait_ticks(void)
649 int32 trace_wait, wait_samples;
651 #ifdef USE_TRACE_TIMER
654 trace_wait = trace_wait_samples();
655 if(device_qsize == 0 || trace_wait == 0)
656 return; /* No wait */
658 wait_samples = (device_qsize / Bps) * 0.2; // def 20%
659 if(trace_wait != -1 && trace_wait < wait_samples) /* There are more trace events */
660 wait_samples = trace_wait;
661 usleep((unsigned int)((double)wait_samples * div_playmode_rate * 1000000.0));
664 /* add_play_bucket() attempts to add buf to audio bucket.
665 * It returns actually added bytes.
667 static int32 add_play_bucket(const uint8 *buf, int n)
675 play_mode->output_data((uint8 *)buf, n);
676 aq_output_data((uint8*)buf, n);
681 head = tail = next_allocated_bucket();
688 if(tail->len == bucket_size)
691 if((b = next_allocated_bucket()) == NULL)
696 tail = tail->next = b;
699 i = bucket_size - tail->len;
702 memcpy(tail->data + tail->len, buf + total, i);
711 /* Flush and clear audio bucket */
712 static void flush_buckets(void)
716 allocated_bucket_list = NULL;
717 for(i = 0; i < nbuckets; i++)
718 reuse_audio_bucket(&base_buckets[i]);
720 aq_fill_buffer_flag = (aq_start_count > 0);
721 play_counter = play_offset_counter = 0;
724 /* next_allocated_bucket() gets free bucket. If all buckets is used, it
727 static AudioBucket *next_allocated_bucket(void)
731 if (!allocated_bucket_list)
733 b = allocated_bucket_list;
734 allocated_bucket_list = allocated_bucket_list->next;
740 /* Reuse specified bucket */
741 static void reuse_audio_bucket(AudioBucket *bucket)
744 bucket->next = allocated_bucket_list;
745 allocated_bucket_list = bucket;
750 void free_audio_bucket(void)