int main() // main function { square_wave(26, 0, 10); // P26, ch0, 10 Hz. pause(1000); // Pause 1 second int cycles = count(26, 1000); // Count for 1 second print("cycles = %d\n", cycles); // Report on/off cycles // Negative pin clears signal and lets go of I/O pin. square_wave(-26, 0, 0); }
static void snd_callback(void *userdata, Uint8 * stream, int len) { static int lasttime; static float lastfreq; int i; swsndupdate(); // lasttime stores the time offset from the last call // we save the time so that the multiple time slices // all fit together smoothly // if we have changed frequency since last time, we need // to adjust lasttime to the new frequency lasttime *= (int)(lastfreq / current_freq); for (i = 0; i < len; ++i) { if (speaker_on) { stream[i] = 127 * (int)square_wave(current_freq * (i + lasttime)); } else { stream[i] = 0; } } lasttime += len; lastfreq = current_freq; }
/** This function gets the next audio sample **/ short SOUND_get_next_sample() { static short buffer; //Do we have something to play? if( current_note == SILENCE || current_note == EOT ) { return wave_mode == TRIANGLE ? buffer : 0; } if( wave_mode == TRIANGLE ) buffer = triangle_wave(); if( wave_mode == SQUARE ) buffer = square_wave(); if( wave_mode == SAWTOOTH ) buffer = sawtooth_wave(); //No sound return buffer; }