1
0
Fork 0
forked from len0rd/rockbox

midi: yield more, fixes flickering backlight fade on some targets while playing midi. Some coding style clean up, it's a little better but still a mess

git-svn-id: svn://svn.rockbox.org/rockbox/trunk@20562 a1c6a512-1295-4272-9138-f99709370657
This commit is contained in:
Nils Wallménius 2009-03-28 11:27:56 +00:00
parent 24ae50699d
commit 1adc869d9c
3 changed files with 321 additions and 353 deletions

View file

@ -29,24 +29,7 @@ PLUGIN_HEADER
PLUGIN_IRAM_DECLARE PLUGIN_IRAM_DECLARE
/* variable button definitions */ /* variable button definitions */
#if CONFIG_KEYPAD == RECORDER_PAD #if (CONFIG_KEYPAD == IRIVER_H100_PAD) || (CONFIG_KEYPAD == IRIVER_H300_PAD)
#define BTN_QUIT BUTTON_OFF
#define BTN_RIGHT BUTTON_RIGHT
#define BTN_UP BUTTON_UP
#define BTN_DOWN BUTTON_DOWN
#define BTN_LEFT BUTTON_LEFT
#define BTN_PLAY BUTTON_PLAY
#elif CONFIG_KEYPAD == ONDIO_PAD
#define BTN_QUIT BUTTON_OFF
#define BTN_RIGHT BUTTON_RIGHT
#define BTN_UP BUTTON_UP
#define BTN_DOWN BUTTON_DOWN
#define BTN_LEFT BUTTON_LEFT
#define BTN_PLAY (BUTTON_MENU | BUTTON_OFF)
#elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || (CONFIG_KEYPAD == IRIVER_H300_PAD)
#define BTN_QUIT BUTTON_OFF #define BTN_QUIT BUTTON_OFF
#define BTN_RIGHT BUTTON_RIGHT #define BTN_RIGHT BUTTON_RIGHT
#define BTN_UP BUTTON_UP #define BTN_UP BUTTON_UP
@ -209,24 +192,220 @@ PLUGIN_IRAM_DECLARE
struct MIDIfile * mf IBSS_ATTR; struct MIDIfile * mf IBSS_ATTR;
int numberOfSamples IBSS_ATTR; /* the number of samples in the current tick */ int number_of_samples IBSS_ATTR; /* the number of samples in the current tick */
int playingTime IBSS_ATTR; /* How many seconds into the file have we been playing? */ int playing_time IBSS_ATTR; /* How many seconds into the file have we been playing? */
int samplesThisSecond IBSS_ATTR; /* How many samples produced during this second so far? */ int samples_this_second IBSS_ATTR; /* How many samples produced during this second so far? */
long bpm IBSS_ATTR; long bpm IBSS_ATTR;
int32_t gmbuf[BUF_SIZE*NBUF]; int32_t gmbuf[BUF_SIZE*NBUF];
static unsigned int samples_in_buf; static unsigned int samples_in_buf;
int quit=0; bool quit = false;
bool swap = false;
bool lastswap = true;
static int midimain(const void * filename); static inline void synthbuf(void)
{
int32_t *outptr;
int i = BUF_SIZE;
#ifndef SYNC
if (lastswap == swap)
return;
lastswap = swap;
outptr = (swap ? gmbuf : gmbuf+BUF_SIZE);
#else
outptr = gmbuf;
#endif
/* synth samples for as many whole ticks as we can fit in the buffer */
for (; i >= number_of_samples; i -= number_of_samples)
{
synthSamples((int32_t*)outptr, number_of_samples);
outptr += number_of_samples;
#ifndef SYNC
/* synthbuf is called in interrupt context is SYNC is defined so it cannot yield
that bug causing the sim to crach when not using SYNC should really be fixed */
rb->yield();
#endif
if (tick() == 0)
quit = true;
}
/* how many samples did we write to the buffer? */
samples_in_buf = BUF_SIZE-i;
}
void get_more(unsigned char** start, size_t* size)
{
#ifndef SYNC
if(lastswap != swap)
{
printf("Buffer miss!"); /* Comment out the printf to make missses less noticable. */
}
#else
synthbuf(); /* For some reason midiplayer crashes when an update is forced */
#endif
*size = samples_in_buf*sizeof(int32_t);
#ifndef SYNC
*start = (unsigned char*)((swap ? gmbuf : gmbuf + BUF_SIZE));
swap = !swap;
#else
*start = (unsigned char*)(gmbuf);
#endif
}
static int midimain(const void * filename)
{
int a, notes_used, vol;
bool is_playing = true; /* false = paused */
printf("Loading file");
mf = loadFile(filename);
if (mf == NULL)
{
printf("Error loading file.");
return -1;
}
if (initSynth(mf, ROCKBOX_DIR "/patchset/patchset.cfg",
ROCKBOX_DIR "/patchset/drums.cfg") == -1)
return -1;
rb->pcm_play_stop();
#if INPUT_SRC_CAPS != 0
/* Select playback */
rb->audio_set_input_source(AUDIO_SRC_PLAYBACK, SRCF_PLAYBACK);
rb->audio_set_output_source(AUDIO_SRC_PLAYBACK);
#endif
rb->pcm_set_frequency(SAMPLE_RATE); /* 44100 22050 11025 */
/*
* tick() will do one MIDI clock tick. Then, there's a loop here that
* will generate the right number of samples per MIDI tick. The whole
* MIDI playback is timed in terms of this value.. there are no forced
* delays or anything. It just produces enough samples for each tick, and
* the playback of these samples is what makes the timings right.
*
* This seems to work quite well. On a laptop, anyway.
*/
printf("Okay, starting sequencing");
bpm = mf->div*1000000/tempo;
number_of_samples = SAMPLE_RATE/bpm;
/* Skip over any junk in the beginning of the file, so start playing */
/* after the first note event */
do
{
notes_used = 0;
for (a = 0; a < MAX_VOICES; a++)
if (voices[a].isUsed)
notes_used++;
tick();
} while (notes_used == 0);
playing_time = 0;
samples_this_second = 0;
synthbuf();
rb->pcm_play_data(&get_more, NULL, 0);
while (!quit)
{
#ifndef SYNC
synthbuf();
#endif
rb->yield();
/* Prevent idle poweroff */
rb->reset_poweroff_timer();
/* Code taken from Oscilloscope plugin */
switch (rb->button_get(false))
{
case BTN_UP:
case BTN_UP | BUTTON_REPEAT:
{
vol = rb->global_settings->volume;
if (vol < rb->sound_max(SOUND_VOLUME))
{
vol++;
rb->sound_set(SOUND_VOLUME, vol);
rb->global_settings->volume = vol;
}
break;
}
case BTN_DOWN:
case BTN_DOWN | BUTTON_REPEAT:
{
vol = rb->global_settings->volume;
if (vol > rb->sound_min(SOUND_VOLUME))
{
vol--;
rb->sound_set(SOUND_VOLUME, vol);
rb->global_settings->volume = vol;
}
break;
}
case BTN_LEFT:
{
/* Rewinding is tricky. Basically start the file over */
/* but run through the tracks without the synth running */
rb->pcm_play_stop();
seekBackward(5);
printf("Rewind to %d:%02d\n", playing_time/60, playing_time%60);
if (is_playing)
rb->pcm_play_data(&get_more, NULL, 0);
break;
}
case BTN_RIGHT:
{
rb->pcm_play_stop();
seekForward(5);
printf("Skip to %d:%02d\n", playing_time/60, playing_time%60);
if (is_playing)
rb->pcm_play_data(&get_more, NULL, 0);
break;
}
case BTN_PLAY:
{
if (is_playing)
{
printf("Paused at %d:%02d\n", playing_time/60, playing_time%60);
is_playing = false;
rb->pcm_play_stop();
} else
{
printf("Playing from %d:%02d\n", playing_time/60, playing_time%60);
is_playing = true;
rb->pcm_play_data(&get_more, NULL, 0);
}
break;
}
#ifdef BTN_RC_QUIT
case BTN_RC_QUIT:
#endif
case BTN_QUIT:
quit = true;
}
}
return 0;
}
enum plugin_status plugin_start(const void* parameter) enum plugin_status plugin_start(const void* parameter)
{ {
int retval = 0; int retval;
PLUGIN_IRAM_INIT(rb) PLUGIN_IRAM_INIT(rb)
if (parameter == NULL) if (parameter == NULL)
@ -266,205 +445,3 @@ enum plugin_status plugin_start(const void* parameter)
return PLUGIN_OK; return PLUGIN_OK;
} }
bool swap=0;
bool lastswap=1;
static inline void synthbuf(void)
{
int32_t *outptr;
int i=BUF_SIZE;
#ifndef SYNC
if(lastswap==swap) return;
lastswap=swap;
outptr=(swap ? gmbuf : gmbuf+BUF_SIZE);
#else
outptr=gmbuf;
#endif
/* synth samples for as many whole ticks as we can fit in the buffer */
for(; i >= numberOfSamples; i -= numberOfSamples)
{
synthSamples((int32_t*)outptr, numberOfSamples);
outptr += numberOfSamples;
if( tick() == 0 )
quit=1;
}
/* how many samples did we write to the buffer? */
samples_in_buf = BUF_SIZE-i;
}
void get_more(unsigned char** start, size_t* size)
{
#ifndef SYNC
if(lastswap!=swap)
{
printf("Buffer miss!"); // Comment out the printf to make missses less noticable.
}
#else
synthbuf(); // For some reason midiplayer crashes when an update is forced
#endif
*size = samples_in_buf*sizeof(int32_t);
#ifndef SYNC
*start = (unsigned char*)((swap ? gmbuf : gmbuf + BUF_SIZE));
swap=!swap;
#else
*start = (unsigned char*)(gmbuf);
#endif
}
static int midimain(const void * filename)
{
int notesUsed = 0;
int a=0;
printf("Loading file");
mf= loadFile(filename);
if(mf == NULL)
{
printf("Error loading file.");
return -1;
}
if (initSynth(mf, ROCKBOX_DIR "/patchset/patchset.cfg",
ROCKBOX_DIR "/patchset/drums.cfg") == -1)
return -1;
rb->pcm_play_stop();
#if INPUT_SRC_CAPS != 0
/* Select playback */
rb->audio_set_input_source(AUDIO_SRC_PLAYBACK, SRCF_PLAYBACK);
rb->audio_set_output_source(AUDIO_SRC_PLAYBACK);
#endif
rb->pcm_set_frequency(SAMPLE_RATE); // 44100 22050 11025
/*
* tick() will do one MIDI clock tick. Then, there's a loop here that
* will generate the right number of samples per MIDI tick. The whole
* MIDI playback is timed in terms of this value.. there are no forced
* delays or anything. It just produces enough samples for each tick, and
* the playback of these samples is what makes the timings right.
*
* This seems to work quite well. On a laptop, anyway.
*/
printf("Okay, starting sequencing");
bpm=mf->div*1000000/tempo;
numberOfSamples=SAMPLE_RATE/bpm;
/* Skip over any junk in the beginning of the file, so start playing */
/* after the first note event */
do
{
notesUsed = 0;
for(a=0; a<MAX_VOICES; a++)
if(voices[a].isUsed)
notesUsed++;
tick();
} while(notesUsed == 0);
playingTime = 0;
samplesThisSecond = 0;
synthbuf();
rb->pcm_play_data(&get_more, NULL, 0);
int isPlaying = 1; /* 0 = paused */
int vol=0;
while(!quit)
{
#ifndef SYNC
synthbuf();
#endif
rb->yield();
/* Prevent idle poweroff */
rb->reset_poweroff_timer();
/* Code taken from Oscilloscope plugin */
switch(rb->button_get(false))
{
case BTN_UP:
case BTN_UP | BUTTON_REPEAT:
vol = rb->global_settings->volume;
if (vol < rb->sound_max(SOUND_VOLUME))
{
vol++;
rb->sound_set(SOUND_VOLUME, vol);
rb->global_settings->volume = vol;
}
break;
case BTN_DOWN:
case BTN_DOWN | BUTTON_REPEAT:
vol = rb->global_settings->volume;
if (vol > rb->sound_min(SOUND_VOLUME))
{
vol--;
rb->sound_set(SOUND_VOLUME, vol);
rb->global_settings->volume = vol;
}
break;
case BTN_LEFT:
{
/* Rewinding is tricky. Basically start the file over */
/* but run through the tracks without the synth running */
rb->pcm_play_stop();
seekBackward(5);
printf("Rewind to %d:%02d\n", playingTime/60, playingTime%60);
if(isPlaying)
rb->pcm_play_data(&get_more, NULL, 0);
break;
}
case BTN_RIGHT:
{
rb->pcm_play_stop();
seekForward(5);
printf("Skip to %d:%02d\n", playingTime/60, playingTime%60);
if(isPlaying)
rb->pcm_play_data(&get_more, NULL, 0);
break;
}
case BTN_PLAY:
{
if(isPlaying == 1)
{
printf("Paused at %d:%02d\n", playingTime/60, playingTime%60);
isPlaying = 0;
rb->pcm_play_stop();
} else
{
printf("Playing from %d:%02d\n", playingTime/60, playingTime%60);
isPlaying = 1;
rb->pcm_play_data(&get_more, NULL, 0);
}
break;
}
#ifdef BTN_RC_QUIT
case BTN_RC_QUIT:
#endif
case BTN_QUIT:
quit=1;
}
}
return 0;
}

