CS 330 Lecture 20 – File I/O and Quiz
Agenda
- writing a WAV file
- the characteristics of imperative
Code
mucis.c
N.B. The fwrite calls below have their middle arguments transposed. Element size comes before the number of elements.
#include <stdio.h> #include <stdlib.h> #include <math.h> /* ------------------------------------------------------------------------- */ const int SAMPLES_PER_SECOND = 22050; const int BEATS_PER_MINUTE = 200; const float PI = 3.14159f; /* ------------------------------------------------------------------------- */ /** Convert a note name into a serial step number. C0 has step number 0, C+0 is 1, D is 2, C1 is 12, C2 is 24, etc. Once the step numbers are known, it is easy to calculate the number of half steps between any two notes. This difference is needed in order to calculate the frequency of a note. @param name Name of note, one of [ABCDEFG] followed by an optional + or - to make the note sharp or flat @param octave Which octave this note is in (octaves span C to B) @return The step number of this note @see get_frequency */ int get_serial_id(const char *name, int octave) { // Here are the step numbers for C, D, E, F, G, A, and B in the 0th octave. static int map[12] = {9, 11, 0, 2, 4, 5, 7}; // Name[0] is [A-G]. We can transform that letter into it's index by taking // away 'A'. int step = map[name[0] - 'A']; // If we're sharp or flat, let's adjust a half-step. if (name[1] == '+') { ++step; } else if (name[1] == '-') { --step; } // We've got the note step. Now let's move to the right level. step += 12 * octave; return step; } /* ------------------------------------------------------------------------- */ /** Get the frequency of the given note. @param name Name of note, one of [ABCDEFG] followed by an optional + or - to make the note sharp or flat @param octave Which octave this note is in (octaves span C to B) @return The frequency of the note, in cycles per second */ float get_frequency(const char *name, int octave) { // This formula is from: http://www.phy.mtu.edu/~suits/NoteFreqCalcs.html. const float F0 = 440.0f; const float A = powf(2.0f, 1.0f / 12.0f); int n = get_serial_id(name, octave) - get_serial_id("A", 4); return F0 * powf(A, n); } /* ------------------------------------------------------------------------- */ /** Generates a clip of the specified frequency of the specified duration. @param frequency The number of cycles per second for this element. Use 0 to produce a rest. @param duration Duration of clip. Is 1 for a whole note, 2 for half, 4 for quarter, 8 for eighth, 16 for sixteenth, and 32 for thirty-second. @param nsamples An out variable used to record the number of samples in the array @return A dynamically-allocated array of samples. Must be freed by caller. */ short *generate_clip(float frequency, int duration, int *nsamples) { const float BEATS_PER_SECOND = BEATS_PER_MINUTE / 60.0f; const float SECONDS_PER_BEAT = 1.0f / BEATS_PER_SECOND; const float SECONDS_PER_WHOLE_NOTE = SECONDS_PER_BEAT * 4; // How many samples are in a note of this durations? const float nseconds = SECONDS_PER_WHOLE_NOTE / duration; *nsamples = (int) ceilf(nseconds * SAMPLES_PER_SECOND); short *samples = (short *) malloc(sizeof(short) * *nsamples); // Sample. float cycles_per_sample = frequency / SAMPLES_PER_SECOND; for (int i = 0; i < *nsamples; ++i) { samples[i] = (short) (sin(2.0f * PI * i * cycles_per_sample) * 32767.0f); } // If we just butt clips up right against each other, we'll have a very // abrupt frequency change. This leads to popping in the audio. To ameliorate // this problem, let's dampen the end of this clip. We take the last chunk of // the note, which has a timespan of 1/32 of a whole note, and progressively // diminish it. const float NSECONDS_PER_BREAK = SECONDS_PER_WHOLE_NOTE / 32.0f; const int NSAMPLES_PER_BREAK = (int) ceilf(NSECONDS_PER_BREAK * SAMPLES_PER_SECOND); for (int i = *nsamples - NSAMPLES_PER_BREAK; i < *nsamples; ++i) { samples[i] = (short) (samples[i] * ((*nsamples - i) / (float) NSAMPLES_PER_BREAK)); } return samples; } /* ------------------------------------------------------------------------- */ int main(int argc, char **argv) { int nsamples; float frequency = get_frequency(argv[1], 3); short *samples = generate_clip(frequency, 1, &nsamples); FILE *f = fopen("out.wav", "wb"); int i; short s; fprintf(f, "RIFF"); i = 2 * nsamples + 36; fwrite(&i, 1, sizeof(int), f); fprintf(f, "WAVE"); fprintf(f, "fmt "); i = 16; fwrite(&i, 1, sizeof(int), f); s = 1; fwrite(&s, 1, sizeof(short), f); fwrite(&s, 1, sizeof(short), f); i = SAMPLES_PER_SECOND; fwrite(&i, 1, sizeof(int), f); i = SAMPLES_PER_SECOND * 2; fwrite(&i, 1, sizeof(int), f); s = 2; fwrite(&s, 1, sizeof(short), f); s = 16; fwrite(&s, 1, sizeof(short), f); fprintf(f, "data"); i = 2 * nsamples; fwrite(&i, 1, sizeof(int), f); fwrite(samples, nsamples, sizeof(short), f); fclose(f); return 0; }