Programmer Guide/Macro Library/BSeq: Difference between revisions

From STX Wiki
Jump to navigationJump to search
No edit summary
No edit summary
Line 19: Line 19:
;Result: always '''0'''
;Result: always '''0'''
;Description: Assign sequence attributes. An attribute value is assigned only if it is valid. For some attributes automatic corrections are applied. Multiple attribute assignments separated by semi-colons can be specified. Attribute names and value keywords can be abbreviated.  
;Description: Assign sequence attributes. An attribute value is assigned only if it is valid. For some attributes automatic corrections are applied. Multiple attribute assignments separated by semi-colons can be specified. Attribute names and value keywords can be abbreviated.  
 
;Supported attributes:
 
Supported attributes:
:* <code>channel=</code><var>n</var> &rarr; set the number of channels (1 &le; <var>n</var> &le; 256)
:* <code>channel=</code><var>n</var> &rarr; set the number of channels (1 &le; <var>n</var> &le; 256)
:* <code>envelope=<var>type,ton,toff</var></code> &rarr; set global envelope type ('''none''', '''linear''' or '''cosine''') and the on/off time (in ms, &ge; 0).
:* <code>envelope=<var>type,ton,toff</var></code> &rarr; set global envelope type ('''none''', '''linear''' or '''cosine''') and the on/off time (in ms, &ge; 0).
Line 32: Line 30:
;Result: value of specified attribute or an empty string
;Result: value of specified attribute or an empty string
;Description: Retrieve a sequence attribute.
;Description: Retrieve a sequence attribute.
 
;Supported attributes:
 
Supported attributes:
:* <code>channel</code> &rarr; get number of channels
:* <code>channel</code> &rarr; get number of channels
:* <code>envelope</code> &rarr; get global envelope definition (<var>type,ton,toff</var>)
:* <code>envelope</code> &rarr; get global envelope definition (<var>type,ton,toff</var>)
Line 42: Line 38:
:* <code>table</code> &rarr; get signal definition table
:* <code>table</code> &rarr; get signal definition table


===Signal===
;Usage: <code><var>bseq_instance</var> signal <var>ch ; base ; time ; sigtype ; sigpars</var></code>
:;<var>ch</var>: sequence channel number (1, 2, ..), '''0''' or '''*''' for all channels
:;<var>base</var>: defines the base (origin) for the signal begin time; possible values are '''absolut''', '''begin''' and '''end'''
:;<var>time</var>: the offset time to the selected base (origin) in seconds
:;<var>sigtype ; sigpars</var>: set the type and parameters of the new signal (see below)
;Result: 0 for success and a non-zero value if failed
;Description: Define a new sequence signal and add it to the sequence definition. If <var>ch</var> is set to '''0''' or '''*''' the new signal is added to each sequence channel, otherwise it is added only to the specified channel. The timebase <var>base</var> selects how the time value <var>time</var> is used. If it is set to '''absolute''', <var>time</var> is an absolute time (0 = begin of the signal). If <var>base</var> is set to '''begin''' or '''end''', <var>time</var> is an offset to the beginning or end of the previous signal. The signal itself is defined by <var>sigtype</var> and <var>sigpars</var>.


{|class="einrahmen"
! <var>sigtype</var> !! <var>sigpars</var> !! signal
|-
| '''pause''' || <var>d</var>
| A zero signal (pause) with a duration of <var>d</var> seconds.
|-
| '''value''' || <var>d ; v</var>
| A constant signal (dc) with a duration of <var>d</var> seconds and an amplitude <var>v</var>.
|-
| '''noise''' || <var>d ; a</var>
| A linear distributed noise with a duration of <var>d</var> seconds and an amplitude <var>a</var>.
|-
| '''rectangle''' || <var>d ; a ; f ; q</var>
| A rectangle function with a duration of <var>d</var> seconds, an amplitude <var>a</var>, a frequency <var>f</var> Hz and a duty cycle <var>q</var> (0 &lt; <var>q</var> &lt; 1).
|-
| '''rectangle''' || <var>d ; a ; f ; q</var>
| A triangle function with a duration of <var>d</var> seconds, an amplitude <var>a</var>, a frequency <var>f</var> Hz and a duty cycle <var>q</var> (0 &lt; <var>q</var> &lt; 1).
|-
| '''tone''' || <var>d ; a ; f ; p ; n ; da ; df</var> || A sine wave or a harmonic tone with a duration of <var>d</var> seconds, an amplitude <var>a</var>, a frequency <var>f</var> Hz and a phase <var>p</var> rad. If the number of harmonics <var>n</var> is greater than one, the values <var>da</var> (def.=1) and <var>df</var> (def.=<var>f</var>) are used to compute the amplitude and frequency of component ''i'' (= 1..<var>n</var>):<br/>ai = <var>a</var> . <var>da</var><sup>''i-1''</sup>; fi = <var>df</var> +</code> <var>f</var> . (''i''-1)
|-
| '''segment''' || <var>aset ; aseg ; ach ; a</var>
| Adds the signal of segment <var>aseg</var> and channel <var>ach</var> of audio set <var>aset</var> to the sequence. If <var>ach</var> is set to <code>0</code> or <code>*</code> all channels are added to the sequence (1 -> <var>ch</var>, 2 -> <var>ch</var>+1, ...). The value <var>a</var> is used as amplification factor.
|}




