Skip to content

Instantly share code, notes, and snippets.

@rubyist
Last active December 21, 2015 02:08
Show Gist options
  • Save rubyist/6232485 to your computer and use it in GitHub Desktop.
Save rubyist/6232485 to your computer and use it in GitHub Desktop.
Generate different kinds of wave forms
long maxSampleCount = SAMPLE_RATE * DURATION;
long sampleCount = 0;
UInt32 bytesToWrite = 2;
double wavelengthInSamples = SAMPLE_RATE / hz;
while (sampleCount < maxSampleCount) {
// for (int i = 0; i < wavelengthInSamples; i++) {
// // Square wave
// SInt16 sample;
// if (i < wavelengthInSamples/2) {
// sample = CFSwapInt16HostToBig(SHRT_MAX);
// } else {
// sample = CFSwapInt16HostToBig(SHRT_MIN);
// }
//
// audioErr = AudioFileWriteBytes(audioFile, false, sampleCount*2, &bytesToWrite, &sample);
// assert(audioErr == noErr);
// sampleCount++;
// }
// for (int i=0; i<wavelengthInSamples; i++) {
// // saw wave
// SInt16 sample = CFSwapInt16HostToBig(((i / wavelengthInSamples) * SHRT_MAX * 2) - SHRT_MAX);
// audioErr = AudioFileWriteBytes(audioFile, false, sampleCount*2, &bytesToWrite, &sample);
// assert (audioErr == noErr);
// sampleCount++;
// }
for (int i=0; i<wavelengthInSamples; i++) {
// sine wave
SInt16 sample = CFSwapInt16HostToBig((SInt16) SHRT_MAX * sin(2*M_PI * (i/wavelengthInSamples)));
audioErr = AudioFileWriteBytes(audioFile, false, sampleCount*2, &bytesToWrite, &sample);
assert (audioErr == noErr);
sampleCount++;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment