/* Example C++ class KS by Pieter Suurmond, march 3, 2004 - 2024 COPYLEFT. Let's build a guitar. */ using namespace std; #include // Because wraiff.h needs to know about type FILE. #include // Because we use rand() below to produce white noise. #include // For our 'sinful' waveshaper. #include "wraiff.hxx" // Struct definition of WRAIFF objects. #include "ks.hxx" // Class definition of KS objects. /* Returns pseudo random between -1.0 (incl) and +1.0 (excl), if t is between start (incl) and end (excl), otherwise returns zero. */ double white_noise_burst(long t, long start, long end) { double result; // Between time interval. if ((t >= start) && (t < end)) result = ((double)((rand() & 32767) - 16384)) / 16384.0; else result = 0.0; // Otherwise silence. return result; } int main(void) { const double sr = 44100.0; KS string_E(sr / 82.5), // A single KS object tuned to 82.5 Hertz. string_A(sr / 110.0), // And another 5 of them,... yes, a guitar. string_D(sr / 146.7), // We tune with some natural (3:4) quarts. string_g(sr / 195.6), string_b(sr / 247.5), string_e(sr / 330.0); WRAIFFp a; // Pointer to our audio file object. int e = WRAIFF_open(&a, // Receive ptr to WRAIFF-object in a. "ks.aiff", // Filename. (long)sr, // Samplerate (in Hertz). 1, // Number of interleaved channels. 16); // Number of bits resolution. if (e) { printf("Cannot create file!\n"); return 1; } for (short annoy = 0L; annoy < 10; annoy++) // Stroke our guitar 10 times. { for (long t = 0L; t < 2*(long)sr; t++) { // Excite at times: double output = (string_E.process(white_noise_burst(t, 0, 300)) + string_A.process(white_noise_burst(t, 5000, 5300)) + string_D.process(white_noise_burst(t, 10000, 10200)) + string_g.process(white_noise_burst(t, 15000, 15200)) + string_b.process(white_noise_burst(t, 20000, 20100)) + string_e.process(white_noise_burst(t, 25000, 25100)) ) / 3.0; if (WRAIFF_double (a, &output, 1)) // Write one single audioframe to disk. { printf("Error writing to AIFF file.\n"); break; } } } // Now we suddenly grrkk..click-on our 'sinful' fuzz pedal. :-) // First we simulate our cracky footswitch (white noise is a little too bright though). for (long t = 0L; t < 500; t++) { double output = (string_E.process(0.0) + string_A.process(0.0) + string_D.process(0.0) + string_g.process(0.0) + string_b.process(0.0) + string_e.process(0.0)) / 8.0; output += 0.8 * white_noise_burst(t, 200, 300); if (WRAIFF_double (a, &output, 1)) { printf("Error writing to AIFF file.\n"); break; } } // Then we listen to the fuzz itself for 2 seconds (not playing anything new yet). for (long t = 0L; t < 2*(long)sr; t++) { double output = (string_E.process(0.0) + string_A.process(0.0) + string_D.process(0.0) + string_g.process(0.0) + string_b.process(0.0) + string_e.process(0.0) ) * 3.0; output = 0.8 * sin(output); // Dirty 'sinful' waveshaper. if (WRAIFF_double (a, &output, 1)) // One single frame to disk. { printf("Error writing to AIFF file.\n"); break; } } // Now playing with the fuzz... not AT ALL annoying... :-( for (short annoy = 0L; annoy < 10; annoy++) { for (long t = 0L; t < 2*(long)sr; t++) { // Excite at times: double output = (string_E.process(white_noise_burst(t, 0, 300)) + string_A.process(white_noise_burst(t, 5000, 5300)) + string_D.process(white_noise_burst(t, 10000, 10200)) + string_g.process(white_noise_burst(t, 15000, 15200)) + string_b.process(white_noise_burst(t, 20000, 20100)) + string_e.process(white_noise_burst(t, 25000, 25100)) ) * 3.0; output = 0.8 * sin(output); if (WRAIFF_double (a, &output, 1)) { printf("Error writing to AIFF file.\n"); break; } } } // Let it decay for 40 seconds... for (long t = 0L; t < 40*(long)sr; t++) { double output = (string_E.process(0.0) + string_A.process(0.0) + string_D.process(0.0) + string_g.process(0.0) + string_b.process(0.0) + string_e.process(0.0) ) * 3.0; output = 0.8 * sin(output); if (WRAIFF_double (a, &output, 1)) { printf("Error writing to AIFF file.\n"); break; } } WRAIFF_info(a, stdout); /* Print file statistics to stdout. */ e = WRAIFF_close(&a); /* Close AIFF file and release object. */ if (e) printf("Error during closing of the file!\n"); else printf("Ok.\n"); return 0; }