summaryrefslogtreecommitdiff
path: root/instrument.c
diff options
context:
space:
mode:
Diffstat (limited to 'instrument.c')
-rw-r--r--instrument.c152
1 files changed, 76 insertions, 76 deletions
diff --git a/instrument.c b/instrument.c
index 097ede4..fac875d 100644
--- a/instrument.c
+++ b/instrument.c
@@ -8,19 +8,19 @@
/* This is not exposed to other files, as this should be controlled by
individual instruments. */
typedef struct {
- float freq;
- float amp;
+ NUMT freq;
+ NUMT amp;
unsigned char pred; /* if this is 0, then no frequency
modulation is present. */
} FM;
/* By similar reasoning, ADSR is not exposed either. */
typedef struct {
- float attack_p;
- float decay_p;
- float sustain_p;
- float release_p;
- float sustain_level;
+ NUMT attack_p;
+ NUMT decay_p;
+ NUMT sustain_p;
+ NUMT release_p;
+ NUMT sustain_level;
unsigned char pred; /* If this is 0 then the instrument
does not adapt the ADSR model. */
} ADSR;
@@ -32,13 +32,13 @@ typedef struct {
flexibility. */
struct Instrument_s {
- float *sin_coefs; /* coefficients of sin in the Fourier
+ NUMT *sin_coefs; /* coefficients of sin in the Fourier
expansion. */
- float *cos_coefs; /* coefficients of cos in the Fourier
+ NUMT *cos_coefs; /* coefficients of cos in the Fourier
expansion. */
sound_t sound; /* the function to produce sounds. */
merger_t merger; /* the function to merge waves. */
- float reverb; /* The percentage of reverberation. */
+ NUMT reverb; /* The percentage of reverberation. */
FM fm;
ADSR adsr;
};
@@ -61,34 +61,34 @@ merge_waves(Instrument *in, WaveFrag *frags, LENT len, int step)
/* The first is piano */
UH_ATTR
-static float
-smooth_step(float t)
+static NUMT
+smooth_step(NUMT t)
{
- if (t <= 0.0f) {
- return 0.0f;
- } else if (t >= 1.0f) {
- return 1.0f;
+ if (t <= 0.0) {
+ return 0.0;
+ } else if (t >= 1.0) {
+ return 1.0;
} else {
return 6*pow(t, 5)-15*pow(t, 4)+10*pow(t, 3);
}
}
UH_ATTR
-static float
-old_piano_sound_internal(Instrument *piano, float theta)
+static NUMT
+old_piano_sound_internal(Instrument *piano, NUMT theta)
{
- return sin(theta)+1.869*sin(2.0f*theta)+0.042f*sin(3.0f*theta)
- +0.022f*sin(4.0f*theta)+cos(theta);
+ return sin(theta)+1.869*sin(2.0*theta)+0.042*sin(3.0*theta)
+ +0.022*sin(4.0*theta)+cos(theta);
}
UH_ATTR
-static float
-piano_sound_internal(Instrument *piano, float theta)
+static NUMT
+piano_sound_internal(Instrument *piano, NUMT theta)
{
return (*(piano->sin_coefs) * sin(theta)+
- *(piano->sin_coefs+1) * sin(2.0f*theta)+
- *(piano->sin_coefs+2) * sin(3.0f*theta))*
- exp(-0.004f*theta);
+ *(piano->sin_coefs+1) * sin(2.0*theta)+
+ *(piano->sin_coefs+2) * sin(3.0*theta))*
+ exp(-0.004*theta);
}
UH_ATTR
@@ -97,17 +97,17 @@ piano_sound(void * in, Volume v, Hertz h, Seconds s)
{
Instrument *piano = (Instrument *)in;
- float step = (float) (h * 2 * M_PI) / SAMPLE_RATE;
+ NUMT step = (NUMT) (h * 2 * M_PI) / SAMPLE_RATE;
LENT sample_num = (LENT) floor(SAMPLE_RATE * s);
Wave w = MYALLOC(Pulse, sizeof(*w) * sample_num);
- float theta = 0.0f, temp = 0.0f;
+ NUMT theta = 0.0, temp = 0.0;
for (LENT i = 0; i < sample_num; i++, theta += step) {
temp = piano_sound_internal(piano, theta);
temp += temp*temp*temp;
- *(w+i) = (float) v * piano_sound_internal(piano, temp);
+ *(w+i) = (NUMT) v * piano_sound_internal(piano, temp);
}
return (WaveFrag) { w, sample_num };
@@ -153,11 +153,11 @@ Instrument *make_piano()
ins->adsr.pred = 0;
ins->sin_coefs = malloc(sizeof(*(ins->sin_coefs)) * 3);
ins->cos_coefs = malloc(sizeof(*(ins->cos_coefs)) * 1);
- ins->reverb = 0.0f;
+ ins->reverb = 0.0;
- *(ins->sin_coefs) = 0.65f;
- *(ins->sin_coefs+1) = 0.45f;
- *(ins->sin_coefs+2) = 0.1f;
+ *(ins->sin_coefs) = 0.65;
+ *(ins->sin_coefs+1) = 0.45;
+ *(ins->sin_coefs+2) = 0.1;
*(ins->cos_coefs) = 1;
ins->sound = piano_sound;
ins->merger = simple_merger;
@@ -177,36 +177,36 @@ destroy_piano(Instrument *in)
UH_ATTR
Pulse
-violin_sound_internal(Instrument *in, float theta)
+violin_sound_internal(Instrument *in, NUMT theta)
{
- return -1.0f*fmod(theta, 2.0f*M_PI)/M_PI + 1.0f;
+ return -1.0*fmod(theta, 2.0*M_PI)/M_PI + 1.0;
/* return (*(in->sin_coefs)*sin(theta)+
- * *(in->sin_coefs+1)*sin(2.0f*theta)+
- * *(in->sin_coefs+2)*sin(3.0f*theta)+
- * *(in->sin_coefs+3)*sin(4.0f*theta)+
- * *(in->sin_coefs+4)*sin(5.0f*theta)); */
- /* Pulse result = 0.0f;
+ * *(in->sin_coefs+1)*sin(2.0*theta)+
+ * *(in->sin_coefs+2)*sin(3.0*theta)+
+ * *(in->sin_coefs+3)*sin(4.0*theta)+
+ * *(in->sin_coefs+4)*sin(5.0*theta)); */
+ /* Pulse result = 0.0;
* for (int i = 0; i < 4; i++)
- * result += *(in->sin_coefs+i) * sin((float) (i+1)*theta); */
+ * result += *(in->sin_coefs+i) * sin((NUMT) (i+1)*theta); */
/* result = sin(theta); */
/* return result; */
}
UH_ATTR
-float violin_adsr(Instrument *violin, float t)
+NUMT violin_adsr(Instrument *violin, NUMT t)
{
- if (t <= 0.0f || t > 1.0f)
- return 0.0f;
+ if (t <= 0.0 || t > 1.0)
+ return 0.0;
if (t <= violin->adsr.attack_p)
- return log(1.0f + 2.7f*(t / violin->adsr.attack_p));
+ return log(1.0 + 2.7*(t / violin->adsr.attack_p));
else if (t <= violin->adsr.decay_p+violin->adsr.attack_p)
return (t - violin->adsr.attack_p) / violin->adsr.decay_p;
- else if (t <= 1.0f - violin->adsr.release_p)
+ else if (t <= 1.0 - violin->adsr.release_p)
return violin->adsr.sustain_level;
else
- return 1.0f - (1.0f - t) / violin->adsr.release_p;
+ return 1.0 - (1.0 - t) / violin->adsr.release_p;
}
UH_ATTR
@@ -217,45 +217,45 @@ violin_sound(void * in, Volume v, Hertz h, Seconds s)
/* We produce a little more samples so that we can create the effect
of reverberation later on. */
- s *= (1.0f+violin->reverb);
+ s *= (1.0+violin->reverb);
- float step = (float) (h * 2 * M_PI) / SAMPLE_RATE;
+ NUMT step = (NUMT) (h * 2 * M_PI) / SAMPLE_RATE;
LENT sample_num = (LENT) floor(SAMPLE_RATE * s);
- /* float attack_step = 1.0f / (float) (sample_num * violin->adsr.attack_p);
- * float decay_step = 1.0f / (float) (sample_num * violin->adsr.decay_p);
- * float sustain_step = 1.0f / (float) (sample_num * violin->adsr.sustain_p);
- * float release_step = 1.0f / (float) (sample_num * violin->adsr.release_p); */
+ /* NUMT attack_step = 1.0 / (NUMT) (sample_num * violin->adsr.attack_p);
+ * NUMT decay_step = 1.0 / (NUMT) (sample_num * violin->adsr.decay_p);
+ * NUMT sustain_step = 1.0 / (NUMT) (sample_num * violin->adsr.sustain_p);
+ * NUMT release_step = 1.0 / (NUMT) (sample_num * violin->adsr.release_p); */
Wave w = MYALLOC(Pulse, sizeof(*w) * sample_num);
- float theta = 0.0f, temp = 0.0f, adsrcounter = 0.0f;
+ NUMT theta = 0.0, temp = 0.0, adsrcounter = 0.0;
for (LENT i = 0; i < sample_num; i++, theta += step) {
temp = violin_sound_internal(violin, theta);
- adsrcounter = violin_adsr(violin, (float) i / sample_num);
+ adsrcounter = violin_adsr(violin, (NUMT) i / sample_num);
/* temp += temp*temp*temp; */
- /* *(w+i) = (float) v * temp * ((sustain_flag) ? SUSTAIN_LEVEL : adsrcounter); */
- *(w+i) = (float) v * temp * adsrcounter;
+ /* *(w+i) = (NUMT) v * temp * ((sustain_flag) ? SUSTAIN_LEVEL : adsrcounter); */
+ *(w+i) = (NUMT) v * temp * adsrcounter;
/* switch (phase) {
* case 0: /\* attack phase *\/
* adsrcounter += attack_step;
- * if (adsrcounter >= 1.0f) {
- * adsrcounter = 1.0f;
+ * if (adsrcounter >= 1.0) {
+ * adsrcounter = 1.0;
* phase++;
* }
* break;
* case 1: /\* decay phase *\/
* adsrcounter -= decay_step;
* if (adsrcounter <= SUSTAIN_LEVEL) {
- * adsrcounter = 0.0f;
+ * adsrcounter = 0.0;
* sustain_flag = 1;
* phase++;
* }
* break;
* case 2: /\* sustain phase *\/
* adsrcounter += sustain_step;
- * if (adsrcounter >= 1.0f) {
+ * if (adsrcounter >= 1.0) {
* adsrcounter = SUSTAIN_LEVEL;
* sustain_flag = 0;
* phase++;
@@ -263,7 +263,7 @@ violin_sound(void * in, Volume v, Hertz h, Seconds s)
* break;
* default: /\* release phase *\/
* adsrcounter -= release_step;
- * if (adsrcounter <= 0.0f) adsrcounter = 0.0f;
+ * if (adsrcounter <= 0.0) adsrcounter = 0.0;
* break;
* } */
}
@@ -283,7 +283,7 @@ violin_merger(void *in, WaveFrag *frags, LENT len, int step)
LENT total_len = 0;
- float reverb_ratio = 1.0f / (1.0f + violin->reverb);
+ NUMT reverb_ratio = 1.0 / (1.0 + violin->reverb);
for (LENT i = 0; i < len; i += step)
total_len += (LENT) ((frags+i)->n * reverb_ratio);
@@ -355,14 +355,14 @@ make_violin()
violin->fm.pred = 0;
violin->adsr.pred = 1;
- violin->reverb = 0.01f;
+ violin->reverb = 0.01;
violin->adsr.attack_p = 0.09;
violin->adsr.release_p = 0.09;
violin->adsr.decay_p = 0.05;
- violin->adsr.sustain_p = max(1.0f - (violin->adsr.attack_p+
+ violin->adsr.sustain_p = max(1.0 - (violin->adsr.attack_p+
violin->adsr.decay_p+
violin->adsr.release_p),
- 0.0f);
+ 0.0);
violin->adsr.sustain_level = 0.9;
double temp [] = {
@@ -376,19 +376,19 @@ make_violin()
0.002860679, 0.002558108, 0.0, 0.001650392
};
- violin->sin_coefs = malloc(sizeof(float)*26);
+ violin->sin_coefs = malloc(sizeof(NUMT)*26);
for (int i = 0; i < 26; i++)
*(violin->sin_coefs+i) = *(temp+i);
- /* *(violin->sin_coefs) = 1.0f;
- * *(violin->sin_coefs+1) = 0.41f;
- * *(violin->sin_coefs+2) = 0.35f;
- * *(violin->sin_coefs+3) = 0.39f;
- * *(violin->sin_coefs+4) = 0.45f;
- * *(violin->sin_coefs+5) = 0.35f;
- * *(violin->sin_coefs+6) = 0.5f;
- * *(violin->sin_coefs+7) = 0.4f;
- * *(violin->sin_coefs+8) = 0.35f;
- * *(violin->sin_coefs+9) = 0.1f; */
+ /* *(violin->sin_coefs) = 1.0;
+ * *(violin->sin_coefs+1) = 0.41;
+ * *(violin->sin_coefs+2) = 0.35;
+ * *(violin->sin_coefs+3) = 0.39;
+ * *(violin->sin_coefs+4) = 0.45;
+ * *(violin->sin_coefs+5) = 0.35;
+ * *(violin->sin_coefs+6) = 0.5;
+ * *(violin->sin_coefs+7) = 0.4;
+ * *(violin->sin_coefs+8) = 0.35;
+ * *(violin->sin_coefs+9) = 0.1; */
violin->sound = violin_sound;
violin->merger = simple_merger;