/* ks.c version 0.03, october 31, 2006. Simple Karplus-Strong implementation in C. Latest version available at https://ecomaan.nl/c/ks/ Copyright (c) 2004 - 2006 - Pieter Suurmond Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. Any person wishing to distribute modifications to the Software is requested to send the modifications to the original developer so that they can be incorporated into the canonical version. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. Difference equation y[n] = 0.49609375 (x[n] + y[n-100] + x[n-1] + y[n-101]) ---------- 0.49609375 | | | | -1 v v x[n] ---> + ---> z --> + ---> * ----> y[n] ^ | | -100 | -- z <-------------- */ #include #include #include "wraiff.h" /* Add wraiff.c to your project or makefile. */ short karplus_strong (short x) { #define MAXDEL (128) /* MUST be a power of 2. */ #define BITMASK (MAXDEL-1) /* Circular buffer. */ static short y[MAXDEL]; /* Hidden global variables (static). */ static long ap; static short n; /* Index, initialisation not necessary. */ long a, b; /* No need to remeber a and b. */ a = (long)x + y[(n - 100) & BITMASK]; /* Read from buffer. */ b = ((a + ap) * 127) >> 8; /* Coeff = +0.49609375. */ ap = a; /* Remember. */ y[n++ & BITMASK] = (short)b; /* Write in buffer. */ return (short)b; } void drain_string (long frames) { while (frames--) /* Drain internal KS buffer for a while. */ karplus_strong(0); /* (Don't record output to file yet.) */ } /* Simulate the plucked string model for 'frames' number of sampleframes, and of course, PLUCK it. Audio output is written to WRAIFF object, created prior to this call. */ int pluck_string (WRAIFFp a, long frames) { short inp; long fr; short audio; /* Audio 'buffer' of 1 (mono-)sample. */ for (fr = 0L; fr < frames; fr++) { if (fr < 60L) /* 60 sample white noise BURST. */ inp = (short)((rand() & 32767) - 16384); else /* No more input (silence). */ inp = 0; audio = karplus_strong(inp); if (WRAIFF_short (a, &audio, 1)) /* One single frame to disk */ return 1; } return 0; } /* Open an AIFF file and write some audio to it. */ int main(void) { int e, i; WRAIFFp a; if (sizeof(short) != 2) { printf("Sorry, 'short' must be 16 bits to run this code!\n"); return 13; } e = WRAIFF_open(&a, /* Receive pointer to WRAIFF-object in a. */ "ks.aiff", /* Filename. */ 11025, /* Samplerate (Hertz). */ 1, /* Number of interleaved channels. */ 16); /* Number of bits resolution. */ if (e) printf("Cannot create audiofile!\n"); else { drain_string (10000); /* Drain noise from internal buffer. */ pluck_string (a, 8000); /* Ignore possible error-return codes */ pluck_string (a, 4000); /* from pluck_string() for now. */ pluck_string (a, 4000); pluck_string (a, 8000); pluck_string (a, 4000); pluck_string (a, 4000); pluck_string (a, 2000); /* Getting irregular. */ pluck_string (a, 2100); pluck_string (a, 2200); pluck_string (a, 2300); pluck_string (a, 2400); pluck_string (a, 3000); pluck_string (a, 16000); /* Having fun with that drone. */ for (i = 1; i <= 2000; i++) /* "KkGggrrRRrrrrrrRrrr... ." */ if (pluck_string (a, 1000 + (rand() % i))) { printf("Error in function pluck_string()!\n"); break; } pluck_string (a, 44100); /* 4 seconds for final decay. */ WRAIFF_info(a, stdout); /* Print file statistics to stdout. */ e = WRAIFF_close(&a); /* Free WRAIFF-object and set a to NULL. */ if (e) printf("Error during closing audiofile!\n"); else printf("Ok.\n"); } return e; }