//begin main int main (void) { short waveform [TOTAL_SAMPLES] ; FILE *f ; f = wavfile_open("music.wav") ; if(!f) { printf("couldn't open music.wav for writing: %s",strerror(errno)) ; return 1 ; } char input ; float octavefactor = 1 ; float notelength = .5 * WAVFILE_SAMPLES_PER_SECOND ; //quarter note is default note length double frequency ; printf("Please enter valid instructions for octaves, notes, rests, and their lengths. Enter X for end of song.\n") ; do { scanf(" %c", &input) ; if (input == 'X') break ; int change = determine_change_type(input) ; int volume = 30000 ; switch (change) { case 0: printf("Error. Invalid character entered. File was written ignoring this invalid data.\n") ; break ; case 1: frequency = letters_to_frequency(input) * octavefactor ; update_waveform(waveform, volume, frequency, notelength, f) ; //only updated when a note is read break ; case 2: octavefactor *= change_octave(input) ; break ; case 3: notelength = new_notelength(input)*WAVFILE_SAMPLES_PER_SECOND ; break ; case 4: insert_rest(waveform, notelength, f) ; } } while (input != 'X' ) ; wavfile_close(f) ; return 0 ; }
int main(int argc, char* argv[]) { PaError err = paNoError; int device_id; BOOL rv; err = Pa_Initialize(); if (err != paNoError) { fprintf(stderr, "Pa_Initialize() failed...\n"); exit(1); } if (argc != 3) { printf("usage: %s devide_id wav_file\n\n", argv[0]); print_deviceinfo(); Pa_Terminate(); return 1; } rv = wavfile_open(argv[2]); if (rv == FALSE) { fprintf(stderr, "wav file open failed...\n"); Pa_Terminate(); exit(1); } device_id = atoi(argv[1]); rv = audio_start(device_id); if (rv == FALSE) { fprintf(stderr, "AudioDevice initialize failed...device_id=%d\n", device_id); Pa_Terminate(); return 1; } while (1) { Pa_Sleep(100); if (finish_flag == TRUE) break; } audio_stop(); Pa_Terminate(); wavfile_close(); return 0; }
int main() { short waveform[NUM_SAMPLES]; double frequency = 440.0; int volume = 32000; FILE * f; int length = NUM_SAMPLES; int i; f = wavfile_open("sound.wav"); if(!f) { printf("couldn't open sound.wav for writing: %s",strerror(errno)); return 1; } for(i=0;i<length;i = i+4) { double t = (double) i / WAVFILE_SAMPLES_PER_SECOND; waveform[i] = volume*sin(frequency*t*2*M_PI); } for(i=1;i<length;i = i + 4) { double t = (double) i / WAVFILE_SAMPLES_PER_SECOND; waveform[i] = (volume*(.3333))*sin(frequency*(.3333)*t*2*M_PI); } for(i=2;i<length;i = i + 4) { double t = (double) i / WAVFILE_SAMPLES_PER_SECOND; waveform[i] = (volume*(.5))*sin(frequency*(.5)*t*2*M_PI); } for(i=3;i<length;i = i+4) { double t = (double) i / WAVFILE_SAMPLES_PER_SECOND; waveform[i] = volume*.25*sin(frequency*.25*t*2*M_PI); } wavfile_write(f,waveform,length); wavfile_close(f); return 0; }
int main (void) { short waveform[NUM_SAMPLES]; double frequency = 440.0; int octave = 1; int volume = 30000; int length = NUM_SAMPLES; char c; FILE * f = wavfile_open("music.wav"); while (c!='X') { c = receive_input(); switch (c) { default: printf("%d is not a valid input\n", c); break; case 'a': frequency = 415.30 * octave; create_note(volume, length, frequency, waveform, f); break; case 'A': frequency = 440.00 * octave; create_note(volume, length, frequency, waveform, f); break; case 'b': frequency = 466.16 * octave; create_note(volume, length, frequency, waveform, f); break; case 'B': frequency = 493.88 * octave; create_note(volume, length, frequency, waveform, f); break; case 'C': create_note(volume, length, frequency, waveform, f); frequency = 523.25 * octave; break; case 'd': frequency = 554.37 * octave; create_note(volume, length, frequency, waveform, f); break; case 'D': frequency = 587.33 * octave; create_note(volume, length, frequency, waveform, f); break; case 'e': frequency = 622.25 * octave; create_note(volume, length, frequency, waveform, f); break; case 'E': frequency = 659.26 * octave; create_note(volume, length, frequency, waveform, f); break; case 'F': frequency = 698.46 * octave; create_note(volume, length, frequency, waveform, f); break; case 'g': frequency = 739.99 * octave; create_note(volume, length, frequency, waveform, f); break; case 'G': frequency = 783.99 * octave; create_note(volume, length, frequency, waveform, f); break; case '.': frequency = 0; create_note(volume, length, frequency, waveform, f); break; case '+': octave*=2; break; case '-': octave/=2; break; case '1': length = NUM_SAMPLES; break; case '2': length = NUM_SAMPLES/2; break; case '4': length = NUM_SAMPLES/4; break; case '8': length = NUM_SAMPLES/8; break; case '6': length = NUM_SAMPLES/16; break; case 'X': wavfile_close(f); break; } } return 0; }