2005-04-15 06:08:55 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
2007-09-24 15:57:32 +00:00
|
|
|
* $Id$
|
2005-04-15 06:08:55 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 2005 Stepan Moskovchenko
|
|
|
|
*
|
2008-06-28 18:10:04 +00:00
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
2005-04-15 06:08:55 +00:00
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
2007-09-24 15:57:32 +00:00
|
|
|
#include "plugin.h"
|
|
|
|
#include "guspat.h"
|
|
|
|
#include "midiutil.h"
|
|
|
|
#include "synth.h"
|
2005-04-15 06:08:55 +00:00
|
|
|
|
|
|
|
void readTextBlock(int file, char * buf)
|
|
|
|
{
|
2005-04-20 21:07:13 +00:00
|
|
|
char c = 0;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
c = readChar(file);
|
|
|
|
} while(c == '\n' || c == ' ' || c=='\t');
|
|
|
|
|
|
|
|
rb->lseek(file, -1, SEEK_CUR);
|
|
|
|
int cp = 0;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
c = readChar(file);
|
|
|
|
buf[cp] = c;
|
|
|
|
cp++;
|
|
|
|
} while (c != '\n' && c != ' ' && c != '\t' && !eof(file));
|
|
|
|
buf[cp-1]=0;
|
|
|
|
rb->lseek(file, -1, SEEK_CUR);
|
2005-04-15 06:08:55 +00:00
|
|
|
}
|
|
|
|
|
2007-11-03 04:09:38 +00:00
|
|
|
void resetControllers()
|
2005-04-15 06:08:55 +00:00
|
|
|
{
|
2005-04-20 21:07:13 +00:00
|
|
|
int a=0;
|
|
|
|
for(a=0; a<MAX_VOICES; a++)
|
|
|
|
{
|
|
|
|
voices[a].cp=0;
|
|
|
|
voices[a].vol=0;
|
|
|
|
voices[a].ch=0;
|
2007-11-11 01:02:45 +00:00
|
|
|
voices[a].isUsed=false;
|
2005-04-20 21:07:13 +00:00
|
|
|
voices[a].note=0;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(a=0; a<16; a++)
|
|
|
|
{
|
|
|
|
chVol[a]=100; /* Default, not quite full blast.. */
|
2007-10-04 19:36:42 +00:00
|
|
|
chPan[a]=64; /* Center */
|
2005-04-20 21:07:13 +00:00
|
|
|
chPat[a]=0; /* Ac Gr Piano */
|
2007-10-04 19:36:42 +00:00
|
|
|
chPW[a]=256; /* .. not .. bent ? */
|
2007-10-15 05:11:37 +00:00
|
|
|
chPBDepth[a]=2; /* Default bend value is 2 */
|
2007-10-17 03:48:24 +00:00
|
|
|
chPBNoteOffset[a]=0; /* No offset */
|
|
|
|
chPBFractBend[a]=65536; /* Center.. no bend */
|
2007-10-21 19:47:33 +00:00
|
|
|
chLastCtrlMSB[a]=0; /* Set to pitch bend depth */
|
|
|
|
chLastCtrlLSB[a]=0; /* Set to pitch bend depth */
|
2005-04-20 21:07:13 +00:00
|
|
|
}
|
2007-11-03 04:09:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Filename is the name of the config file */
|
|
|
|
/* The MIDI file should have been loaded at this point */
|
|
|
|
int initSynth(struct MIDIfile * mf, char * filename, char * drumConfig)
|
|
|
|
{
|
|
|
|
char patchUsed[128];
|
|
|
|
char drumUsed[128];
|
|
|
|
int a=0;
|
|
|
|
|
|
|
|
resetControllers();
|
|
|
|
|
2005-04-20 21:07:13 +00:00
|
|
|
for(a=0; a<128; a++)
|
|
|
|
{
|
|
|
|
patchSet[a]=NULL;
|
|
|
|
drumSet[a]=NULL;
|
|
|
|
patchUsed[a]=0;
|
|
|
|
drumUsed[a]=0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Always load the piano.
|
|
|
|
* Some files will assume its loaded without specifically
|
|
|
|
* issuing a Patch command... then we wonder why we can't hear anything
|
|
|
|
*/
|
|
|
|
patchUsed[0]=1;
|
|
|
|
|
|
|
|
/* Scan the file to see what needs to be loaded */
|
2007-09-27 03:59:33 +00:00
|
|
|
if(mf != NULL)
|
2005-04-20 21:07:13 +00:00
|
|
|
{
|
2007-09-27 03:59:33 +00:00
|
|
|
for(a=0; a<mf->numTracks; a++)
|
2005-04-20 21:07:13 +00:00
|
|
|
{
|
2007-09-27 03:59:33 +00:00
|
|
|
unsigned int ts=0;
|
2005-04-20 21:07:13 +00:00
|
|
|
|
2007-09-27 03:59:33 +00:00
|
|
|
if(mf->tracks[a] == NULL)
|
|
|
|
{
|
|
|
|
printf("NULL TRACK !!!");
|
|
|
|
rb->splash(HZ*2, "Null Track in loader.");
|
|
|
|
return -1;
|
|
|
|
}
|
2005-04-20 21:07:13 +00:00
|
|
|
|
2007-09-27 03:59:33 +00:00
|
|
|
for(ts=0; ts<mf->tracks[a]->numEvents; ts++)
|
|
|
|
{
|
2005-04-20 21:07:13 +00:00
|
|
|
|
2007-09-27 03:59:33 +00:00
|
|
|
if((getEvent(mf->tracks[a], ts)->status) == (MIDI_NOTE_ON+9))
|
|
|
|
drumUsed[getEvent(mf->tracks[a], ts)->d1]=1;
|
|
|
|
|
|
|
|
if( (getEvent(mf->tracks[a], ts)->status & 0xF0) == MIDI_PRGM)
|
|
|
|
patchUsed[getEvent(mf->tracks[a], ts)->d1]=1;
|
|
|
|
}
|
2005-04-20 21:07:13 +00:00
|
|
|
}
|
2007-09-27 03:59:33 +00:00
|
|
|
} else
|
|
|
|
{
|
|
|
|
/* Initialize the whole drum set */
|
|
|
|
for(a=0; a<128; a++)
|
|
|
|
drumUsed[a]=1;
|
|
|
|
|
2005-04-20 21:07:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int file = rb->open(filename, O_RDONLY);
|
2006-05-07 07:12:07 +00:00
|
|
|
if(file < 0)
|
2005-04-20 21:07:13 +00:00
|
|
|
{
|
2007-04-21 05:35:17 +00:00
|
|
|
printf("");
|
|
|
|
printf("No MIDI patchset found.");
|
|
|
|
printf("Please install the instruments.");
|
|
|
|
printf("See Rockbox page for more info.");
|
2006-05-07 07:12:07 +00:00
|
|
|
|
2007-03-16 21:56:08 +00:00
|
|
|
rb->splash(HZ*2, "No Instruments");
|
2005-04-20 21:07:13 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-07-27 20:40:20 +00:00
|
|
|
char name[40];
|
|
|
|
char fn[40];
|
2005-04-20 21:07:13 +00:00
|
|
|
|
|
|
|
/* Scan our config file and load the right patches as needed */
|
|
|
|
int c = 0;
|
2007-03-17 09:02:53 +00:00
|
|
|
name[0] = '\0';
|
2007-04-21 05:35:17 +00:00
|
|
|
printf("Loading instruments");
|
2005-04-20 21:07:13 +00:00
|
|
|
for(a=0; a<128; a++)
|
|
|
|
{
|
|
|
|
while(readChar(file)!=' ' && !eof(file));
|
|
|
|
readTextBlock(file, name);
|
2008-07-27 20:40:20 +00:00
|
|
|
|
|
|
|
rb->snprintf(fn, 40, ROCKBOX_DIR "/patchset/%s.pat", name);
|
2006-05-01 23:22:59 +00:00
|
|
|
/* printf("\nLOADING: <%s> ", fn); */
|
2005-04-20 21:07:13 +00:00
|
|
|
|
|
|
|
if(patchUsed[a]==1)
|
|
|
|
{
|
|
|
|
patchSet[a]=gusload(fn);
|
|
|
|
|
|
|
|
if(patchSet[a] == NULL) /* There was an error loading it */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
while((c != '\n'))
|
|
|
|
c = readChar(file);
|
|
|
|
}
|
|
|
|
rb->close(file);
|
2008-07-27 20:40:20 +00:00
|
|
|
|
2005-04-20 21:07:13 +00:00
|
|
|
file = rb->open(drumConfig, O_RDONLY);
|
2006-05-07 07:12:07 +00:00
|
|
|
if(file < 0)
|
2005-04-20 21:07:13 +00:00
|
|
|
{
|
2007-03-16 21:56:08 +00:00
|
|
|
rb->splash(HZ*2, "Bad drum config. Did you install the patchset?");
|
2005-04-20 21:07:13 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Scan our config file and load the drum data */
|
|
|
|
int idx=0;
|
|
|
|
char number[30];
|
2007-04-21 05:35:17 +00:00
|
|
|
printf("Loading drums");
|
2005-04-20 21:07:13 +00:00
|
|
|
while(!eof(file))
|
|
|
|
{
|
|
|
|
readTextBlock(file, number);
|
|
|
|
readTextBlock(file, name);
|
2008-07-27 20:40:20 +00:00
|
|
|
rb->snprintf(fn, 40, ROCKBOX_DIR "/patchset/%s.pat", name);
|
2005-04-20 21:07:13 +00:00
|
|
|
|
|
|
|
idx = rb->atoi(number);
|
|
|
|
if(idx == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if(drumUsed[idx]==1)
|
|
|
|
{
|
|
|
|
drumSet[idx]=gusload(fn);
|
|
|
|
if(drumSet[idx] == NULL) /* Error loading patch */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
while((c != '\n') && (c != 255) && (!eof(file)))
|
|
|
|
c = readChar(file);
|
|
|
|
}
|
|
|
|
rb->close(file);
|
|
|
|
return 0;
|
2005-04-15 06:08:55 +00:00
|
|
|
}
|
|
|
|
|
2007-04-11 10:48:50 +00:00
|
|
|
#define getSample(s,wf) ((short *)(wf)->data)[s]
|
2005-04-15 06:08:55 +00:00
|
|
|
|
2007-09-30 11:21:25 +00:00
|
|
|
void setPoint(struct SynthObject * so, int pt) ICODE_ATTR;
|
2006-05-01 23:22:59 +00:00
|
|
|
void setPoint(struct SynthObject * so, int pt)
|
2005-04-15 06:08:55 +00:00
|
|
|
{
|
2005-04-20 21:07:13 +00:00
|
|
|
if(so->ch==9) /* Drums, no ADSR */
|
|
|
|
{
|
|
|
|
so->curOffset = 1<<27;
|
|
|
|
so->curRate = 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(so->wf==NULL)
|
|
|
|
{
|
2007-04-21 05:35:17 +00:00
|
|
|
printf("Crap... null waveform...");
|
2005-04-20 21:07:13 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if(so->wf->envRate==NULL)
|
|
|
|
{
|
2007-04-21 05:35:17 +00:00
|
|
|
printf("Waveform has no envelope set");
|
2005-04-20 21:07:13 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
so->curPoint = pt;
|
|
|
|
|
2006-05-01 23:22:59 +00:00
|
|
|
int r;
|
2005-04-20 21:07:13 +00:00
|
|
|
int rate = so->wf->envRate[pt];
|
|
|
|
|
|
|
|
r=3-((rate>>6) & 0x3); /* Some blatant Timidity code for rate conversion... */
|
|
|
|
r*=3;
|
|
|
|
r = (rate & 0x3f) << r;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Okay. This is the rate shift. Timidity defaults to 9, and sets
|
|
|
|
* it to 10 if you use the fast decay option. Slow decay sounds better
|
|
|
|
* on some files, except on some other files... you get chords that aren't
|
|
|
|
* done decaying yet.. and they dont harmonize with the next chord and it
|
|
|
|
* sounds like utter crap. Yes, even Timitidy does that. So I'm going to
|
|
|
|
* default this to 10, and maybe later have an option to set it to 9
|
|
|
|
* for longer decays.
|
|
|
|
*/
|
2007-09-30 09:01:38 +00:00
|
|
|
so->curRate = r<<10;
|
2005-04-20 21:07:13 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Do this here because the patches assume a 44100 sampling rate
|
|
|
|
* We've halved our sampling rate, ergo the ADSR code will be
|
|
|
|
* called half the time. Ergo, double the rate to keep stuff
|
|
|
|
* sounding right.
|
2006-05-01 23:22:59 +00:00
|
|
|
*
|
|
|
|
* Or just move the 1 up one line to optimize a tiny bit.
|
2005-04-20 21:07:13 +00:00
|
|
|
*/
|
2006-10-03 23:27:44 +00:00
|
|
|
/* so->curRate = so->curRate << 1; */
|
2005-04-20 21:07:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
so->targetOffset = so->wf->envOffset[pt]<<(20);
|
|
|
|
if(pt==0)
|
|
|
|
so->curOffset = 0;
|
2005-04-15 06:08:55 +00:00
|
|
|
}
|
|
|
|
|
2007-10-15 05:11:37 +00:00
|
|
|
static inline void synthVoice(struct SynthObject * so, int32_t * out, unsigned int samples)
|
2005-04-15 06:08:55 +00:00
|
|
|
{
|
2006-05-01 23:22:59 +00:00
|
|
|
struct GWaveform * wf;
|
2007-10-15 05:11:37 +00:00
|
|
|
register int s1;
|
|
|
|
register int s2;
|
|
|
|
|
|
|
|
register unsigned int cp_temp = so->cp;
|
2006-05-01 23:22:59 +00:00
|
|
|
|
2005-04-20 21:07:13 +00:00
|
|
|
wf = so->wf;
|
2005-04-15 06:08:55 +00:00
|
|
|
|
2007-11-11 01:02:45 +00:00
|
|
|
const unsigned int pan = chPan[so->ch];
|
|
|
|
const int volscale = so->volscale;
|
|
|
|
|
2007-10-15 05:11:37 +00:00
|
|
|
const int mode_mask24 = wf->mode&24;
|
|
|
|
const int mode_mask28 = wf->mode&28;
|
|
|
|
const int mode_mask_looprev = wf->mode&LOOP_REVERSE;
|
2005-04-15 06:08:55 +00:00
|
|
|
|
2007-10-15 05:11:37 +00:00
|
|
|
const unsigned int num_samples = (wf->numSamples-1) << FRACTSIZE;
|
2006-10-02 04:13:33 +00:00
|
|
|
|
2007-10-15 05:11:37 +00:00
|
|
|
const unsigned int end_loop = wf->endLoop << FRACTSIZE;
|
|
|
|
const unsigned int start_loop = wf->startLoop << FRACTSIZE;
|
|
|
|
const int diff_loop = end_loop-start_loop;
|
2006-10-02 04:13:33 +00:00
|
|
|
|
2008-11-24 21:32:08 +00:00
|
|
|
bool rampdown = (so->state == STATE_RAMPDOWN);
|
|
|
|
const bool ch_9 = (so->ch == 9);
|
|
|
|
|
2008-11-22 16:56:20 +00:00
|
|
|
while(LIKELY(samples-- > 0))
|
2005-04-20 21:07:13 +00:00
|
|
|
{
|
2007-10-15 05:11:37 +00:00
|
|
|
/* Is voice being ramped? */
|
2008-11-24 21:32:08 +00:00
|
|
|
if(UNLIKELY(rampdown))
|
2007-10-15 05:11:37 +00:00
|
|
|
{
|
|
|
|
if(so->decay != 0) /* Ramp has been started */
|
|
|
|
{
|
|
|
|
so->decay = so->decay / 2;
|
2005-04-15 06:08:55 +00:00
|
|
|
|
2007-10-15 05:11:37 +00:00
|
|
|
if(so->decay < 10 && so->decay > -10)
|
2007-11-11 01:02:45 +00:00
|
|
|
so->isUsed = false;
|
2006-10-03 21:09:47 +00:00
|
|
|
|
2008-11-24 21:32:08 +00:00
|
|
|
s1 = so->decay;
|
|
|
|
s2 = s1 * pan;
|
|
|
|
s1 = (s1 << 7) -s2;
|
|
|
|
*(out++) += ((s1 << 9) & 0xFFFF0000) | ((s2 >> 7) &0xFFFF);
|
2007-10-15 05:11:37 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else /* OK to advance voice */
|
|
|
|
{
|
|
|
|
cp_temp += so->delta;
|
|
|
|
}
|
2005-04-15 06:08:55 +00:00
|
|
|
|
2007-10-15 05:11:37 +00:00
|
|
|
s2 = getSample((cp_temp >> FRACTSIZE)+1, wf);
|
2005-04-15 06:08:55 +00:00
|
|
|
|
2008-11-22 16:56:20 +00:00
|
|
|
if(LIKELY(mode_mask28))
|
2006-10-03 21:09:47 +00:00
|
|
|
{
|
2007-11-15 21:20:29 +00:00
|
|
|
/* LOOP_REVERSE|LOOP_PINGPONG = 24 */
|
2008-11-22 16:56:20 +00:00
|
|
|
if(UNLIKELY(mode_mask24 && so->loopState == STATE_LOOPING && (cp_temp < start_loop)))
|
2007-10-15 05:11:37 +00:00
|
|
|
{
|
2008-11-22 16:56:20 +00:00
|
|
|
if(UNLIKELY(mode_mask_looprev))
|
2007-11-15 21:20:29 +00:00
|
|
|
{
|
|
|
|
cp_temp += diff_loop;
|
|
|
|
s2=getSample((cp_temp >> FRACTSIZE), wf);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
so->delta = -so->delta; /* At this point cp_temp is wrong. We need to take a step */
|
|
|
|
}
|
2007-10-15 05:11:37 +00:00
|
|
|
}
|
2007-11-11 01:02:45 +00:00
|
|
|
|
2008-11-22 16:56:20 +00:00
|
|
|
if(UNLIKELY(cp_temp >= end_loop))
|
2007-11-15 21:20:29 +00:00
|
|
|
{
|
|
|
|
so->loopState = STATE_LOOPING;
|
2008-11-22 16:56:20 +00:00
|
|
|
if(UNLIKELY(!mode_mask24))
|
2007-11-15 21:20:29 +00:00
|
|
|
{
|
|
|
|
cp_temp -= diff_loop;
|
|
|
|
s2=getSample((cp_temp >> FRACTSIZE), wf);
|
|
|
|
}
|
|
|
|
else
|
2007-11-11 01:02:45 +00:00
|
|
|
{
|
2007-11-15 21:20:29 +00:00
|
|
|
so->delta = -so->delta;
|
2007-11-11 01:02:45 +00:00
|
|
|
}
|
2007-10-15 05:11:37 +00:00
|
|
|
}
|
2006-05-01 23:22:59 +00:00
|
|
|
}
|
2007-10-15 05:11:37 +00:00
|
|
|
|
|
|
|
/* Have we overrun? */
|
2008-11-22 16:56:20 +00:00
|
|
|
if(UNLIKELY(cp_temp >= num_samples))
|
2005-04-20 21:07:13 +00:00
|
|
|
{
|
2007-10-15 05:11:37 +00:00
|
|
|
cp_temp -= so->delta;
|
|
|
|
s2 = getSample((cp_temp >> FRACTSIZE)+1, wf);
|
2008-11-24 21:32:08 +00:00
|
|
|
|
|
|
|
if (!rampdown) /* stop voice */
|
|
|
|
{
|
|
|
|
rampdown = true;
|
|
|
|
so->decay = 0;
|
|
|
|
}
|
2005-04-20 21:07:13 +00:00
|
|
|
}
|
2006-10-02 04:13:33 +00:00
|
|
|
|
2007-10-15 05:11:37 +00:00
|
|
|
/* Better, working, linear interpolation */
|
|
|
|
s1=getSample((cp_temp >> FRACTSIZE), wf);
|
2005-04-20 21:07:13 +00:00
|
|
|
|
2007-11-11 01:02:45 +00:00
|
|
|
s1 +=((signed)((s2 - s1) * (cp_temp & ((1<<FRACTSIZE)-1)))>>FRACTSIZE);
|
2005-04-20 21:07:13 +00:00
|
|
|
|
2008-11-22 16:56:20 +00:00
|
|
|
if(UNLIKELY(so->curRate == 0))
|
2007-10-15 05:11:37 +00:00
|
|
|
{
|
2008-11-24 21:32:08 +00:00
|
|
|
if (!rampdown) /* stop voice */
|
|
|
|
{
|
|
|
|
rampdown = true;
|
|
|
|
so->decay = 0;
|
|
|
|
}
|
2007-11-11 01:02:45 +00:00
|
|
|
// so->isUsed = false;
|
2007-10-15 05:11:37 +00:00
|
|
|
}
|
2005-04-20 21:07:13 +00:00
|
|
|
|
2008-11-24 21:32:08 +00:00
|
|
|
if(LIKELY(!ch_9 && !rampdown)) /* Stupid ADSR code... and don't do ADSR for drums */
|
2005-04-20 21:07:13 +00:00
|
|
|
{
|
2008-11-22 16:56:20 +00:00
|
|
|
if(UNLIKELY(so->curOffset < so->targetOffset))
|
2005-04-20 21:07:13 +00:00
|
|
|
{
|
2007-10-15 05:11:37 +00:00
|
|
|
so->curOffset += (so->curRate);
|
2008-11-22 16:56:20 +00:00
|
|
|
if(UNLIKELY(so -> curOffset > so->targetOffset && so->curPoint != 2))
|
2006-10-03 23:27:44 +00:00
|
|
|
{
|
2008-11-22 16:56:20 +00:00
|
|
|
if(UNLIKELY(so->curPoint != 5))
|
2007-10-15 05:11:37 +00:00
|
|
|
{
|
|
|
|
setPoint(so, so->curPoint+1);
|
|
|
|
}
|
2008-11-24 21:32:08 +00:00
|
|
|
else if (!rampdown) /* stop voice */
|
2007-10-15 05:11:37 +00:00
|
|
|
{
|
2008-11-24 21:32:08 +00:00
|
|
|
rampdown = true;
|
|
|
|
so->decay = 0;
|
2007-10-15 05:11:37 +00:00
|
|
|
}
|
2006-10-03 23:27:44 +00:00
|
|
|
}
|
2007-10-15 05:11:37 +00:00
|
|
|
} else
|
|
|
|
{
|
|
|
|
so->curOffset -= (so->curRate);
|
2008-11-22 16:56:20 +00:00
|
|
|
if(UNLIKELY(so -> curOffset < so->targetOffset && so->curPoint != 2))
|
2006-10-03 21:09:47 +00:00
|
|
|
{
|
2008-11-22 16:56:20 +00:00
|
|
|
if(UNLIKELY(so->curPoint != 5))
|
2007-10-15 05:11:37 +00:00
|
|
|
{
|
|
|
|
setPoint(so, so->curPoint+1);
|
|
|
|
}
|
2008-11-24 21:32:08 +00:00
|
|
|
else if (!rampdown) /* stop voice */
|
2007-10-15 05:11:37 +00:00
|
|
|
{
|
2008-11-24 21:32:08 +00:00
|
|
|
rampdown = true;
|
|
|
|
so->decay = 0;
|
2007-10-15 05:11:37 +00:00
|
|
|
}
|
|
|
|
|
2006-10-03 21:09:47 +00:00
|
|
|
}
|
2005-04-20 21:07:13 +00:00
|
|
|
}
|
2007-10-15 05:11:37 +00:00
|
|
|
}
|
|
|
|
|
2008-11-22 16:56:20 +00:00
|
|
|
if(UNLIKELY(so->curOffset < 0))
|
2005-04-20 21:07:13 +00:00
|
|
|
{
|
2007-10-15 05:11:37 +00:00
|
|
|
so->curOffset = so->targetOffset;
|
2008-11-24 21:32:08 +00:00
|
|
|
if (!rampdown)
|
|
|
|
{
|
|
|
|
rampdown = true;
|
|
|
|
so->decay = 0;
|
|
|
|
}
|
2007-10-15 05:11:37 +00:00
|
|
|
}
|
2005-04-20 21:07:13 +00:00
|
|
|
|
2007-11-11 01:02:45 +00:00
|
|
|
s1 = s1 * (so->curOffset >> 22) >> 8;
|
|
|
|
|
|
|
|
/* Scaling by channel volume and note volume is done in sequencer.c */
|
|
|
|
/* That saves us some multiplication and pointer operations */
|
|
|
|
s1 = s1 * volscale >> 14;
|
2005-04-20 21:07:13 +00:00
|
|
|
|
2007-10-15 05:11:37 +00:00
|
|
|
/* need to set ramp beginning */
|
2008-11-24 21:32:08 +00:00
|
|
|
if(UNLIKELY(rampdown && so->decay == 0))
|
2007-10-15 05:11:37 +00:00
|
|
|
{
|
2007-11-11 01:02:45 +00:00
|
|
|
so->decay = s1;
|
2007-10-15 05:11:37 +00:00
|
|
|
if(so->decay == 0)
|
|
|
|
so->decay = 1; /* stupid junk.. */
|
2005-04-20 21:07:13 +00:00
|
|
|
}
|
|
|
|
|
2008-11-24 21:32:08 +00:00
|
|
|
s2 = s1 * pan;
|
|
|
|
s1 = (s1 << 7) - s2;
|
|
|
|
*(out++) += ((s1 << 9) & 0xFFFF0000) | ((s2 >> 7) &0xFFFF);
|
2005-04-20 21:07:13 +00:00
|
|
|
}
|
|
|
|
|
2008-11-24 21:32:08 +00:00
|
|
|
/* store these again */
|
|
|
|
if (rampdown)
|
|
|
|
so->state = STATE_RAMPDOWN;
|
|
|
|
so->cp = cp_temp;
|
2007-10-15 05:11:37 +00:00
|
|
|
return;
|
2005-04-15 06:08:55 +00:00
|
|
|
}
|
|
|
|
|
2007-10-15 05:11:37 +00:00
|
|
|
/* buffer to hold all the samples for the current tick, this is a hack
|
|
|
|
neccesary for coldfire targets as pcm_play_data uses the dma which cannot
|
|
|
|
access iram */
|
2007-10-21 22:50:10 +00:00
|
|
|
int32_t samp_buf[512] IBSS_ATTR;
|
2007-10-15 05:11:37 +00:00
|
|
|
|
2007-10-08 19:28:41 +00:00
|
|
|
/* synth num_samples samples and write them to the */
|
|
|
|
/* buffer pointed to by buf_ptr */
|
|
|
|
void synthSamples(int32_t *buf_ptr, unsigned int num_samples) ICODE_ATTR;
|
|
|
|
void synthSamples(int32_t *buf_ptr, unsigned int num_samples)
|
|
|
|
{
|
2008-11-22 16:56:20 +00:00
|
|
|
if (UNLIKELY(num_samples > 512))
|
2007-10-21 22:50:10 +00:00
|
|
|
DEBUGF("num_samples is too big!\n");
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct SynthObject *voicept;
|
2007-10-15 05:11:37 +00:00
|
|
|
|
2007-10-21 22:50:10 +00:00
|
|
|
rb->memset(samp_buf, 0, num_samples*4);
|
2007-10-08 19:28:41 +00:00
|
|
|
|
2007-10-21 22:50:10 +00:00
|
|
|
for(i=0; i < MAX_VOICES; i++)
|
2007-10-08 19:28:41 +00:00
|
|
|
{
|
2007-10-21 22:50:10 +00:00
|
|
|
voicept=&voices[i];
|
2007-11-11 01:02:45 +00:00
|
|
|
if(voicept->isUsed)
|
2007-10-21 22:50:10 +00:00
|
|
|
{
|
|
|
|
synthVoice(voicept, samp_buf, num_samples);
|
|
|
|
}
|
2007-10-08 19:28:41 +00:00
|
|
|
}
|
|
|
|
|
2007-10-21 22:50:10 +00:00
|
|
|
rb->memcpy(buf_ptr, samp_buf, num_samples*4);
|
|
|
|
}
|
2008-11-20 20:20:43 +00:00
|
|
|
|
2007-10-08 19:28:41 +00:00
|
|
|
/* TODO: Automatic Gain Control, anyone? */
|
|
|
|
/* Or, should this be implemented on the DSP's output volume instead? */
|
|
|
|
|
|
|
|
return; /* No more ghetto lowpass filter. Linear interpolation works well. */
|
|
|
|
}
|
|
|
|
|