Sounds

Reference ❯ Playing and Generating Sound Effects

Sounds

sound( name, seed ) top ↑

Syntax

sound( name )
sound( name, seed )
sound( parameterTable )
sound( buffer )

Calling this function immediately plays either a randomly generated sound with a type specified by name or using the given parameters.

sound() uses sfxr to randomly generate sounds of a given type. The name parameter is a string specifying the type of sound (see related items), and the seed parameter allows you to choose a particular random sound. Sounds with the same name and seed always sound the same. If no seed is specified the resulting sound will be a random sound of the specified type. If a parameterTable is passed in, those values are used to generate the sound.

name

string, the type of sound to play

can be SOUND_BLIT, SOUND_EXPLODE, SOUND_HIT, SOUND_JUMP, SOUND_PICKUP, SOUND_POWERUP, SOUND_RANDOM or SOUND_SHOOT.

Can also be DATA followed by a base64 string encoding the parameters which is generated by Codea based on the sound picker panel properties and should not be edited

seed

int, specifies the random seed to use when generating a sound of this type

buffer

soundbuffer, specifies the raw sound data to play

parameterTable

table, specifies the parameters to use when generating a sound. This is an advanced option, the parameters will change the noise in subtle ways. Play with the Sounds Plus example app to see what these parameters do. Any missing keys will cause the sound to use a default value for that parameter

The table can contain the following (case sensitive) keys:

Waveform - The synthesizer waveform to use, can be SOUND_NOISE, SOUND_SAWTOOTH, SOUND_SINEWAVE, or SOUND_SQUAREWAVE

AttackTime - number

SustainTime - number

SustainPunch - number

DecayTime - number

StartFrequency - number

MinimumFrequency - number

Slide - number

DeltaSlide - number

VibratoDepth - number

VibratoSpeed - number

ChangeAmount - number

ChangeSpeed - number

SquareDuty - number

DutySweep - number

RepeatSpeed - number

PhaserSweep - number

LowPassFilterCutoff - number

LowPassFilterCutoffSweep - number

LowPassFilterResonance - number

HighPassFilterCutoff - number

HighPassFilterCutoffSweep - number

Volume - number

Examples

-- play a random jump sound sound( SOUND_JUMP )
-- play a specific jump sound sound( SOUND_JUMP, 1234 )
-- using the table to define properties sound( { Waveform = SOUND_NOISE, AttackTime = 1.2, SustainTime = 1 } )

soundbuffer( data, format, freq ) top ↑

Syntax

soundbuffer( data, format, freq )

This object represents a sound buffer containing arbitrary audio data. You may create soundbuffer objects using soundbuffer( data, format, freq ) where the data is uncompressed PCM audio and the format is one of FORMAT_MONO8, FORMAT_MONO16, FORMAT_STEREO8, or FORMAT_STEREO16.

data

string, uncompressed audio data

format

FORMAT_MONO8, FORMAT_MONO16, FORMAT_STEREO8 or FORMAT_STEREO16

freq

integer, the frequency of the data

Examples

-- Creating a sound buffer object function setup() tap = false parameter.integer("freq",1,4000,800) parameter.number("length",0.1,1,0.5) end function makeBuffer() local data = "" datum="\0\xAD\" numSamples = freq * length for i = 1,numSamples/#datum do data = data .. datum end return soundbuffer( data, FORMAT_MONO8, freq ) end function touched(touch) if touch.state == ENDED and touch.tapCount == 1 then tap = true end end function draw() background(0) if tap then b = makeBuffer() sound(b) tap = false end end

Returns

A new soundbuffer object

soundBufferSize( size ) top ↑

Syntax

soundBufferSize( size )
soundBufferSize()

Calling this function will set the maximum buffer size for sounds. Sounds are stored in a buffer so that they do not need to be recreated if reused. Sounds are removed from this buffer on a least recently used basis when the buffer is full and a new sound is added. For example, if the buffer is 2 megabytes (the default), and we add a sound that puts it over the limit, the sound we used the longest time ago is removed from the buffer and will have to be re-created if played again. Generating the sound data can take a considerable amount of time for longer sounds.Playing a sound at 0 volume counts as playing and can be used to keep a sound in the cache (and to pre-generate a sound in the setup function.) Calling this method with 0 for the size sets the buffer to no limit. This can cause Codea to crash if too many sounds are generated as it may run out of memory.Calling this with no parameters returns the max size and current size of the buffer.

size

number, the maximum size of the buffer in megabytes. This can be a fraction. If this is 0, the buffer size is unlimited. An unlimited buffer size can cause Codea to crash if it runs out of memory, so take care if it is used.

Examples

-- setting the sound buffer maximum size soundBufferSize( 3 )
-- reading the current maximum size and used size maxSize, usedSize = nsoundBufferSize()
-- setting to no limit soundBufferSize(0)

Returns

If no parameters are given, a pair of values: maxBufferSize, currentBufferSize

SOUND_JUMP top ↑

Syntax

SOUND_JUMP

This constant specifies a jump sound. Similar to a character jumping in a platform game.

Returns

The string "jump"

SOUND_HIT top ↑

Syntax

SOUND_HIT

This constant specifies a hit sound. For example, when the enemy collides with the player.

Returns

The string "hit"

SOUND_PICKUP top ↑

Syntax

SOUND_PICKUP

This constant specifies a pickup sound. For example, collecting coins in a game.

Returns

The string "pickup"

SOUND_POWERUP top ↑

Syntax

SOUND_POWERUP

This constant specifies a powerup sound. For example, collecting bonuses in a game.

Returns

The string "powerup"

SOUND_SHOOT top ↑

Syntax

SOUND_SHOOT

This constant specifies a shooting sound. For example, firing a bullet in a game.

Returns

The string "shoot"

SOUND_EXPLODE top ↑

Syntax

SOUND_EXPLODE

This constant specifies an explosion sound. For example, a space ship blowing up.

Returns

The string "explode"

SOUND_BLIT top ↑

Syntax

SOUND_BLIT

This constant specifies a generic "blit" sound.

Returns

The string "blit"

SOUND_RANDOM top ↑

Syntax

SOUND_RANDOM

This constant specifies a randomly generated sound. You can use this in conjunction with the seed value of the sound() function to find a sound that you like.

Returns

The string "random"

SOUND_NOISE top ↑

Syntax

SOUND_NOISE

This specifices to use a white noise function as the waveform for this sound

Returns

The integer 3

SOUND_SAWTOOTH top ↑

Syntax

SOUND_SAWTOOTH

This specifices to use a sawtooth function as the waveform for this sound

Returns

The integer 1

SOUND_SINEWAVE top ↑

Syntax

SOUND_SINEWAVE

This specifices to use a sine wave function as the waveform for this sound

Returns

The integer 2

SOUND_SQUAREWAVE top ↑

Syntax

SOUND_SQUAREWAVE

This specifices to use a square wave function as the waveform for this sound

Returns

The integer 0