Copyright (c) Hyperion Entertainment and contributors.

AmigaOS Manual: Sounds

From AmigaOS Documentation Wiki
Revision as of 23:24, 9 August 2017 by Janne Peräaho (talk | contribs) (Added 'Playing' section)
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 B=BITS/K/N Number of bits per sample. Default is 8.
CHANNELS C=CHANNELS/K/N Number of channels to use: 1 = mono audio, 2 = stereo audio. Default is 1.
FREQUENCY F=FREQUENCY/K/N Playback frequency. Default is 8000.
TYPE 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 V=VOLUME/K/N Playback volume: 0-100. 0 mutes the playback and 100 is the full volume. Default is 100.
POSITION P=POSITION/K/N Stereo panning: -100 = far left, 0 = center, 100 = far right. Default is 0.
PRIORITY 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 L=LENGTH/K/N Limits the number of bytes the AUDIO: device will receive. Default is no limit.
SECONDS S=SECONDS/K/N Limits the time AUDIO: device will be reading the audio stream. Default is no limit.
BUFFER 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 (/).

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

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' )