Next: 2.3 Description of all Up: 2 The system Previous: 2.1 Overview of system

# 2.2 MIDAS concepts

This section briefly describes basic MIDAS concepts and terminology.

## 2.2.1 Samples

A sample in MIDAS Sound System includes the sample data for a sound sample, plus information on its data type, the sample length and looping information. It does not contain other attributes such as volume or panning -- these are instrument properties. In MIDAS, samples are managed and stored by by Sound Devices. A sample is identified by a sample handle, and this sample handle can be used to play the sample or remove it from Sound Device storage.

## 2.2.2 Channels

In MIDAS Sound System, channels are used to play the sound. Each channel can play one sample or stream at a time, either mono or stereo. When a new sound is played on a channel, the old one is removed. A channel is identified by a channel number, which range from zero upwards.

Before any sound can be played, a number of channels must be opened from the Sound Device. MIDAS supports an unlimited number of channels on all platforms, unless hardware mixing is used. Although the number of open channels has some impact on CPU usage, the amount of CPU power used depends mostly on the channels that actually play sound. Therefore it is much more important to ensure that no unnecessary sounds are left playing than it is to minimize the number of open channels.

## 2.2.3 Panning

Panning in MIDAS Sound System affects the apparent position of the sound in stereo environments. Sound panned to ``left'' will be played from the left speaker, ``right'' from the right one and ``middle'' from both. 64 different panning positions are available between ``left'' and ``middle'' plus ``right'' and ``middle'', to give smooth control on the sound position.

In addition, surround sound is also considered a separate panning position. Sounds played with their panning position set to ``surround'' are played from the surround speakers if the computer is connected to a surround decoder. Even if surround equipment is not available, the sound appears to come from around the listener's head, not from inside it as in ``middle'' panning position.

Note that not all Sound Devices necessarily support surround sound. Furthermore, if the computer is playing in stereo mode, but connected to mono equipment, all sounds played in ``surround'' will disappear!

## 2.2.4 Software and hardware mixing

Most sound cards supported by MIDAS Sound System are only capable of playing one digital sound channel at a time, but still MIDAS supports an unlimited number of channels with them. This is accomplished through software mixing of the sound -- the sound channels are mixed digitally together before passing the sound to the sound card.

Mixing the sound in software is a complicated process, and, although MIDAS mixing routines are carefully optimized, can still take a considerable amount of CPU time. The CPU time used is determined by four factors: The number of channels active, the mixing rate, the output mode and the type of the samples played. The time used depends almost linearly on the mixing rate and the number of channels, and stereo output can take up to 50% more CPU time than mono. The sample type effect is almost the same - stereo samples can take up to 50% more CPU than mono ones, and 16-bit samples 50% more than 8-bit.

The opposite of software mixing is hardware mixing. Hardware mixing sound cards are capable of playing multiple digital sounds in hardware, and thus there is no need to mix the sound in software. This often uses much less CPU power, depending on the sound card, but as a tradeoff some flexibility is lost. One problem with hardware mixing cards is, that samples have to be placed in on-card memory. Very often the cards come with as little as 512kb of memory, and thus can store only a few samples. The second problem is, that playing streams with hardware mixing cards is usually impossible. This severely limits the usability of hardware mixing cards with MIDAS.

## 2.2.5 Streams

In MIDAS, streams are continuous flows of sample data. Unlike samples, streams are not stored in the Sound Devices themselves, but are instead read from the system memory as they are played. Because of this, the complete data for the stream does not need to be available when playing starts, but can instead be read from disk, or even generated on the fly, as playback proceeds. The playback properties of the stream -- sampling rate, volume and panning -- can be changed in real time just like those of a sample. However, as the sample data for the stream is not stored anywhere in the Sound Device, each stream can only be played once.

[TODO: picture]

Internally to the system, streams are implemented as circular sound data buffers. New sound data is written to the buffer, and the buffer write position is set to the end of the new data. This informs the Sound Device which area of the buffer is ready to be played. As the Sound Device plays the data from the buffer, the updated read position is available from function SoundDevice.GetPosition().

However, this implementation is completely invisible for application developers. Instead, the MIDAS API offers three simple ways for playing streams: file playback, polling playback and callbacks. For file playback, the application simply supplies the system the name of the file containing the sample data for the stream, and playback proceeds automatically. In polling playback mode, the application opens the stream and periodically feeds new sample data to be played. In callback mode, the application sets a callback function that is called each time the stream buffer loops, and can there fill the buffer with new sample data.

The most obvious use for streams is to play long sections of digital sound or music directly from disk, without needing to load everything into memory first. Stream playback could also be used to mix the output of a separate sound generator, such as a speech synthesizer, with the rest of the sound and music in the system.

Currently streams can only be played with software mixing Sound Devices. In addition, file playback is only possible under operating systems that support multithreading.

Next: 2.3 Description of all Up: 2 The system Previous: 2.1 Overview of system