Reggae tutorial: Playing a synthesized, continuous wave
From MorphOS Library
Grzegorz Kraszewski
Contents
Introduction
This tutorial shows how raw sound data may be played with Reggae. The example code synthesizes a 1 kHz sine wave and plays it continuously. The wave is synthesized into a table in memory. Then memory.stream is used to access it. The next object, an instance of rawaudio.filter, attaches audio parameters to the raw data. Finally audio.output plays the wave in a endless loop, using its looping feature.
Raw sine wave synthesis
In theory a single period of sine wave is enough to play it continuously. There are two reasons for using more periods however. The first is number of samples in one period. We want to play a 1 kHz wave sampled at 44.1 kHz. Then one period would contain 44.1 samples, which is not integer obviously. The second reason is processing overhead of tight loop. Every loop turn takes additional processing time, so it is better, when the loop is longer.
Taking both these reasons into account, 100 periods of the sine are generated into the table containing 4410 samples, which makes 0.1 second of sound. The generation is then straightforward and uses sin() function from the standard C math library. As the argument of sin() is in radians (so one period is 2π), it goes from 0 to 200π in 4410 steps (without the last value). The amplitude of the sine wave (which is normally 1.0) is scaled to 16-bit signed range by multiplying by 32767.
Using memory.stream for synthesis buffer
Basic usage of memory.stream has been discussed in "Playing a Sound From Memory" tutorial. MMA_StreamHandle attribute is used to pass the memory table address, MMA_StreamLength one takes the table size in bytes (note, it is not the same as the table size, as every element of the table ocuppies two bytes). Note also, MMA_StreamLength is 64-bit attribute, and as such is passed as a pointer to a QUAD variable containing the value.