Plays and records sound samples in a variety of different data formats.
The Sound class provides an interface for the playback of audio sample files. By default, all loading and saving of sound data is in WAVE format. Other audio formats such as MP3 can be supported through Sound class extensions, if available.
Automatic streaming is enabled by default. If an attempt is made to play an audio file that exceeds the maximum buffer size, it will be streamed from the source location. Streaming behaviour can be modified via the Stream field.
The following example illustrates playback of a sound sample that is one octave higher than its normal frequency. The subscription to the OnStop callback will result in the program waking once the sample has finished playback.
local snd = obj.new('sound', { path = 'audio:samples/doorbell.wav', note = 'C6', onStop = function(Sound) proc.signal() end }) snd.acActivate() proc.sleep()
The Sound class consists of the following fields:
Access | Name | Type | Comment | ||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Active | INT | Returns true if the sound sample is being played back. | |||||||||||||||||||||||||||||
Audio | OBJECTID | Refers to the audio object/device to use for playback. | |||||||||||||||||||||||||||||
Set this field if a specific Audio object should be targeted when playing the sound sample. | |||||||||||||||||||||||||||||||
BitsPerSample | INT | Indicates the sample rate of the audio sample, typically 8 or 16 bit. | |||||||||||||||||||||||||||||
BytesPerSecond | INT | The flow of bytes-per-second when the sample is played at normal frequency. | |||||||||||||||||||||||||||||
This field is set on initialisation. It indicates the total number of bytes per second that will be played if the sample is played back at its normal frequency. | |||||||||||||||||||||||||||||||
ChannelIndex | INT | Refers to the channel that the sound is playing through. | |||||||||||||||||||||||||||||
This field reflects the audio channel index that the sound is currently playing through, or has most recently played through. | |||||||||||||||||||||||||||||||
Compression | INT | Determines the amount of compression used when saving an audio sample. | |||||||||||||||||||||||||||||
Setting the Compression field will determine how much compression is applied when saving an audio sample. The range of compression is 0 to 100%, with 100% being the strongest level available while 0% is uncompressed and loss-less. This field is ignored if the file format does not support compression. | |||||||||||||||||||||||||||||||
Duration | DOUBLE | Returns the duration of the sample, measured in seconds. | |||||||||||||||||||||||||||||
Flags | SDF | Optional initialisation flags. | |||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||
Frequency | INT | The frequency of a sampled sound is specified here. | |||||||||||||||||||||||||||||
This field specifies the frequency of the sampled sound data. If the frequency cannot be determined from the source, this value will be zero. Note that if the playback frequency needs to be altered, set the Playback field. | |||||||||||||||||||||||||||||||
Header | BYTE [] | Contains the first 128 bytes of data in a sample's file header. | |||||||||||||||||||||||||||||
The Header field is a pointer to a 128 byte buffer that contains the first 128 bytes of information read from an audio file on initialisation. This special field is considered to be helpful only to developers writing add-on components for the sound class. The buffer that is referred to by the Header field is not populated until the Init action is called on the sound object. | |||||||||||||||||||||||||||||||
Length | INT | Indicates the total byte-length of sample data. | |||||||||||||||||||||||||||||
This field specifies the length of the sample data in bytes. To get the length of the sample in seconds, divide this value by the BytesPerSecond field. | |||||||||||||||||||||||||||||||
LoopEnd | INT | The byte position at which sample looping will end. | |||||||||||||||||||||||||||||
When using looped samples (via the | |||||||||||||||||||||||||||||||
LoopStart | INT | The byte position at which sample looping begins. | |||||||||||||||||||||||||||||
When using looped samples (via the Note that the LoopStart variable does not affect the position at which playback occurs for the first time - it only affects the restart position when the end of the sample is reached. | |||||||||||||||||||||||||||||||
Note | STRING | The musical note to use when playing a sound sample. | |||||||||||||||||||||||||||||
Set the Note field to alter the playback frequency of a sound sample. Setting this field as opposed to the Playback frequency will assure that the sample is played at a correctly scaled tone. The Note field can be set using either string or integer based format. If using the integer format, the chosen value will reflect the position on a musical keyboard. A value of zero refers to the middle C key. Each octave is measured in sets of 12 notes, so a value of 24 would indicate a C note at 3 times normal playback. To play at lower values, simply choose a negative integer to slow down sample playback. Setting the Note field with the string format is useful if human readability is valuable. The correct format is The middle C key for this format is
| |||||||||||||||||||||||||||||||
Octave | INT | The octave to use for sample playback. | |||||||||||||||||||||||||||||
The Octave field determines the octave to use when playing back a sound sample. The default setting is zero, which represents the octave at which the sound was sampled. Setting a negative octave will lower the playback rate, while positive values raise the playback rate. The minimum octave setting is The octave can also be adjusted by setting the Note field. Setting the Octave field directly is useful if you need to quickly double or halve the playback rate. | |||||||||||||||||||||||||||||||
OnStop | FUNCTION | This callback is triggered when sample playback stops. | |||||||||||||||||||||||||||||
Set OnStop to a callback function to receive an event trigger when sample playback stops. The prototype for the function is The timing of this event does not guarantee precision, but should be accurate to approximately 1/100th of a second in most cases. | |||||||||||||||||||||||||||||||
Pan | DOUBLE | Determines the horizontal position of a sound when played through stereo speakers. | |||||||||||||||||||||||||||||
The Pan field adjusts the "horizontal position" of a sample that is being played through stereo speakers. The default value for this field is zero, which plays the sound through both speakers at an equal level. The minimum value is | |||||||||||||||||||||||||||||||
Path | STRING | Location of the audio sample data. | |||||||||||||||||||||||||||||
This field must refer to a file that contains the audio data that will be loaded. If creating a new sample with the | |||||||||||||||||||||||||||||||
Playback | INT | The playback frequency of the sound sample can be defined here. | |||||||||||||||||||||||||||||
Set this field to define the exact frequency of a sample's playback. The playback frequency can be modified at any time, including during audio playback if real-time adjustments to a sample's audio output rate is desired. | |||||||||||||||||||||||||||||||
Position | BIGINT | The current playback position. | |||||||||||||||||||||||||||||
The current playback position of the audio sample is indicated by this field. Writing to the field will alter the playback position, either when the sample is next played, or immediately if it is currently playing. | |||||||||||||||||||||||||||||||
Priority | INT | The priority of a sound in relation to other sound samples being played. | |||||||||||||||||||||||||||||
The playback priority of the sample is defined here. This helps to determine if the sample should be played when all available mixing channels are busy. Naturally, higher priorities are played over samples with low priorities. The minimum priority value allowed is -100, the maximum is 100. | |||||||||||||||||||||||||||||||
Stream | STREAM | Defines the preferred streaming method for the sample. | |||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||
Volume | DOUBLE | The volume to use when playing the sound sample. | |||||||||||||||||||||||||||||
The field specifies the volume of a sound in the range 0 - 1.0 (low to high). Setting this field during sample playback will dynamically alter the volume. |
The following actions are currently supported:
Name | Comment | |||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Activate | Plays the audio sample. | |||||||||||||||||||||||||
Deactivate | Stops the audio sample and resets the playback position. | |||||||||||||||||||||||||
Disable | Disable playback of an active audio sample, equivalent to pausing. | |||||||||||||||||||||||||
Enable | Continues playing a sound if it has been disabled. | |||||||||||||||||||||||||
GetKey | Retrieve custom key values. | |||||||||||||||||||||||||
ERR acGetKey(*Object, CSTRING Key, STRING Value, LONG Size)
The following custom key values are formally recognised and may be defined automatically when loading sample files:
| ||||||||||||||||||||||||||
Read | Read decoded audio from the sound sample. | |||||||||||||||||||||||||
ERR acRead(*Object, APTR Buffer, LONG Length, LONG *Result)
This action will read decoded audio from the sound sample. Decoding is a live process and it may take some time for all data to be returned if the requested amount of data is considerable. The starting point for the decoded data is determined by the Position value. | ||||||||||||||||||||||||||
SaveToObject | Saves audio sample data to an object. | |||||||||||||||||||||||||
ERR acSaveToObject(*Object, OBJECTID Dest, CLASSID ClassID)
| ||||||||||||||||||||||||||
Seek | Moves the cursor position for reading data. | |||||||||||||||||||||||||
ERR acSeek(*Object, DOUBLE Offset, LONG Position)
Use Seek to move the read cursor within the decoded audio stream and update Position. This will affect the Read action. If the sample is in active playback at the time of the call, the playback position will also be moved. | ||||||||||||||||||||||||||
SetKey | Define custom tags that will be saved with the sample data. | |||||||||||||||||||||||||
ERR acSetKey(*Object, CSTRING Key, CSTRING Value)
|
Definitions for the Note field. An 'S' indicates a sharp note.
Name | Description |
---|---|
NOTE::A | |
NOTE::AS | |
NOTE::B | |
NOTE::C | |
NOTE::CS | |
NOTE::D | |
NOTE::DS | |
NOTE::E | |
NOTE::F | |
NOTE::FS | |
NOTE::G | |
NOTE::GS | |
NOTE::OCTAVE |
Sound flags
Name | Description |
---|---|
SDF::LOOP | Enables sample looping. The LoopStart and LoopEnd fields determine where the looping area lies within the sample data. |
SDF::NEW | Create the sample from scratch (e.g. for audio recording purposes). |
SDF::RESTRICT_PLAY | Restricts playback so that the sound can never play on more than one channel at any given time. |
SDF::STEREO | Indicates that the sound has multiple audio channels. |
SDF::STREAM | Read-Only. Indicates that streaming is enabled. |
Streaming options
Name | Description |
---|---|
STREAM::ALWAYS | Stream if the sample length exceeds 64k. |
STREAM::NEVER | No streaming - load all data into memory. |
STREAM::SMART | Smart streaming is the default. If the sample appears to be relatively small with respect to available system RAM, it will be loaded into memory. Otherwise it will be streamed. |
Class Info | |
---|---|
ID | ID_SOUND |
Category | Audio |
Include | modules/sound.h |
Version | 1 |