;<var>value</var>


:The value to set the attribute to. See <var>attr</var> for details.


=====Result:=====


An empty string.




Line 454: Line 477:
<code>0</code> for success and a non-zero value if failed.
<code>0</code> for success and a non-zero value if failed.


===Get===
Get an attribute value. Any valid <code>Set</code> attribute name can be used as the attribute name. In addition the keyword <code>SIGNALS</code> (gets the number of defined sequence signals) and the keyword <code>TABLE</code> (get signal definition table) can be used.
=====Usage:=====
<code><var>inst</var> Get <var>attr</var></code>
=====Parameters:=====
;<var>attr</var>
:The name of the attribute.
=====Result:=====
The value of the attribute or empty string.


===Load===
===Load===
Line 535: Line 541:




===Signal===
Define a new sequence signal and add it to the sequence definition. If <var>ch</var> is set to <code>0</code> or <code>*</code> the new signal is added to each sequence channel, otherwise it is added only to the specified channel. The timebase <var>base</var> selects how the time value <var>time</var> is used. If <var>base</var> is set to <code>absolute</code>, <var>time</var> is an absolute value relative to the beginning of the signal. If base is set to <code>begin</code> or <code>end</code>, <var>time</var> is an offset to the beginning or end of the previous signal. The signal itself is defined by <var>sigtype</var> and <var>sigpars</var>.
=====Usage:=====
<code><var>inst</var> Signal <var>ch</var> ; <var>base</var> ; <var>time</var> ; <var>sigtype</var> ; <var>sigpars</var></code>
=====Parameters:=====
;<var>ch</var>
:The sequence channel (<code>0</code> or <code>*</code> for all channels).
;<var>base</var>
:The time base. The following values are supported:
:<code>absolute</code>
:<code>begin</code>
:<code>end</code>
;<var>time</var>
:time value in seconds
;<var>sigtype</var>
:The type of signal. The following values are supported:
:<code>PAUSE | VALUE | NOISE | RECTANGLE | TRIANGLE | TONE | SEGMENT</code>
;<var>sigpars</var>
:The parameters for the specified signal type.
:<code>PAUSE ;</code> <var>d</var>
:A zero signal with a duration of <var>d</var> seconds.
:<code>VALUE ;</code> <var>d</var> <code>;</code> <var>v</var>
:A constant signal (dc) with a duration of <var>d</var> seconds and an amplitude <var>v</var>.
:<code>NOISE ;</code> <var>d</var> <code>;</code> <var>a</var>
:A linear distributed noise with a duration of <var>d</var> seconds and an amplitude <var>a</var>.
:<code>RECTANGLE ;</code> <var>d</var> <code>;</code> <var>a</var> <code>;</code> <var>f</var> <code>;</code> <var>q</var>
:A rectangle function with a duration of <var>d</var> seconds, an amplitude <var>a</var>, a frequency <var>f</var> Hz and a duty cycle <var>q</var> (0 < <var>q</var> < 1).
:<code>TRIANGLE ;</code> <var>d</var> <code>;</code> <var>a</var> <code>;</code> <var>f</var> <code>;</code> <var>q</var>
:A triangle function with a duration of <var>d</var> seconds, an amplitude <var>a</var>, a frequency <var>f</var> Hz and a duty cycle <var>q</var> (0 < <var>q</var> < 1).
:<code>TONE ;</code> <var>d</var> <code>;</code> <var>a</var> <code>;</code> <var>f</var> <code>;</code> <var>p</var> <code>;</code> <var>n</var> <code>;</code> <var>da</var> <code>;</code> <var>df</var>
:A sine wave or a harmonic tone with a duration of <var>d</var> seconds, an amplitude <var>a</var>, a frequency <var>f</var> Hz and a phase <var>p</var> rad. If the number of harmonics <var>n</var> is greater than one, the values <var>da</var> (def.=1) and <var>df</var> (def.=f) are used to compute the amplitude and frequency of component <code>i</code> (<code>i</code> = 1..<var>n</var>) as follows:
:<code>ai =</code> <var>a</var> <code>.</code> <var>da</var><code>i-1</code>
:<code>fi =</code> <var>df</var> <code>+</code> <var>f</var><code>.(i-1)</code>
:<code>SEGMENT ;</code> <var>aset</var> <code>;</code> <var>aseg</var> <code>;</code> <var>ach</var> <code>;</code> <var>a</var>
:Adds the signal of segment <var>aseg</var> and channel <var>ach</var> of audio set <var>aset</var> to the sequence. If <var>ach</var> is set to <code>0</code> or <code>*</code> all channels are added to the sequence (1 -> ch, 2 -> ch+1, ...). The value <var>a</var> is used as amplification factor.
=====Result:=====
0 for success and a non-zero value if failed


