spat3dt
Prev | Orchestra Opcodes and Operators | Next

## spat3dt

spat3dt — Can be used to render an impulse response for a 3D space at i-time.

## Description

This opcode positions the input sound in a 3D space, with optional simulation of room acoustics, in various output formats. spat3dt can be used to render the impulse response at i-time, storing output in a function table, suitable for convolution.

## Syntax

    spat3dt ioutft, iX, iY, iZ, idist, ift, imode, irlen [, iftnocl]

## Initialization

ioutft -- Output ftable number for spat3dt. W, X, Y, and Z outputs are written interleaved to this table. If the table is too short, output will be truncated.

iX -- Sound source X coordinate in meters (positive: right, negative: left)

iY -- Sound source Y coordinate in meters (positive: front, negative: back)

iZ -- Sound source Z coordinate in meters (positive: up, negative: down)

idist -- For modes 0 to 3, idist is the unit circle distance in meters. For mode 4, idist is the distance between microphones.

The following formulas describe amplitude and delay as a function of sound source distance from microphone(s):

amplitude = 1 / (0.1 + distance)

delay = distance / 340 (in seconds)

Distance can be calculated as:

distance = sqrt(iX^2 + iY^2 + iZ^2)

In Mode 4, distance can be calculated as:

distance from left mic = sqrt((iX + idist/2)^2 + iY^2 + iZ^2)
distance from right mic = sqrt((iX - idist/2)^2 + iY^2 + iZ^2)

With spat3d the distance between the sound source and any microphone should be at least (340 * 18) / sr meters. Shorter distances will work, but may produce artifacts in some cases. There is no such limitation for spat3di and spat3dt.

Sudden changes or discontinuities in sound source location can result in pops or clicks. Very fast movement may also degrade quality.

ift -- Function table storing room parameters (for free field spatialization, set it to zero or negative). Table size is 54. The values in the table are:

Room Parameter | Purpose
--- | ---
0 | Early reflection recursion depth (0 is the sound source, 1 is the first reflection etc.) for spat3d and spat3di. The number of echoes for four walls (front, back, right, left) is: N = (2*R + 2) * R. If all six walls are enabled: N = (((4*R + 6)*R + 8)*R) / 3
1 | Late reflection recursion depth (used by spat3dt only). spat3dt skips early reflections and renders echoes up to this level. If early reflection depth is negative, spat3d and spat3di will output zero, while spat3dt will start rendering from the sound source.
2 | imdel for spat3d. Overrides opcode parameter if non-negative.
3 | irlen for spat3dt. Overrides opcode parameter if non-negative.
4 | idist value. Overrides opcode parameter if >= 0.
5 | Random seed (0 - 65535) -1 seeds from current time.
6 - 53 | wall parameters (w = 6: ceil, w = 14: floor, w = 22: front, w = 30: back, w = 38: right, w = 46: left)
w + 0 | Enable reflections from this wall (0: no, 1: yes)
w + 1 | Wall distance from listener (in meters)
w + 2 | Randomization of wall distance (0 - 1) (in units of 1 / (wall distance))
w + 3 | Reflection level (-1 - 1)
w + 4 | Parametric equalizer frequency in Hz.
w + 5 | Parametric equalizer level (1.0: no filtering)
w + 6 | Parametric equalizer Q (0.7071: no resonance)
w + 7 | Parametric equalizer mode (0: peak EQ, 1: low shelf, 2: high shelf)

imode -- Output mode

- 0: B format with W output only (mono)

  aout = aW

- 1: B format with W and Y output (stereo)

  aleft = aW + 0.7071*aY
  aright = aW - 0.7071*aY

- 2: B format with W, X, and Y output (2D). This can be converted to UHJ:

  aWre, aWim hilbert aW
  aXre, aXim hilbert aX
  aYre, aYim hilbert aY
  aWXr = 0.0928*aXre + 0.4699*aWre
  aWXiYr = 0.2550*aXim - 0.1710*aWim + 0.3277*aYre
  aleft = aWXr + aWXiYr
  aright = aWXr - aWXiYr

- 3: B format with all outputs (3D)

- 4: Simulates a pair of microphones (stereo output)

  aW butterlp aW, ifreq ; recommended values for ifreq
  aY butterlp aY, ifreq ; are around 1000 Hz
  aleft = aW + aX
  aright = aY + aZ

Mode 0 is the cheapest to calculate, while mode 4 is the most expensive.

In Mode 4, The optional lowpass filters can change the frequency response depending on direction. For example, if the sound source is located left to the listener then the high frequencies are attenuated in the right channel and slightly increased in the left. This effect can be disabled by not using filters. You can also experiment with other filters (tone etc.) for better effect.

