Copyright (c) Hyperion Entertainment and contributors.

AmigaOS Manual: Sounds

From AmigaOS Documentation Wiki
Revision as of 00:00, 22 September 2017 by Janne Peräaho (talk | contribs)
Jump to navigation Jump to search

Sound card configured in the AHI Preferences can be used for playing and recording sound. Modern sound cards allow recording from several sources: through the line in and AUX connectors, microphone connector, CD connector, etc. Your sound card may also provide several outputs to choose from, and in addition you can route the audio output to a file. To select your preferred input and output channels, open the AHI Preferences editor and make your choice.

Playing

The AUDIO: device allows playing raw (SIGNED), AIFF, or AIFC formatted sound samples. The procedure of playing a sound sample is as follows:

  1. Open the AUDIO: device for writing.
  2. Write the sound sample to the device.
  3. Close the device.

The AUDIO: device expects to receive an 8 bit mono AIFF or AIFC formatted data stream. It will play the stream at the rate of 8000 Hz, with full volume, using the AUDIO: device unit 0.

You can change all those parameters when opening the AUDIO: device. These are the properties you can configure:

Keyword Template Description
BITS or B B=BITS/K/N Number of bits per sample. Default is 8.
CHANNELS or C C=CHANNELS/K/N Number of channels to use: 1 = mono audio, 2 = stereo audio. Default is 1.
FREQUENCY or F F=FREQUENCY/K/N Playback frequency. Default is 8000.
TYPE or T T=TYPE/K Type of the audio stream: SIGNED, AIFF, or AIFC. The AUDIO: device will detect the stream format automatically if not set.
VOLUME or V V=VOLUME/K/N Playback volume: 0-100. 0 mutes the playback and 100 is the full volume. Default is 100.
POSITION or P P=POSITION/K/N Stereo panning: -100 = far left, 0 = center, 100 = far right. Default is 0.
PRIORITY or PRI PRI=PRIORITY/K/N Playback priority: -128 to 127. If priority of 127 is used, no other playback request can interrupt the current playback. Default is 0.
LENGTH or L L=LENGTH/K/N Limits the number of bytes the AUDIO: device will receive. Default is no limit.
SECONDS or S S=SECONDS/K/N Limits the time AUDIO: device will be reading the audio stream. Default is no limit.
BUFFER or BUF BUF=BUFFER/K/N Size of the playback buffer in bytes. Default is 32768.
UNIT UNIT/K/N Device unit to be used. Default is 0.

If you supply several parameters when opening the device, the parameters must be separated with slashes (/). For example:

OPEN( 'audio', 'AUDIO:FREQUENCY=22050/VOLUME=50/PRIORITY=1', 'W' )

Program 16. Play.rexx

/*
** Play an AIFF audio file
*/
 
fileName   = 'recording.aiff' /* Audio file name */
frequency  = 22050            /* Playback frequency */
volume     = 100              /* Full volume */
unit       = 0                /* Use device unit 0 */
 
/* Playback properties */
properties = 'TYPE=AIFF/'
properties = properties || 'FREQUENCY=' || frequency || '/'
properties = properties || 'VOLUME=' || volume || '/'
properties = properties || 'UNIT=0'
 
/* Open audio stream */
IF ( ~OPEN( 'audio', 'AUDIO:' || properties, 'W' ) ) THEN DO
    SAY 'Failed to open audio stream.'
    exit 0
END
 
/* Open audio file */
IF ( OPEN( 'file', fileName, 'R' ) ) THEN DO
    /* Play */
    DO UNTIL ( EOF( 'file' ) = 1 )
        buffer = READCH( 'file', 4096 )
        WRITECH( 'audio', buffer )
    END
 
    /* Close audio file */
    CLOSE( 'file' )
END
ELSE DO
    SAY 'Failed to open file "' || fileName || '".'
END
 
/* Close stream */
CLOSE( 'audio' )

Recording

As with playing, three audio formats are available for recording: raw (SIGNED), AIFF, and AIFC. The overall procedure of recording is similar to playing, except instead of writing to the AUDIO: device you read from the device:

  1. Open the AUDIO: device for reading.
  2. Read sound sample from the device.
  3. Close the device.

By default the AUDIO: device outputs an 8 bit mono raw (SIGNED) sound through the device unit 0. The used sampling rate is 8000 Hz.

Different recording settings can be requested when opening the AUDIO: device. The following settings are available:

Keyword Template Description
BITS or B B=BITS/K/N Number of bits per sample. Default is 8.
CHANNELS or C C=CHANNELS/K/N Number of channels to use: 1 = mono audio, 2 = stereo audio. Default is 1.
FREQUENCY or F F=FREQUENCY/K/N Sampling frequency. Default is 8000.
TYPE or T T=TYPE/K Sample format: SIGNED, AIFF, or AIFC. Default is SIGNED.
LENGTH or L L=LENGTH/K/N Limit the sound sample length (number of bytes). The default sample length is extremely long.
SECONDS or S S=SECONDS/K/N Limit the recording time (seconds). The default recording time is very long.
BUFFER or BUF BUF=BUFFER/K/N Size of the recording buffer in bytes. Default is 32768.
UNIT UNIT/K/N Device unit to be used. Default is 0.

If you supply several parameters when opening the device, the parameters must be separated with slashes (/). For example:

OPEN( 'audio', 'AUDIO:BITS=16/CHANNELS=2/FREQUENCY=22050/SECONDS=10', 'R' )

Program 17. Record.rexx

/*
** Record an AIFF audio file
*/
 
fileName   = 'recording.aiff' /* Audio file name */
duration   = 5                /* Recording time (seconds) */
frequency  = 22050            /* Sampling frequency */
unit       = 0                /* Use device unit 0 */
 
/* Recording properties */
properties = 'TYPE=AIFF/'
properties = properties || 'SECONDS=' || duration || '/'
properties = properties || 'FREQUENCY=' || frequency || '/'
properties = properties || 'BITS=16' || '/'
properties = properties || 'CHANNELS=2' || '/'
properties = properties || 'UNIT=0'
 
/* Open audio stream */
IF ( ~OPEN( 'audio', 'AUDIO:' || properties, 'R' ) ) THEN DO
    SAY 'Failed to open audio stream.'
    EXIT 0
END
 
/* Open audio file */
IF ( OPEN( 'file', fileName, 'W' ) ) THEN DO
    /* Open console stream for printing */
    OPEN( 'console', '*', 'W' )
 
    /* Wait for enter */
    WRITECH( 'console', 'Press enter to start recording.' )
    PARSE PULL input
    WRITECH( 'console', '    REC (·)' || COPIES( d2c( 8 ), 2 ) )
 
    counter = 0
 
    /* Record */
    DO UNTIL ( EOF( 'audio' ) = 1 )
        buffer = READCH( 'audio', 4096 )
        WRITECH( 'file', buffer )
 
        /* Animate recording indicator */
        counter = counter + 1
        IF ( counter // 2 = 0 ) THEN DO
            WRITECH( 'console', '·' || d2c( 8 ) )
            counter = 0
        END
        ELSE DO
            WRITECH( 'console', 'x' || d2c( 8 ) )
        END
    END
    WRITECH( 'console', '·' )
 
    /* Close console stream */
    CLOSE( 'console ' )
 
    /* Close audio file */
    CLOSE( 'file' )
 
    SAY ''
    SAY 'Recorded ' || duration || ' seconds to file "' || fileName || '".'
END
ELSE DO
    SAY 'Failed to open file "' || fileName || '" for writing.'
END
 
/* Close stream */
CLOSE( 'audio' )