RedKCS:
Filter:
redSys/Classes (extension) | Red > redTools

RedKCS
ExtensionExtension

kansas city standard encoder/decoder for sclang and server

Description

audio frequency-shift keying (AFSK) as used by old computers to store data on cassette. See https://en.wikipedia.org/wiki/Kansas_City_standard

Class Methods

.new

.newSine

a new encoder/decoder using square or sine waves.

Arguments:

sampleRate

defaults to the server's sample rate.

baudRate

can go lower and higher. lower can be interesting sound-wise. higher for faster transmission.

zeroFreq

in Hz. the standard specifies 1200 which mean that at 300 bauds there will be four pulses/cycles for marking a zero bit.

oneFreq

in Hz. the standard specifies 2400 which mean that at 300 bauds there will be eight pulses/cycles for marking a one bit.

addNewlineGap

optional. add ten extra NULL characters at the end of each line.

addCarriageReturn

optional. add an extra Char.ret (ascii 13) for each Char.nl (ascii 10).

leaderDur

in seconds. lead-in data (stream of 1).

trailerDur

in seconds. trailing data (stream of 1).

Discussion:

600 baud is possible without increasing the zeroFreq and oneFreq values. for 1200 baud use frequencies of 2400 and 4800.

Instance Methods

language side encoding

.encodeByte

convert an 8-bit value into an array of samples.

NOTE: normally no need to call this method directly.

Arguments:

byte

an Integer 0-255.

Returns:

an array of samples. for square waves an Int8Array and for sines a FloatArray.

.encodeString

convert a string into an array of samples.

NOTE: normally no need to call this method directly.

Arguments:

str

a String.

Returns:

a FloatArray of samples. either square waves or sines.

.encodeStringAsBuffer

convert a string into a buffer that can be played.

NOTE: it's easier to use the -play method below.

Arguments:

server

defaults to Server.default.

str

the string to be encoded.

action

a Function for what to do when the buffer is created.

Returns:

the Buffer instance (if successful). note that the buffer will not be filled with samples yet - use the action function.

.encodeStringAsSoundFile

convert a string into a sound file (render to disk).

Arguments:

str

the string to be encoded.

path

a string specifying where to save the resulting sound file.

Returns:

a Boolean indicating if creating the file was successful.

language side decoding

.decodeFloatArray

convert an array of sample values into an array of 8-bit bytes.

NOTE: normally no need to call this method directly.

Arguments:

arr

a FloatArray.

rate

sample rate of the data. if not specified -sampleRate will be used.

channels

might need to adapt this if the samples in the array are interleaved (stereo).

Returns:

.decodeBuffer

convert a buffer of sample values into a string.

Arguments:

buf

the Buffer instance to decode.

raw

a Boolean indicating if output should be the raw bytes (Int8Array) or a String (with any NULL characters removed).

action

a Function that will be called when the decoding is finished. the string or array with raw bytes is passed in as the first argument.

.decodeSoundFile

convert a sound file into a string.

Arguments:

path

a String pointing at the sound file to be decoded. mono or multi-channel does not matter - only the first (left) channel will be used.

raw

a Boolean indicating if output should be the raw bytes (Int8Array) or a String (with any NULL characters removed).

Returns:

a String or an Int8Array. nil if unsuccessful.

server side encoding

.play

.playSine

start realtime generation using square or sine waves.

Arguments:

target

the Group or Server to add the node to.

out

bus to play on.

amp

volume

addAction

a Symbol.

.send

encode and generate sound in realtime.

Arguments:

str

the String to send.

.stop

turn off realtime generation

server side decoding

.listen

start realtime analysis.

Arguments:

target

the Group or Server to add the node to.

action

a Function that will be called when and if the decoding is finished. a string is passed in as the first argument.

in

bus to listen to. use s.options.numOutputBusChannels for the mic (same as SoundIn.ar(0))

thresh

internal analyser synth's amplitude threshold (+/-).

rq

internal analyser synth's bandpass filter bandwidth.

addAction

a Symbol.

NOTE: defaults to \addToTail because normally the internal analyser synth needs to be below any sound generating synths in the order of execution

.stopListening

free internal analyser synth and responder. also a CmdPeriod will clear these.

Examples