···381381382382struct MIDIfile * mf IBSS_ATTR;
383383384384+int sample_rate IBSS_ATTR;
385385+int max_voices IBSS_ATTR;
384386int number_of_samples IBSS_ATTR; /* the number of samples in the current tick */
385387int playing_time IBSS_ATTR; /* How many seconds into the file have we been playing? */
386388int samples_this_second IBSS_ATTR; /* How many samples produced during this second so far? */
···490492 *size = samples_in_buf*sizeof(int32_t);
491493}
492494495495+static const struct mixer_play_cbs mixer_cbs = {
496496+ .get_more = get_more,
497497+ /* TODO: update sample_rate and max_voices on sampr_changed() */
498498+};
499499+500500+UNUSED_ATTR static int find_min_sampr_ge_22(void)
501501+{
502502+ const struct pcm_sink_caps* caps = rb->pcm_current_sink_caps();
503503+ int ret = caps->samprs[0];
504504+ for (size_t i = 1; i < caps->num_samprs; i += 1)
505505+ {
506506+ /* caps->samprs is in descending order */
507507+ if (caps->samprs[i] >= SAMPR_22)
508508+ ret = caps->samprs[i];
509509+ else
510510+ break;
511511+ }
512512+ return ret;
513513+}
514514+493515static int midimain(const void * filename)
494516{
495517 int a, notes_used, vol;
496518 bool is_playing = true; /* false = paused */
497519520520+ /* decide sample_rate and max_voices */
521521+#if defined(SIMULATOR) /* Simulator requires 44100Hz, and we can afford to use more voices */ || \
522522+ (CONFIG_PLATFORM & PLATFORM_HOSTED) /* All hosted targets have CPU to spare */ || \
523523+ defined(CPU_MIPS) /* All MIPS targets are pretty fast */
524524+ sample_rate = SAMPR_44;
525525+ max_voices = 48;
526526+#elif defined(CPU_PP)
527527+ /* Some of the pp based targets can't handle too many voices
528528+ mainly because they have to use 44100Hz sample rate, this could be
529529+ improved to increase max_voices for targets that can do 22kHz */
530530+ sample_rate = find_min_sampr_ge_22();
531531+ max_voices = sample_rate == SAMPR_22 ? 24 : 16;
532532+#elif defined(CPU_ARM)
533533+ /* ARMv4 targets are slow, but treat everything else as fast */
534534+#if (ARM_ARCH >= 6)
535535+ sample_rate = SAMPR_44;
536536+ max_voices = 32;
537537+#elif (ARM_ARCH >= 5)
538538+ sample_rate = find_min_sampr_ge_22();
539539+ max_voices = 32;
540540+#else /* ie v4 */
541541+ sample_rate = find_min_sampr_ge_22();
542542+ max_voices = sample_rate == SAMPR_22 ? 24 : 16;
543543+#endif
544544+#else /* !CPU_ARM */
545545+ /* Treat everything else as slow */
546546+ sample_rate = find_min_sampr_ge_22();
547547+ max_voices = sample_rate == SAMPR_22 ? 24 : 16;
548548+#endif
549549+498550#if defined(HAVE_ADJUSTABLE_CPU_FREQ)
499551 rb->cpu_boost(true);
500552#endif
···537589 rb->dsp_set_timestretch(PITCH_SPEED_100);
538590#endif
539591 rb->dsp_configure(dsp, DSP_SET_SAMPLE_DEPTH, 22);
540540- rb->dsp_configure(dsp, DSP_SET_FREQUENCY, SAMPLE_RATE); /* 44100 22050 11025 */
592592+ rb->dsp_configure(dsp, DSP_SET_FREQUENCY, sample_rate); /* 44100 22050 11025 */
541593 rb->dsp_configure(dsp, DSP_SET_STEREO_MODE, STEREO_INTERLEAVED);
542594543595 /*
···553605 midi_debug("Okay, starting sequencing");
554606555607 bpm = mf->div*1000000/tempo;
556556- number_of_samples = SAMPLE_RATE/bpm;
608608+ number_of_samples = sample_rate/bpm;
557609558610 /* Skip over any junk in the beginning of the file, so start playing */
559611 /* after the first note event */
560612 do
561613 {
562614 notes_used = 0;
563563- for (a = 0; a < MAX_VOICES; a++)
615615+ for (a = 0; a < max_voices; a++)
564616 if (voices[a].isUsed)
565617 notes_used++;
566618 tick();