===Test===
===Test===

Revision as of 10:00, 6 March 2018

File: BSeq.STX, linked to library STX.LIB
See also: Sequences

The class BSeq can create and play sequences and provides an interface to the Programmer_Guide/Shell_Items/Wave/SET_WAVE#sequence wave-item functions.

Create a new Instance of BSeq

Usage
bseq [ attr=value ; ... ]
cobj new bseq [ attr=value ; ... ]
attr=value, ...
initial values for sequence attributes (see member function Set)
Result
An instance of BSeq or an empty string
Description
Create and initialize a sequence instance and assign sequence attributes. To assign the attribute(s) the member function Set is called.

Set Attributes

Usage
bseq_instance set attr=val [ ; ... ]
attr=val
Assign the value val to the attribute attr (see description below)
Result
always 0
Description
Assign sequence attributes. An attribute value is assigned only if it is valid. For some attributes automatic corrections are applied. Multiple attribute assignments separated by semi-colons can be specified. Attribute names and value keywords can be abbreviated.
Supported attributes
  • channel=n → set the number of channels (1 ≤ n ≤ 256)
  • envelope=type,ton,toff → set global envelope type (none, linear or cosine) and the on/off time (in ms, ≥ 0).
  • factor=a → set global amplitude factor (a ≠ 0)
  • srate=frq → set sampling rate in Hz (> 0).

Get Attribute

Usage
bseq_instance get attr
attr
name of the attribute to retrieve (see description below)
Result
value of specified attribute or an empty string
Description
Retrieve a sequence attribute.
Supported attributes
  • channel → get number of channels
  • envelope → get global envelope definition (type,ton,toff)
  • factor → get global amplitude factor
  • srate → get sampling rate
  • signals → get number of signal definitions
  • table → get signal definition table

Signal

Usage
bseq_instance signal ch ; base ; time ; sigtype ; sigpars
ch
sequence channel number (1, 2, ..), 0 or * for all channels
base
defines the base (origin) for the signal begin time; possible values are absolut, begin and end
time
the offset time to the selected base (origin) in seconds
sigtype ; sigpars
set the type and parameters of the new signal (see below)
Result
0 for success and a non-zero value if failed
Description
Define a new sequence signal and add it to the sequence definition. If ch is set to 0 or * the new signal is added to each sequence channel, otherwise it is added only to the specified channel. The timebase base selects how the time value time is used. If it is set to absolute, time is an absolute time (0 = begin of the signal). If base is set to begin or end, time is an offset to the beginning or end of the previous signal. The signal itself is defined by sigtype and sigpars.
sigtype sigpars signal
pause d A zero signal (pause) with a duration of d seconds.
value d ; v A constant signal (dc) with a duration of d seconds and an amplitude v.
noise d ; a A linear distributed noise with a duration of d seconds and an amplitude a.
rectangle d ; a ; f ; q A rectangle function with a duration of d seconds, an amplitude a, a frequency f Hz and a duty cycle q (0 < q < 1).
rectangle d ; a ; f ; q A triangle function with a duration of d seconds, an amplitude a, a frequency f Hz and a duty cycle q (0 < q < 1).
tone d ; a ; f ; p ; n ; da ; df A sine wave or a harmonic tone with a duration of d seconds, an amplitude a, a frequency f Hz and a phase p rad. If the number of harmonics n is greater than one, the values da (def.=1) and df (def.=f) are used to compute the amplitude and frequency of component i (= 1..n):
ai = a . dai-1; fi = df + f . (i-1)
segment aset ; aseg ; ach ; a Adds the signal of segment aseg and channel ach of audio set aset to the sequence. If ach is set to 0 or * all channels are added to the sequence (1 -> ch, 2 -> ch+1, ...). The value a is used as amplification factor.






