summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKlara Modin <klarasmodin@gmail.com>2020-08-28 16:01:33 +0200
committerKlara Modin <klarasmodin@gmail.com>2020-08-28 16:01:33 +0200
commit5cb4b2dd03af08d7116d02683e2b211c7f898d40 (patch)
tree6a5eec921ec77a396b18b87d1e572af1bfb244f4
parenta7e29639ca734495b5ef30079ab46225940f831c (diff)
adapt more functions to instrument
-rw-r--r--genwav.c61
1 files changed, 32 insertions, 29 deletions
diff --git a/genwav.c b/genwav.c
index c860ecf..f51b452 100644
--- a/genwav.c
+++ b/genwav.c
@@ -146,23 +146,23 @@ void put_tone(struct wav_header *header, int16_t data[], double(*func)(double),
}
}
-void put_scale(struct wav_header *header, int16_t data[], double(*func)(double), double frequency, int16_t amplitude, uint32_t length)
+void put_scale(struct wav_header *header, int16_t data[], struct instrument *instr, double frequency, int16_t amplitude, uint32_t length)
{
int split = 9;
- put_tone(header, data, func, frequency, 0xfff, length/split);
- put_tone(header, data+D_LEN(header,length)/split, func, 9*frequency/8, 0xfff, length/split);
- put_tone(header, data+2*D_LEN(header,length)/split, func, 5*frequency/4, 0xfff, length/split);
- put_tone(header, data+3*D_LEN(header,length)/split, func, 4*frequency/3, 0xfff, length/split);
- put_tone(header, data+4*D_LEN(header,length)/split, func, 3*frequency/2, 0xfff, length/split);
- put_tone(header, data+5*D_LEN(header,length)/split, func, 5*frequency/3, 0xfff, length/split);
- put_tone(header, data+6*D_LEN(header,length)/split, func, 15*frequency/8, 0xfff, length/split);
- put_tone(header, data+7*D_LEN(header,length)/split, func, 2*frequency, 0xfff, length/split);
+ put_instrument(header, instr, frequency, amplitude, data, length/split);
+ put_instrument(header, instr, 9*frequency/8, amplitude, data+D_LEN(header,length)/split, length/split);
+ put_instrument(header, instr, 5*frequency/4, amplitude, data+2*D_LEN(header,length)/split, length/split);
+ put_instrument(header, instr, 4*frequency/3, amplitude, data+3*D_LEN(header,length)/split, length/split);
+ put_instrument(header, instr, 3*frequency/2, amplitude, data+4*D_LEN(header,length)/split, length/split);
+ put_instrument(header, instr, 5*frequency/3, amplitude, data+5*D_LEN(header,length)/split, length/split);
+ put_instrument(header, instr, 15*frequency/8, amplitude, data+6*D_LEN(header,length)/split, length/split);
+ put_instrument(header, instr, 2*frequency, amplitude, data+7*D_LEN(header,length)/split, length/split);
}
-void put_scale_general(struct wav_header *header, int16_t data[], double(*func)(double), double frequency, int16_t amplitude, uint32_t length, int divisions)
+void put_scale_general(struct wav_header *header, int16_t data[], struct instrument *inst, double frequency, int16_t amplitude, uint32_t length, int divisions)
{
for (int i = 0; i < divisions+1; ++i) {
- put_tone(header, data+i*D_LEN(header,length)/(divisions+1), func, frequency*pow(2, i/(double)divisions), amplitude, length/((double)divisions+1));
+ put_instrument(header, inst, frequency*pow(2, i/(double)divisions), amplitude, data+i*D_LEN(header,length)/(divisions+1),length/((double)divisions+1));
}
}
@@ -235,6 +235,7 @@ void put_something(struct wav_header *header, int16_t data[], struct instrument
if (!l1) l1++;
if (!l2) l2++;
int dur = (gcd(l1,l2) * header->sample_rate/10) % (3*header->sample_rate/2);
+ dur *= 2;
if (pos+dur >= length || pos > pos+dur || dur > pos+dur)
break;
f = getchar();
@@ -249,16 +250,6 @@ void put_something(struct wav_header *header, int16_t data[], struct instrument
}
printf("length=%ld, pos=%ld, dur=%d, freq=%f, amp=%d\n", length, pos, dur, freq, amp);
put_instrument(header, inst, freq, amp, data+D_LEN(header,pos), dur);
- /*
- //put_fm(header, &data[pos], &sin, freq, amp/16, &sin, freq*0.98, amp/16, dur);
- int16_t *tmp = calloc(sizeof(*tmp), dur);
- put_tone(header, tmp, &sin, 220, amp/64, dur);
- mod_fm(header, tmp, &sin, freq, amp, dur);
- for (int i = 0; i < dur; ++i) {
- data[pos+i] += tmp[i];
- }
- free(tmp);
- */
pos += dur;
} while (pos < length);
}
@@ -270,12 +261,12 @@ int main(int argc, char *argv[])
file = argv[1];
FILE *out = fopen(file, "w");
struct wav_header *header = header_init();
- size_t length = header->sample_rate*header->channels*header->bits_per_sample/8*20;
+ size_t length = header->sample_rate*header->channels*header->bits_per_sample/8*10;
int16_t *data = calloc(length, sizeof(*data));
header_update(header, length);
struct instrument *inst = instrument_init(2);
- struct instrument_part part = {.mod_func=&sin, .frequency=220, .amplitude=1/64.0, .amp_kind=Coeff};
+ struct instrument_part part = {.mod_func=&half_sine, .frequency=220, .amplitude=1/64.0, .amp_kind=Coeff};
instrument_add_part(inst, &part);
part.frequency=1;
part.amplitude=1;
@@ -284,16 +275,26 @@ int main(int argc, char *argv[])
part.amp_kind=Coeff;
instrument_add_part(inst, &part);
- //put_instrument(header, inst, 440, 0x7ff, data, length);
- //put_instrument(header, inst, 440*3/2, 0x7ff, data, length);
+ struct instrument *inst2 = instrument_init(2);
+ struct instrument_part part2 = {.mod_func=&pseudo_sawtooth, .frequency=1000, .amplitude=1/16.0, .amp_kind=Coeff};
+ instrument_add_part(inst2, &part2);
+ part2.frequency=1/2.0;
+ part2.amplitude=1;
+ part2.mod_func=&sin;
+ part2.freq_kind=Coeff;
+ part2.amp_kind=Coeff;
+ instrument_add_part(inst2, &part2);
+
+ //put_instrument(header, inst2, 440, 0x7ff, data, length);
+ //put_instrument(header, inst2, 440*5/2, 0x7ff, data, length);
int frequency = 440;
//put_tone(header, data, &sin, frequency, 0x7ff, length/2);
//put_tone(header, data+D_LEN(header,length/2), &sin, frequency*2, 0x7ff, length/2);
- //put_scale_general(header, data, &sin, frequency, 0x7ff, length, 12);
- //put_scale(header, data, &sin, frequency, 0x7ff, length);
- put_something(header, data, inst, frequency, 0x7ff, length, 24);
- //put_something(header, data, inst, frequency*5/2, length, 0x7ff, 48);
+ //put_scale_general(header, data, inst2, frequency, 0x7ff, length, 12);
+ //put_scale(header, data, inst, frequency, 0x7ff, length);
+ //put_something(header, data, inst, frequency, 0x7ff, length, 24);
+ //put_something(header, data, inst2, frequency, 0x7ff, length, 48);
fwrite(header, sizeof(*header), 1, out);
fwrite(data, sizeof(*data), length, out);
@@ -302,6 +303,8 @@ int main(int argc, char *argv[])
free(data);
free(inst->parts);
free(inst);
+ free(inst2->parts);
+ free(inst2);
return 0;
}