mirror of
https://github.com/thestk/stk
synced 2026-01-15 05:51:52 +00:00
Version 2.01
This commit is contained in:
committed by
Stephen Sinclair
parent
6485746ee9
commit
ea749b71d2
233
DrumSynt.cpp
233
DrumSynt.cpp
@@ -2,7 +2,7 @@
|
||||
/* Master Class for Drum Synthesizer */
|
||||
/* by Perry R. Cook, 1995-96 */
|
||||
/* This instrument contains a bunch of */
|
||||
/* RawWvIn objects (Non-Interpolating, */
|
||||
/* NI1sWave objects (Non-Interpolating, */
|
||||
/* 1 shot players), run through a bunch */
|
||||
/* of one-pole filters. You can specify */
|
||||
/* the maximum Polyphony (maximum number */
|
||||
@@ -13,152 +13,171 @@
|
||||
#include "DrumSynt.h"
|
||||
#include <string.h>
|
||||
|
||||
/* Not really General MIDI yet. Coming soon. */
|
||||
unsigned char genMIDIMap[128] = { 0,0,0,0,0,0,0,0, // 0-7
|
||||
0,0,0,0,0,0,0,0, // 8-15
|
||||
0,0,0,0,0,0,0,0, // 16-23
|
||||
0,0,0,0,0,0,0,0, // 24-31
|
||||
0,0,0,0,1,0,2,0, // 32-39
|
||||
2,3,6,3,6,4,7,4, // 40-47
|
||||
5,8,5,0,0,0,10,0, // 48-55
|
||||
9,0,0,0,0,0,0,0, // 56-63
|
||||
0,0,0,0,0,0,0,0, // 64-71
|
||||
0,0,0,0,0,0,0,0, // 72-79
|
||||
0,0,0,0,0,0,0,0, // 80-87
|
||||
0,0,0,0,0,0,0,0, // 88-95
|
||||
0,0,0,0,0,0,0,0, // 96-103
|
||||
0,0,0,0,0,0,0,0, // 104-111
|
||||
0,0,0,0,0,0,0,0, // 112-119
|
||||
0,0,0,0,0,0,0,0}; // 120-127
|
||||
|
||||
char waveNames[DRUM_NUMWAVES][16] = {
|
||||
"bass.raw",
|
||||
"snar.raw",
|
||||
"tomlow.raw",
|
||||
"tommid.raw",
|
||||
"tomhi.raw",
|
||||
"hat.raw",
|
||||
"ride.raw",
|
||||
"crash.raw",
|
||||
"cowbell.raw",
|
||||
"tamb.raw"
|
||||
"dope.raw",
|
||||
"bassdrum.raw",
|
||||
"snardrum.raw",
|
||||
"tomlowdr.raw",
|
||||
"tommiddr.raw",
|
||||
"tomhidrm.raw",
|
||||
"hihatcym.raw",
|
||||
"ridecymb.raw",
|
||||
"crashcym.raw",
|
||||
"cowbell1.raw",
|
||||
"tambourn.raw"
|
||||
};
|
||||
|
||||
DrumSynt :: DrumSynt() : Object()
|
||||
DrumSynt :: DrumSynt() : Instrmnt()
|
||||
{
|
||||
int i;
|
||||
/* char temp[64]; */
|
||||
|
||||
/* for (i=0;i<DRUM_NUMWAVES;i++) { */
|
||||
/* strcpy(temp,"rawwaves/drumwavs/"); */
|
||||
/* strcat(temp,waveNames[i]); */
|
||||
/* waves[i] = new RawWvIn(temp); */
|
||||
/* waves[i]->finish(); */
|
||||
/* waves[i]->normalize(0.2); */
|
||||
/* } */
|
||||
int i;
|
||||
|
||||
for (i=0;i<DRUM_POLYPHONY;i++) {
|
||||
filters[i] = new OnePole;
|
||||
sounding[i] = -1;
|
||||
/* filtSounding[i] = -1; */
|
||||
}
|
||||
numSounding = 0; /* This counts the number */
|
||||
/* of sounding voices */
|
||||
for (i=0;i<DRUM_POLYPHONY;i++) {
|
||||
filters[i] = new OnePole;
|
||||
sounding[i] = -1;
|
||||
}
|
||||
numSounding = 0; /* This counts the number */
|
||||
/* of sounding voices */
|
||||
}
|
||||
|
||||
void DrumSynt :: noteOn(int noteNum, int vel)
|
||||
void DrumSynt :: noteOn(MY_FLOAT freq, MY_FLOAT amp)
|
||||
{
|
||||
int i,notDone;
|
||||
char tempString[64];
|
||||
RawWvIn *tempWv;
|
||||
OnePole *tempFilt;
|
||||
|
||||
printf("NoteOn: %s vel=%i\n",waveNames[noteNum],vel);
|
||||
|
||||
notDone = -1;
|
||||
for (i=0;i<DRUM_POLYPHONY;i++) { /* Check first to see */
|
||||
if (sounding[i] == noteNum) notDone = i; /* if there's already */
|
||||
} /* one like this sounding */
|
||||
|
||||
if (notDone<0) { /* If not, then */
|
||||
if (numSounding == DRUM_POLYPHONY) { /* If we're already */
|
||||
delete waves[0]; /* at max polyphony, */
|
||||
filters[0]->clear(); /* then */
|
||||
tempWv = waves[0];
|
||||
tempFilt = filters[0];
|
||||
for (i=0;i<DRUM_POLYPHONY-1;i++) { /* preempt oldest */
|
||||
waves[i] = waves[i+1]; /* oldest voice and */
|
||||
filters[i] = filters[i+1]; /* ripple all down */
|
||||
}
|
||||
waves[DRUM_POLYPHONY-1] = tempWv;
|
||||
filters[DRUM_POLYPHONY-1] = tempFilt;
|
||||
}
|
||||
else {
|
||||
numSounding += 1; /* otherwise just add one */
|
||||
}
|
||||
int i,notDone;
|
||||
int noteNum;
|
||||
int vel;
|
||||
char tempString[64];
|
||||
RawWvIn *tempWv;
|
||||
OnePole *tempFilt;
|
||||
|
||||
sounding[numSounding-1] = noteNum; /* allocate new wave */
|
||||
strcpy(tempString,"rawwaves/drumwavs/");
|
||||
strcat(tempString,waveNames[noteNum]);
|
||||
waves[numSounding-1] = new RawWvIn(tempString);
|
||||
waves[numSounding-1]->normalize(0.2);
|
||||
filters[numSounding-1]->setPole(0.999 - (vel / 32.0));
|
||||
filters[numSounding-1]->setGain(vel / 128.0);
|
||||
}
|
||||
else {
|
||||
waves[notDone]->reset();
|
||||
filters[notDone]->setPole(0.999 - (vel / 32.0));
|
||||
filters[notDone]->setGain(vel / 128.0);
|
||||
}
|
||||
noteNum = (int) ((12*log(freq/220)/log(2)) + 57.01); // Yes I know, this is tres kludgey
|
||||
vel = (int) (amp * 127);
|
||||
|
||||
#if defined(_debug_)
|
||||
printf("NoteOn: %s vel=%i\n",waveNames[genMIDIMap[noteNum]],vel);
|
||||
#endif
|
||||
|
||||
notDone = -1;
|
||||
for (i=0;i<DRUM_POLYPHONY;i++) { /* Check first to see */
|
||||
if (sounding[i] == noteNum) notDone = i; /* if there's already */
|
||||
} /* one like this sounding */
|
||||
|
||||
printf("Number Sounding = %i\n",numSounding);
|
||||
for (i=0;i<numSounding;i++) printf(" %i ",sounding[i]);
|
||||
printf("\n");
|
||||
if (notDone<0) { /* If not, then */
|
||||
if (numSounding == DRUM_POLYPHONY) { /* If we're already */
|
||||
delete waves[0]; /* at max polyphony, */
|
||||
filters[0]->clear(); /* then */
|
||||
tempWv = waves[0];
|
||||
tempFilt = filters[0];
|
||||
for (i=0;i<DRUM_POLYPHONY-1;i++) { /* preempt oldest */
|
||||
waves[i] = waves[i+1]; /* voice and */
|
||||
filters[i] = filters[i+1]; /* ripple all down */
|
||||
}
|
||||
waves[DRUM_POLYPHONY-1] = tempWv;
|
||||
filters[DRUM_POLYPHONY-1] = tempFilt;
|
||||
}
|
||||
else {
|
||||
numSounding += 1; /* otherwise just add one */
|
||||
}
|
||||
|
||||
sounding[numSounding-1] = noteNum; /* allocate new wave */
|
||||
strcpy(tempString,"rawwaves/");
|
||||
strcat(tempString,waveNames[genMIDIMap[noteNum]]);
|
||||
waves[numSounding-1] = new RawWvIn(tempString);
|
||||
waves[numSounding-1]->normalize((MY_FLOAT) 0.2);
|
||||
filters[numSounding-1]->setPole((MY_FLOAT) 0.999 - ((MY_FLOAT) vel * NORM_7 * 0.6));
|
||||
filters[numSounding-1]->setGain(vel / (MY_FLOAT) 128.0);
|
||||
}
|
||||
else {
|
||||
waves[notDone]->reset();
|
||||
filters[notDone]->setPole((MY_FLOAT) 0.999 - ((MY_FLOAT) vel * NORM_7 * 0.6));
|
||||
filters[notDone]->setGain(vel / (MY_FLOAT) 128.0);
|
||||
}
|
||||
|
||||
#if defined(_debug_)
|
||||
printf("Number Sounding = %i\n",numSounding);
|
||||
for (i=0;i<numSounding;i++) printf(" %i ",sounding[i]);
|
||||
printf("\n");
|
||||
#endif
|
||||
}
|
||||
|
||||
MY_FLOAT DrumSynt :: tick()
|
||||
{
|
||||
int i, j, notDone;
|
||||
MY_FLOAT output;
|
||||
OnePole *tempFilt;
|
||||
int i, j, notDone;
|
||||
MY_FLOAT output;
|
||||
OnePole *tempFilt;
|
||||
|
||||
i = 0;
|
||||
notDone = 1;
|
||||
output = 0.0;
|
||||
i = 0;
|
||||
notDone = 1;
|
||||
output = (MY_FLOAT) 0.0;
|
||||
|
||||
if (numSounding == 0) notDone = 0;
|
||||
while (notDone && (i < numSounding)) {
|
||||
if (numSounding == 0) notDone = 0;
|
||||
while (notDone && (i < numSounding)) {
|
||||
|
||||
output += waves[i]->lastOut();
|
||||
output += filters[i]->tick(waves[i]->lastOut());
|
||||
|
||||
if (waves[i]->informTick() == 1) {
|
||||
|
||||
printf("Wave %i %i down here\n",i,sounding[i]);
|
||||
|
||||
delete waves[i];
|
||||
if (waves[i]->informTick() == 1) {
|
||||
#if defined(_debug_)
|
||||
printf("Wave %i %i down here\n",i,sounding[i]);
|
||||
#endif
|
||||
delete waves[i];
|
||||
tempFilt = filters[i];
|
||||
|
||||
for (j=i;j<numSounding-1;j++) {
|
||||
sounding[j] = sounding[j+1];
|
||||
waves[j] = waves[j+1];
|
||||
filters[j] = filters[j+1];
|
||||
}
|
||||
filters[j] = tempFilt;
|
||||
filters[j]->clear();
|
||||
sounding[j] = -1;
|
||||
numSounding -= 1;
|
||||
if (numSounding == 0) notDone = 0;
|
||||
i -= 1;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
for (j=i;j<numSounding-1;j++) {
|
||||
sounding[j] = sounding[j+1];
|
||||
waves[j] = waves[j+1];
|
||||
filters[j] = filters[j+1];
|
||||
}
|
||||
filters[j] = tempFilt;
|
||||
filters[j]->clear();
|
||||
sounding[j] = -1;
|
||||
numSounding -= 1;
|
||||
if (numSounding == 0) notDone = 0;
|
||||
i -= 1;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
|
||||
return output;
|
||||
return output * 2;
|
||||
}
|
||||
|
||||
/************** Test Main Program *********************/
|
||||
|
||||
/*
|
||||
#include "miditabl.h"
|
||||
#include "RawWvOut.h"
|
||||
#include "Reverb.h"
|
||||
#include "Noise.h"
|
||||
|
||||
int main()
|
||||
void main()
|
||||
{
|
||||
long i,j;
|
||||
DrumSynt instrument;
|
||||
RawWvOut output("test.snd");
|
||||
Reverb reverb(2137);
|
||||
Reverb reverb((MY_FLOAT) 2137);
|
||||
Noise noise;
|
||||
|
||||
for (j=0;j<100;j++) {
|
||||
i = (int) (fabs(noise.tick()) * DRUM_NUMWAVES);
|
||||
instrument.noteOn(i,127);
|
||||
instrument.noteOn(i,1.0);
|
||||
for (i=0;i<2000;i++) output.tick(reverb.tick(instrument.tick()));
|
||||
}
|
||||
|
||||
for (i=0;i<22000;i++) output.tick(reverb.tick(instrument.tick()));
|
||||
|
||||
}
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user