BSeq Member Functions

The BSeq class has the following member functions. See CObj Member Functions for a list of functions implemented in the parent class.

AMod

Define/undefine an amplitude modulation function for the signal pos. The amplitude modulation is computed as follows:

y(t) = ((1 – fac/2) + m(t).fac/2) . x(t)

with:

y(t) modulated sequence signal
x(t) sequence signal
m(t) modulation function, depends on type, rate and dc or phase
fac modulation factor (0 <= fac <= 1)
Usage:

inst AMod pos ; type ; rate ; factor ; dc|phase

Parameters:
pos
The signal index (* for the last defined signal).
type
The type of modulation function. The following types are supported:
none|rectangular|triangle|sine
rate
The modulation rate in Hz.
factor
The modulation factor.
dc
The duty cycle (if the type is rectangular or triangle).
phase
The starting phase (if the type is sine).
Result:

0 for success and a non-zero value if failed

Parameters:
attr
The name of the sequence attribute.
value
The value of the sequence attribute.
Result:

0 for success and 1 for error

Note that you can create an instance of the BSeq class with the following syntax:

#inst := BSeq

Copy

Copies the sequence signal to the specified target. To perform the copy function the class BSIGNAL is used.

Usage:

inst Copy mode ; sch ; tset ; tseg ; tch ; tmode ; tname

Parameters:
mode
This parameter is currently not used, but is reserved for future expansions.
sch
The sequence channel list (see CreateWave).
tset
The target audio set.
tseg
The target segment reference, id or expression.
tch
The target channel.
tmode
The target mode. The following values are supported:
insert|replace|mix
tname
The name for the copied signal.
Result:

0 for success and a non-zero value if failed.

CreateSegments

Creates audio segments (ASeg elements) for all defined signals and stores them in the specified DataSet element. Also a segment named Signal.All for the whole sequence signal is created. The segment names are chosen automatically.

Usage:

inst CreateSegments ref

Parameters:
ref
The reference to a DataSet element of type ASequence.
Result:

The number of created segments.

CreateWave

Creates the sequence wave item and prepares it for use. If create succeeds the wave item is returned and can be used for signal processing. Otherwise the error information is written to the log. window and an empty string is returned. If a channel list argument is specified only the requested channels are created. If the definition contains signals of type segment, the sound files are opened automatically before the wave item is created. The returned wave item must be deleted with the member function DeleteWave and not with the command 'delete waveitem'!

Usage:

inst CreateWave [ sch ]

Parameters:
sch
The sequence channel list (blank separated list of channel indices).
Result:

The name of the created sequence wave item or empty string if creation fails.

Delete

Delete the signal pos from the sequence.

Usage:

inst Delete pos

Parameters:
pos
The signal index (* for the last defined signal).
Result:

Always 0.

DeleteWave

Deletes the sequence wave item created with CreateWave and closes all sound files opened during creation.

Result:

Always 0.

Destruct

Remove sequence and sequence wave item (if created).

Result:

none

DialogEdit

Displays a dialog to add, modify and delete signal definitions.

Result:

none

DialogSetup

Displays a dialog to change the sequence attributes.

Result:

0 for OK and 1 for cancel.

Envelope

Define/undefine an envelope function for the signal pos. An envelope is defined by the function type and a set of points, each one defined with a time and an amplitude value (tX and aX). The time values can be specified as an offset from the beginning (tX > 0) or from the end (tX < 0). If an amplitude is defined, the signal starts and ends with a zero amplitude.

Usage:

inst Envelope pos ; type ; t1 a1 t2 a2 ...

Parameters:
pos
The signal index (* for last defined signal).
type
The type of envelope. The following values are supported:
none|linear|cosine
tX,aX
The envelope points (tX = time in seconds, aX = linear amplitude).
result:

0 for success and a non-zero value if failed

Filter

Define or undefine a filter for the signal pos. The filter applied to the signal is an IIR filter of the specified type. The arguments f1, f2 and inv define the frequency band and filter type:

f1 = 0, f2 > 0, inv = 0 // lowpass f1 > 0, f2 = samplingrate / 2, inv = 0 // highpass 0 < f1 < f2 < samplingrate / 2, inv = 0 // bandpass 0 < f1 < f2 < samplingrate / 2, inv = 1 // bandstop

The parameters order, damp and ripple are used for the coefficient computation.