View file

@ -32,8 +32,10 @@
#define SAMPLE_RATE SAMPR_44 /* 44100 */ #define SAMPLE_RATE SAMPR_44 /* 44100 */
#endif #endif
#ifdef CPU_PP /* the pp based targets can't handle too many voices /* Some of the pp based targets can't handle too many voices
mainly because they have to use 44100Hz sample rate */ mainly because they have to use 44100Hz sample rate, this could be
improved to increase MAX_VOICES for targets that can do 22kHz */
#ifdef CPU_PP
#define MAX_VOICES 16 #define MAX_VOICES 16
#else #else
#define MAX_VOICES 24 /* Note: 24 midi channels is the minimum general midi spec implementation */ #define MAX_VOICES 24 /* Note: 24 midi channels is the minimum general midi spec implementation */
@ -166,6 +168,8 @@ extern struct GPatch * drumSet[128];
extern struct MIDIfile * mf; extern struct MIDIfile * mf;
extern int numberOfSamples; extern int number_of_samples;
extern int playing_time IBSS_ATTR;
extern int samples_this_second IBSS_ATTR;
extern long bpm; extern long bpm;

View file

@ -23,9 +23,6 @@
#include "guspat.h" #include "guspat.h"
#include "synth.h" #include "synth.h"
extern int playingTime IBSS_ATTR;
extern int samplesThisSecond IBSS_ATTR;
long tempo = 375000; long tempo = 375000;
/* From the old patch config.... each patch is scaled. /* From the old patch config.... each patch is scaled.
@ -62,7 +59,7 @@ static inline void setVolScale(int a)
static inline void setVol(int ch, int vol) static inline void setVol(int ch, int vol)
{ {
int a=0; int a;
chVol[ch] = vol; chVol[ch] = vol;
/* If channel volume changes, we need to recalculate the volume scale */ /* If channel volume changes, we need to recalculate the volume scale */
@ -77,8 +74,6 @@ static inline void setPatch(int ch, int pat)
chPat[ch]=pat; chPat[ch]=pat;
} }
/* /*
This is the new pitch bend table. There are 512 entries. This is the new pitch bend table. There are 512 entries.
The middle entry is exactly 65536 - no bending. The middle entry is exactly 65536 - no bending.
@ -143,7 +138,8 @@ const uint32_t pitchTbl[] ICONST_ATTR={
static void findDelta(struct SynthObject * so, int ch, int note) static void findDelta(struct SynthObject * so, int ch, int note)
{ {
struct GWaveform * wf = patchSet[chPat[ch]]->waveforms[patchSet[chPat[ch]]->noteTable[note]]; struct GWaveform * wf =
patchSet[chPat[ch]]->waveforms[patchSet[chPat[ch]]->noteTable[note]];
so->wf = wf; so->wf = wf;
/* Used to be unsigned int, but math had to be done in different order to avoid overflow */ /* Used to be unsigned int, but math had to be done in different order to avoid overflow */
@ -169,7 +165,7 @@ static void findDelta(struct SynthObject * so, int ch, int note)
static inline void computeDeltas(int ch) static inline void computeDeltas(int ch)
{ {
int a=0; int a;
for (a = 0; a < MAX_VOICES; a++) for (a = 0; a < MAX_VOICES; a++)
{ {
if (voices[a].isUsed && voices[a].ch == ch) if (voices[a].isUsed && voices[a].ch == ch)
@ -212,7 +208,7 @@ inline void pressNote(int ch, int note, int vol)
if(ch == 14) return; if(ch == 14) return;
if(ch == 15) return; if(ch == 15) return;
*/ */
int a=0; int a;
for (a = 0; a < MAX_VOICES; a++) for (a = 0; a < MAX_VOICES; a++)
{ {
if (voices[a].ch == ch && voices[a].note == note) if (voices[a].ch == ch && voices[a].note == note)
@ -284,16 +280,15 @@ inline void pressNote(int ch, int note, int vol)
static void releaseNote(int ch, int note) static void releaseNote(int ch, int note)
{ {
if (ch == 9) if (ch == 9)
return; return;
int a=0; int a;
for (a = 0; a < MAX_VOICES; a++) for (a = 0; a < MAX_VOICES; a++)
{ {
if (voices[a].ch == ch && voices[a].note == note) if (voices[a].ch == ch && voices[a].note == note)
{ {
if((voices[a].wf->mode & 28)) if (voices[a].wf->mode & 28)
{ {
setPoint(&voices[a], 3); setPoint(&voices[a], 3);
} }
@ -381,14 +376,14 @@ static void sendEvent(struct Event * ev)
return; return;
case MIDI_PRGM: case MIDI_PRGM:
if((status_low) != 9) if (status_low != 9)
setPatch(status_low, d1); setPatch(status_low, d1);
} }
} }
void rewindFile(void) void rewindFile(void)
{ {
int i=0; int i;
for (i = 0; i < mf->numTracks; i++) for (i = 0; i < mf->numTracks; i++)
{ {
mf->tracks[i]->delta = 0; mf->tracks[i]->delta = 0;
@ -396,65 +391,13 @@ void rewindFile(void)
} }
} }
int tick(void) ICODE_ATTR; int tick(void) ICODE_ATTR;
void seekBackward(int nsec)
{
int notesUsed = 0, a=0;
int desiredTime = playingTime - nsec; /* Rewind 5 sec */
if(desiredTime < 0)
desiredTime = 0;
/* Set controllers to default values */
resetControllers();
/* Set the tempo to defalt */
bpm=mf->div*1000000/tempo;
numberOfSamples=SAMPLE_RATE/bpm;
/* Reset the tracks to start */
rewindFile();
/* Reset the time counter to 0 */
playingTime = 0;
samplesThisSecond = 0;
/* Quickly run through any initial things that occur before notes */
do
{
notesUsed = 0;
for(a=0; a<MAX_VOICES; a++)
if(voices[a].isUsed)
notesUsed++;
tick();
} while(notesUsed == 0);
/* Reset the time counter to 0 */
playingTime = 0;
samplesThisSecond = 0;
/* Tick until goal is reached */
while(playingTime < desiredTime)
tick();
}
void seekForward(int nsec)
{
int desiredTime = playingTime + nsec;
while(tick() && playingTime < desiredTime);
}
int tick(void) int tick(void)
{ {
if (mf == NULL) if (mf == NULL)
return 0; return 0;
int a=0; int a, tracksAdv=0;
int tracksAdv=0;
for (a = 0; a < mf->numTracks; a++) for (a = 0; a < mf->numTracks; a++)
{ {
struct Track * tr = mf->tracks[a]; struct Track * tr = mf->tracks[a];
@ -462,11 +405,9 @@ int tick(void)
if (tr == NULL) if (tr == NULL)
printf("NULL TRACK: %d", a); printf("NULL TRACK: %d", a);
//BIG DEBUG STATEMENT //BIG DEBUG STATEMENT
//printf("\nTrack %2d, Event = %4d of %4d, Delta = %5d, Next = %4d", a, tr->pos, tr->numEvents, tr->delta, getEvent(tr, tr->pos)->delta); //printf("\nTrack %2d, Event = %4d of %4d, Delta = %5d, Next = %4d", a, tr->pos, tr->numEvents, tr->delta, getEvent(tr, tr->pos)->delta);
if (tr != NULL && (tr->pos < tr->numEvents)) if (tr != NULL && (tr->pos < tr->numEvents))
{ {
tr->delta++; tr->delta++;
@ -478,7 +419,7 @@ int tick(void)
if (e->status != 0xFF) if (e->status != 0xFF)
{ {
sendEvent(e); sendEvent(e);
if(((e->status&0xF0) == MIDI_PRGM)) if ((e->status&0xF0) == MIDI_PRGM)
{ {
/* printf("\nPatch Event, patch[%d] ==> %d", e->status&0xF, e->d1); */ /* printf("\nPatch Event, patch[%d] ==> %d", e->status&0xF, e->d1); */
} }
@ -490,7 +431,7 @@ int tick(void)
tempo = (((short)e->evData[0])<<16)|(((short)e->evData[1])<<8)|(e->evData[2]); tempo = (((short)e->evData[0])<<16)|(((short)e->evData[1])<<8)|(e->evData[2]);
/* printf("\nMeta-Event: Tempo Set = %d", tempo); */ /* printf("\nMeta-Event: Tempo Set = %d", tempo); */
bpm=mf->div*1000000/tempo; bpm=mf->div*1000000/tempo;
numberOfSamples=SAMPLE_RATE/bpm; number_of_samples=SAMPLE_RATE/bpm;
} }
} }
@ -502,13 +443,12 @@ int tick(void)
} }
} }
samplesThisSecond += numberOfSamples; samples_this_second += number_of_samples;
while(samplesThisSecond >= SAMPLE_RATE) while (samples_this_second >= SAMPLE_RATE)
{ {
samplesThisSecond -= SAMPLE_RATE; samples_this_second -= SAMPLE_RATE;
playingTime++; playing_time++;
// printf("Time: %d sec\n", playingTime);
} }
if (tracksAdv != 0) if (tracksAdv != 0)
@ -517,3 +457,50 @@ int tick(void)
return 0; return 0;
} }
void seekBackward(int nsec)
{
int notes_used, a;
int desired_time = playing_time - nsec; /* Rewind 5 sec */
if (desired_time < 0)
desired_time = 0;
/* Set controllers to default values */
resetControllers();
/* Set the tempo to defalt */
bpm = mf->div*1000000/tempo;
number_of_samples = SAMPLE_RATE/bpm;
/* Reset the tracks to start */
rewindFile();
/* Reset the time counter to 0 */
playing_time = 0;
samples_this_second = 0;
/* Quickly run through any initial things that occur before notes */
do
{
notes_used = 0;
for (a = 0; a < MAX_VOICES; a++)
if (voices[a].isUsed)
notes_used++;
tick();
} while (notes_used == 0);
/* Reset the time counter to 0 */
playing_time = 0;
samples_this_second = 0;
/* Tick until goal is reached */
while (playing_time < desired_time)
tick();
}
void seekForward(int nsec)
{
int desired_time = playing_time + nsec;
while (tick() && playing_time < desired_time);
}