OSC Audio Oscillator

osc is a real-time audio signal generator program. It transmits a sine wave to the default sound device in a jitter-free endless loop. Default parameters are: sampling rate 44.1 KHz, format 16-bit linear PCM.


osc frequency dB_level

frequency specifies the tone frequency in Hz, with a resolution of 0.1 Hz.
dB_level specifies the amplitude in decibels relative to full scale.


osc has been tested on Microsoft Windows 2000, XP, Vista, and 7. On Linux, it uses the ALSA sound application programming interface and libalsatoss to access the OSS sound system.

Theory of operation

To generate a periodic signal such as a sinusoidal tone, the program constructs a signal segment that contains samples representing at least one complete period, and usually more than one period, of the signal. The goal is to generate a sampled data segment that contains an exact integral multiple of the desired period, so that the equipment can play it back in a loop without incurring a phase error at the loop boundaries or anywhere else in the sampled waveform.

The length of the required signal segment depends on the audio sampling rate and the signal period. If the sampling rate is R samples per second, and the desired frequency (inverse of the period) of the signal is a multiple of 1 Hz, the signal segment can be exactly R samples, or one second, in length. The sampled output waveform can be constructed as follows. Start with an array, A, {A_0, A_1, ..., A_R-1} of R samples that contains exactly one period of the signal. If the equipment plays that array repeatedly, it will have a frequency of exactly 1 Hz and will have no phase sampling error.

To get a 2 Hz signal, suppose we copy every other sample from the array A into another array, B. The B array will contain {B_0 = A_0, B_1 = A_2, B_2 = A_4, ...}. When the copy reaches B_R/2 = A_R, the program subtracts R from the array index in A and continues copying from A to B until there are R samples in B. The new waveform in B contains two full cycles and has a frequency of 2 Hz.

In general, copying every f-th sample yields f Hz. B always contains R samples of the f Hz wave. Since R times f is congruent to 0, modulo R, then for every integer f, the f-Hertz wave fits perfectly into the 1-second long array B.

For some frequencies the copying process may reach an exact zero crossing point earlier than R samples, but in general the program cannot expect that. The lengths of the arrays can be increased to achieve a guaranteed frequncy resolution that is finer than 1 Hz. For example, if the arrays contain 10R samples, the frequency resolution of the oscillator will be 0.1 Hz.

The URL of this file is http://www.moshier.net/osc.html.
Last update: 14 July 2011