Usage:

inst Filter pos ; type ; f1 ; f2 ; inv ; order ; damp ; ripple

Parameters:
pos
The signal index (* for the last defined signal).
type
The type of filter. The following values are supported:
none|elliptic|butterworth|chebychev
f1,f2
The frequency band.
inv
Invert - yes|no, 0|1
order
The filter order must be an integer greater than 1. The default is 5.
damp
The stopband damping in dB must be a number less than -9 or greater than 9. The default is 60.
ripple
The passband ripple in dB must be a number between -10 and 10. The default is 1.
Result:

0 for success and a non-zero value if failed.

FMod

Define or undefine a frequency modulation function for the signal pos. A frequency modulation can only be defined for signal generators which generates a periodic signal (e.g. rectangle, triangle or tone). The frequency modulation is computed as follows:

f(t) = f + bw/2.m(t)

with:

fc the fundamental frequency of the generator (carrier)
f(t) modulated frequency
m(t) modulation function, depends on type, rate and dc or phase
bw modulation bandwidth in Hz (>0)
Usage:

inst FMod pos ; type ; rate ; bw ; dc ; phase

Parameters:
pos
The signal index (* for the last defined signal).
type
The type of modulation function. The following values are supported:
none|rectangle|triangle|sine
rate
The modulation rate in Hz.
bw
The modulation bandwidth in Hz.
dc
The duty cycle (if type is rectangle or triangle).
phase
The starting phase (if type is sine).
Result:

0 for success and a non-zero value if failed

Define or undefine an frequency sweep function for the signal pos. A sweep can only defined for signal generators which generates a periodic signal (e.g. rectangle, triangle or tone). The sweep is computed as follows:

for exp = 0: f(t) = f + width . t / d for exp > 0: f(t) = f + width . (t / d)exp

with:

fc fundamental frequency of the generator (carrier)
f(t) modulated frequency
width sweep width in Hz (any number)
exp sweep function exponent
d signal duration
Usage:

inst FMod pos ; SWEEP ; width ; exp

Parameters:
pos
See above
width
The sweep width in Hz (positive and negative values are allowed).
exp
The sweep function exponent (>= 0).
Result:

0 for success and a non-zero value if failed.


Load

Loads the signal definitions from child element ASequenceSignals of the specified DataSet element. The signal definition table is cleared before the element is loaded.

Usage:

inst Load ref

Parameters:
ref
The reference to a DataSet element containing a sequence signal definition element with tag ASequenceSignals.
Result:

0 for success and a non-zero value if failed.

Play

Plays the sequence. The class PlayWave is used for playback.

Usage:

inst Play dialog [; sch ]

Parameters:
dialog
Display a play dialog or play without a dialog. The following values are supported:
0 - do not display a dialog
1 - display a dialog
sch
The sequence channel list (see CreateWave).
Result:

0 for success and a non-zero value if failed

Save

Stores the signal definitions in the child element ASequenceSignals of the specified DataSet element.

Usage:

inst Save ref

Parameters:
ref
The reference to a DataSet element.
Result:

0 for success and a non-zero value if failed.


Test

Test the sequence definition. Returns 0 if the definitions are valid and the sequence wave item can be created. For a description of the channel index arguments see function CreateWave.

Usage:

inst Test [ sch ]

Parameters:
sch
The sequence channel list (see CreateWave).
Result:

0 for success and a non-zero value if failed.

Examples

//{{3.8 3219}{2007.08.14  15.05.47} - automatically created version information - do not change or delete this line}
///////////////////////////////////////////////////////////////////////////////
//
//  Macro:          bseq_examples
//  Description:    Playback a sine tone at 1000Hz using the BSeq class.
//  Parameters:     none
//  Return:         0 on success
//  Author:         Jonnie White
//  History:        2007-08-14  created
//  File:           bseq_examples.sts
//
///////////////////////////////////////////////////////////////////////////////
[Macro bseq_example]

// create a BSeq instance
#bseq := BSeq
if '$#bseq[?]' != 'instance' em -1 ; $#mac::Error - failed to create a BSeq instance
// define a 100s sine tone at 1000Hz
$#bseq Signal * ; begin ; 0 ; tone ; 100 ; 1 ; 1000 ; 0
if '$result' != 0 em $result ; $#mac::Error - sequence Signal call failed
// play the tone using the playback dialog
$#bseq play 1
if '$result' != 0 em $result ; $#mac::Error - playback failed
// destroy the instance
$#bseq destroy

exit 1 int 0

Navigation menu

Personal tools