Note that mode 4 is most useful for listening with headphones, and is also more expensive to calculate than the B-format (0 to 3) modes. The idist parameter in this case sets the distance between left and right microphone; for headphones, values between 0.2 - 0.25 are recommended, although higher settings up to 0.4 may be used for wide stereo effects.

More information about B format can be found here: http://www.york.ac.uk/inst/mustech/3d_audio/ambis2.htm

irlen -- Impulse response length of echoes (in seconds). Depending on filter parameters, values around 0.005-0.01 are suitable for most uses (higher values result in more accurate output, but slower rendering)

iftnocl (optional, default=0) -- Do not clear output ftable (mix to existing data) if set to 1, clear table before writing if set to 0 (default: 0).

## Examples

Here is an example of the spat3dt opcode. It uses the file spat3dt.csd.

Example 929. Example of the spat3dt opcode.

See the sections Real-time Audio and Command Line Flags for more information on using command line flags.

    <CsoundSynthesizer>
    <CsOptions>
    ; Select audio/midi flags here according to platform
    -odac     ;;;realtime audio out
    ;-iadc    ;;;uncomment -iadc if realtime audio input is needed too
    ; For Non-realtime ouput leave only the line below:
    ; -o spat3dt.wav -W ;;; for file output any platform
    </CsOptions>
    <CsInstruments>

    sr = 44100
    ksmps = 32
    nchnls = 2
    0dbfs  = 1

    garvb   init 0
    gaW     init 0
    gaX     init 0
    gaY     init 0

    itmp ftgen 1, 0, 64, -2, 2, 40, -1, -1, -1, 123,          \
               1, 13.000, 0.05, 0.85, 20000.0, 0.0, 0.50, 2,   \
               1,  2.000, 0.05, 0.85, 20000.0, 0.0, 0.25, 2,   \
               1, 16.000, 0.05, 0.85, 20000.0, 0.0, 0.35, 2,   \
               1,  9.000, 0.05, 0.85, 20000.0, 0.0, 0.35, 2,   \
               1, 12.000, 0.05, 0.85, 20000.0, 0.0, 0.35, 2,   \
               1,  8.000, 0.05, 0.85, 20000.0, 0.0, 0.35, 2

    itmp ftgen 2, 0, 262144, -2, 0
         spat3dt 2, -0.2, 1, 0, 1, 1, 2, 0.005

    itmp ftgen 3, 0, 262144, -52, 3, 2, 0, 4, 2, 1, 4, 2, 2, 4

    instr 1

    a1   vco2 1, 440, 10
    kfrq port 100, 0.008, 20000
    a1   butterlp a1, kfrq
    a2   linseg 0, 0.003, 1, 0.01, 0.7, 0.005, 0, 1, 0
    a1   =  a1 * a2 * 2
         denorm a1
         vincr garvb, a1
    aw, ax, ay, az spat3di a1, p4, p5, p6, 1, 1, 2
         vincr gaW, aw
         vincr gaX, ax
         vincr gaY, ay

    endin

    instr 2

    denorm garvb
    ; skip as many samples as possible without truncating the IR
    arW, arX, arY ftconv garvb, 3, 2048, 2048, (65536 - 2048)
    aW     =  gaW + arW
    aX     =  gaX + arX
    aY     =  gaY + arY
    garvb  =  0
    gaW    =  0
    gaX    =  0
    gaY    =  0

    aWre, aWim hilbert aW
    aXre, aXim hilbert aX
    aYre, aYim hilbert aY
    aWXr    =  0.0928*aXre + 0.4699*aWre
    aWXiYr  =  0.2550*aXim - 0.1710*aWim + 0.3277*aYre
    aL      =  aWXr + aWXiYr
    aR      =  aWXr - aWXiYr
            outs aL, aR

    endin

    </CsInstruments>
    <CsScore>

    i 1 0 0.5  0.0  2.0 -0.8
    i 1 1 0.5  1.4  1.4 -0.6
    i 1 2 0.5  2.0  0.0 -0.4
    i 1 3 0.5  1.4 -1.4 -0.2
    i 1 4 0.5  0.0 -2.0  0.0
    i 1 5 0.5 -1.4 -1.4  0.2
    i 1 6 0.5 -2.0  0.0  0.4
    i 1 7 0.5 -1.4  1.4  0.6
    i 1 8 0.5  0.0  2.0  0.8
    i 2 0 10
    e

    </CsScore>
    </CsoundSynthesizer>

## See Also

spat3d, spat3di

## Credits

Author: Istvan Varga
2001

New in version 4.12

Updated April 2002 by Istvan Varga

Prev | Up | Next
spat3di | Home | spdist