Programmer Guide/Macro Library/BSF: Difference between revisions

From STX Wiki
Jump to navigationJump to search
m (1 revision: Initial import)
 
(51 intermediate revisions by 4 users not shown)
Line 1: Line 1:
{{DISPLAYTITLE:{{SUBPAGENAME}} - Soundfile Handling}}
:'''File''': BSF.STX, linked to library STX.LIB
:'''See also''': [[Programmer_Guide/Command_Reference/LOAD#LOAD_SOUNDFILE|LOAD SOUNDFILE]], [[Programmer_Guide/Command_Reference/UNLOAD#UNLOAD_SOUNDFILE|UNLOAD SOUNDFILE]],[[Programmer_Guide/Command_Reference/SFTRUNCATE|SFTRUNCATE]],  [[Programmer_Guide/Command_Reference/SEGMENT|SEGMENT]], [[Programmer_Guide/Shell_Items/Wave|WAVE items]]
==Variables and items used by this library==
:{|class="einrahmen"
!name !!type !!description
|-
|<code>@ParSoundFileNew</code> ||variable ||default values for function [[#NewDialog|NewDialog]]
|-
|<code>@ListAudioSampleCode</code> ||variable ||list of defined sample code keywords
|-
|<code>@MaxAudioChannels</code> ||variable ||maximum number of soundfile channels
|-
|<code>@MaxAudioSRate</code> ||variable ||maximum sampling rate in Hz
|-
|<code>@ListAudioSRate</code> ||variable ||list of standard sampling rate values
|-
|<code>@TempDir</code> ||variable ||directory for temporary files
|-
|<code>BSFOpenList</code> ||table item ||list of open soundfiles (only for [[Programmer_Guide/BScript|BScript]] applications); this table is used by the functions [[#Open|Open]], [[#Close|Close]] and [[#CloseAll|CloseAll]]
|-
|<code>SoundFileList</code> ||variable ||name of the shared soundfile table item (see [[../BSTXIni|BSTXIni]]); the following information is stored in the table fields:<BR>
<code>File</code> = full path<BR>
<code>SRate</code> = sampling rate in Hz<BR>
<code>Channels</code> = number of channels<BR>
<code>Length</code> = signal length in seconds<BR>
<code>Used</code> = open-counter<BR>
<code>Code</code> = sampling code<BR>
<code>Mode</code> = accesss mode<BR>
Because the table is shared between shells, the LOCK/UNLOCK methods should be used to access the table. Do not change table content! 
|-
|<code>AutoCloseSF</code> ||variable ||if set to <code>1</code>all soundfiles opened by a [[Programmer_Guide/BScript|BScript]] application are closed when the script exits.
|-
|<code>CSF</code> ||variable ||full path of the selected (active) soundfile.
|-
|<code>CSFH</code> ||variable ||header parameters of the selected (active) soundfile.<BR>
<code>CSFH=srate nchannels nsamples code type accessmode</code>
|}


{{DISPLAYTITLE:{{SUBPAGENAME}}}}
==NewDialog==

BSF NEWDIALOG [ <var>srate</var> [ ; <var>nch</var> ; <var>code</var> ]
==BSF==
:Dialog to create a new soundfile.
{|class="einrahmen"
!argument !!description !!default
|-
|<var>srate</var>
|Sampling rate in Hz
|last dialog value
|-
|<var>nch</var>
|Number of channels.
|last dialog value
|-
|<var>code</var>
|Signal sample size (number of bits) and code.
|last dialog value
|-
!RESULT !!description
|-
|<var>path</var> ||The full pathname of the created soundfile.
|-
|''empty string'' ||If the dialog was canceled or the creation fails.
|}
;Notes:
*This function uses a two step dialog. In the first the soundfile name is selected. The second is used to select the soundfile parameters.
*The new soundfile is always created using the '''Windows WAVE''' format.
*On return, the soundfile is created but not opened.


This macro implements a function package for dealing with sound files.
==OpenDialog==
 
BSF OPENDIALOG

:Dialog to open an existing soundfile.
==CLOSE==
{|class="einrahmen"
 
|-
Decrements the open-counter of the sound file and closes it if the counter is <code>0</code>. The argument <var>path</var>|<var>index</var> is used to select the sound file. If the argument delete is set to <code>1</code> the sound file is deleted after closing.
!RESULT !!description
 
|-
A sound file can only be closed if it is not in use (e.g. by a wave- or a spu-item). If a wave file addresses this sound file then the wave file must be deleted before closing the sound file. If a sound file was opened more than once, the same number of close-calls must be used to really close the sound file.
|<var>path</var> ||The full pathname of the selected soundfile.
 
|-
=====Usage:=====
|''empty string'' ||If the dialog was canceled or the soundfile check fails.
 
|}
<code>BSF CLOSE [ <var>path</var>|<var>index</var> [ ; <var>delete</var> ]]</code>
;Notes:
 
*The dialog can open soundfiles in the '''Windows WAVE''' and the '''S_TOOLS 5''' format.
=====Parameters:=====
*On return, the soundfile is checked (existence, format) but not opened.
 
;<var>path</var>
 
:The sound file path.
 
;<var>index</var>
 
:The zero-based sound file index.
 
;<var>delete</var>
 
:A flag indicating if the sound file should be deleted. The following values are supported:
 
:<code>0</code> Do not delete the sound file (this is the default).
 
:<code>1</code> Delete the sound file.
 
=====Result:=====
 
<code>0</code> for success or non-zero errorcode
 

==CLOSEALL==
 
Close all soundfiles opened by a script application. This function is called if the variable AUTOCLOSESF is set to 1 during script execution.
 
=====Result:=====
 
0 for success or non-zero errorcode
 
 
 

==NEWDIALOG==
 
Shows a dialog to select the sound file path and the parameters and creates the file. The arguments (if present and valid) are used as default values for the sound file parameters. The created sound file is not opened!
 
=====Usage:=====
 
<code>WDIALOG [srate; channels; code]</code>
 
=====Parameters:=====
 
;<var>srate</var>
 
:The sampling rate in Hz.


;<var>channels</var>
==Open==
 
BSF OPEN <var>path</var> [ ; <var>mode</var> ; <var>srate</var> [ ; <var>nch</var> ; <var>code</var> ; <var>type</var> ; <var>offset</var> ]
:The number of channels.
:Create and/or open a sound file.
 
{|class="einrahmen"
;<var>code</var>
!argument !!description !!default
 
:The sample code (<code>PCM8</code>|<code>PCM16</code>|<code>PCM24</code>|<code>PCM32</code>|<code>FLOAT</code>).
 
=====Result:=====
 
The path of created sound file or empty string
 

==OPEN==
 
The macro <code>BSF OPEN</code> creates and/or opens a sound file. The function returns <code>0</code> if the sound file was opened/created. In this case the sound file is made to the current sound file and its name (full path) and parameters are stored in the variables <code>CSF</code> and <code>CSFH</code>. If a sound file is already opened, it is only selected and the open-counter is incremented. See Shell Environment for detailed information about <code>CSF</code> and <code>CSFH</code>.
 
{|
|-
|-
|<code>READ</code>
|<var>path</var>
|The file must exist and is opened for read only access.
|Path of the sound file to be opened or created.<BR>Note: If the asterisk '''*''' is used for <var>path</var>, an unique sound file is '''created''' in the temp. directory (<code>@TempDir</code>).
|
|-
|-
|<var>mode</var>
|<code>CREATE</code> &rarr; Create a new sound file. If the file <var>path</var> exists, it is replaced by the new sound file.<BR>
<code>READ</code> &rarr; Open an existing sound file for readonly-access.<BR>
<code>WRITE</code> &rarr; Open a sound file for read- and write-access. The file is created if it do not exist!<BR>
<code>AUTO</code> &rarr; Open a sound file and select the access type automatically (try read/write first and than readonly).
|<code>WRITE</code>
|<code>WRITE</code>
|The file is opened for read/write access and created if it does not exist.
|-
|-
|<code>AUTO</code>
|<var>srate</var>
|The mode <code>READ</code> or <code>WRITE</code> is chosen automatically depending on the file's status.
|Sampling rate in Hz.
|<code>44100</code>
|-
|-
|<code>CREATE</code>
|<var>nch</var>
|A new file is created even if the specified file exists. If an automatic file name is requested (path equals '<code>*</code>'), the mode <code>CREATE</code> is always assumed. The arguments <var>srate</var>, <var>channels</var>, <var>code</var>, <var>type</var> and <var>offset</var> are only used if a new file is created.
|Number of channels.
|<code>1</code>
|-
|<var>code</var>
|Signal sample size (number of bits) and code.<BR>
<code>PCM8, PCM16, PCM24, PCM32</code> &rarr; 8/16/24/32 bit integer twos-complement<BR>
<code>FLOAT</code> &rarr; 32 bit IEEE floating point
|<code>PCM16</code>
|-
|<var>type</var>
|File format.<BR>
<code>WAVE</code> &rarr; Windows WAVE format (Microsoft)<BR>
<code>ST5</code> &rarr; S_TOOLS 5 format (ISF)<BR>
<code>BINARY</code> &rarr; plain binary file; may contain a unknown <var>offset</var>-byte header
|<code>WAVE</code>
|-
|<var>offset</var>
|The size of the header of a plain binary sound file.
|<code>0</code>.
|-
!RESULT !!description
|-
|<code>0</code> ||The sound file was successfully selected, opened or created.
|-
|&ne; <code>0</code> ||It is not the case that the sound file was successfully selected, opened or created.<BR>
<code>-2</code> &rarr; can not assign unique tempfile name<BR>
<code>-3</code> &rarr; sampling rate out of range<BR>
<code>-4</code> &rarr; number of channels out of range<BR>
<code>-5</code> &rarr; header size of binary-files must be greater/equal 0<BR>
otherwise &rarr; a {{STX}} error code
|}
|}
;Notes:
*If a sound file is already opened, it is selected and its open-counter is incremented. The variables <code>CSF</code> and <code>CSFH</code> contain the full path and the header parameters of the sound file.
*If a sound file is opened the first time, the open-counter is set to 1.
*This function updates the shared sound file table (<code>SoundFileList</code>) and the table <code>BSFOpenList</code> (if called in a [[Programmer_Guide/BScript|BScript]] application)
*The arguments <var>srate</var>, <var>nch</var>, <var>code</var> and <var>type</var> are only used if a new sound file is created (<var>mode</var> equals <code>CREATE</code>, <code>WRITE</code> or <code>AUTO</code>) or if the <var>type</var> <code>BINARY</code> is used. If an existing sound file with a known header format is opened, these parameters are read from the sound file header.
*If a sound file of type <code>BINARY</code> is opened, the sound file parameters <var>srate</var>, <var>nch</var> and <var>code</var> must always be specified, because the format and content of the (optional) header of such files is not known. The header of binary files must be located at the file begin and must consist of <var>offset</var> (&ge; 0) bytes. If a binary file is created, no header is written and <var>offset</var> must be <code>0</code>.


=====Usage:=====
==Close==
BSF CLOSE <var>path</var> [ ; <var>del</var> ]
BSF CLOSE <var>index</var> [ ; <var>del</var> ]
:Decrement open-counter of the soundfile and close it, if the open-counter equals zero.
{|class="einrahmen"
!argument !!description !!default
|-
|<var>path</var>
|Path of the soundfile to be closed.
|
|-
|<var>index</var>
|Zero based index of the soundfile (see [[#Select|Select]]).
|
|-
|<var>del</var>
|Delete flag. If this flag is set to <code>1</code> (or <code>yes</code>) and the specified soundfile is closed by this function, the soundfile and all corresponding metadata files are deleted.
|<code>0</code>
|-
!RESULT !!description
|-
|<code>0</code> ||Success.
|-
|&ne; <code>0</code> ||Failed. The return value is a {{STX}} error code.
|}
;Notes:
*First the open-counter of the specified soundfile is decremented. If the open-counter equals zero, the soundfile is closed and (optional) deleted.
*This function updates the shared soundfile table (<code>SoundFileList</code>) and the table <code>BSFOpenList</code> (if called in a [[Programmer_Guide/BScript|BScript]] application).
*To really close a soundfile the number of <code>CLOSE</code> calls must be equal to the number of <code>OPEN</code> calls for this soundfile.


<code>BSF OPEN <var>path</var> [; <var>mode</var>; <var>srate</var>; <var>channels</var>; <var>code</var>; <var>type</var>; <var>offset</var>]</code>
==CloseAll==
BSF CLOSEALL
:Close all soundfiles opened by a [[Programmer_Guide/BScript|BScript]] application. This function is automatically called at the end of the script execution if the variable <code>AutoCloseSF</code> equals <code>1</code>.
{|class="einrahmen"
|-
!RESULT !!description
|-
|<code>0</code> ||Success. All soundfiles are closed.
|-
|&ne; <code>0</code> ||Failed, some soundfiles are still open. The return value is a {{STX}} error code.
|}


=====Parameters:=====
==Select==
BSF SELECT <var>path</var>|<var>index</var>
:Select (activate) the specified soundfile.
{|class="einrahmen"
!argument !!description !!default
|-
|<var>path</var>
|Path of the soundfile to be selected.
|
|-
|<var>index</var>
|Zero based index of the soundfile to be selected.
|
|-
!RESULT !!description
|-
|<code>0</code> ||Success.
|-
|&ne; <code>0</code> ||Failed. The return value is a {{STX}} error code.
|}
;Notes:
*To select a soundfile, it must be open.
*After a successful soundfile selection, the variables <code>[[Programmer_Guide/Concepts/Current_Soundfile|CSF]]</code> and <code>[[Programmer_Guide/Concepts/Current_Soundfile|CSFH]]</code> contain the full path and the header parameters of the soundfile.
*This function do not change the open-counter of a soundfile.
*Some commands (e.g. [[Programmer_Guide/Command_Reference/NEW|NEW WAVE]] or [[Programmer_Guide/Command_Reference/SEGMENT|SEGMENT]]) are using the selected (activated) soundfile.


;<var>path</var>
==Truncate==
 
BSF TRUNCATE <var>path</var>|<var>index</var> ; <var>segexp</var> [ ; <var>afileref</var> ]
:The sound file path or <code>*</code> for automatic file name (e.g. for temporary files).
:Truncate the specified soundfile. Remove the signal outside the range specified by <var>segexp</var> from the soundfile.
 
{|class="einrahmen"
;<var>mode</var>
!argument !!description !!default
 
|-
:The access mode {<code>READ</code>|<code>WRITE</code>|<code>AUTO</code>|<code>CREATE</code>}. See above for more details.
|<var>path</var>
 
|Path of the soundfile to be truncate.
;<var>srate</var>
|
 
|-
:The sampling rate in Hz.
|<var>index</var>
 
|Zero based index of the soundfile to be truncated (see [[#Select|Select]]).
;<var>channels</var>
|
 
|-
:The number of channels.
|<var>segexp</var>
 
|A segment expression (see [[Programmer_Guide/Command_Reference/SEGMENT|SEGMENT]]) specifying the remaining signal range.
;<var>code</var>
|
 
|-
:The sample code (<code>PCM8</code>|<code>PCM16</code>|<code>PCM24</code>|<code>PCM32</code>|<code>FLOAT</code>).
|<var>afileref</var>
 
|A reference to the AFile element of the current project, containing the metadate (segments, etc.) of the specified soundfile (see [[Programmer_Guide/Macro_Library/BDataSet|BDataSet]]). If <var>afileref</var> is specified, the segment addresses are updated and segments addressing removed signal parts are deleted.
;<var>type</var>
|
 
|-
:The file format (<code>ST5</code>|<code>WAVE</code>|<code>BINARY</code>).
!RESULT !!description
 
|-
;<var>offset</var>
|<code>0</code> ||Success.
 
|-
:The size of the file header in bytes (for type=BINARY).
|&ne; <code>0</code> ||Failed. The return value is a {{STX}} error code.
 
|}
=====Result:=====
 
<code>0</code> for success or non-zero error code.
 

==OPENDIALOG==
 
Display a dialog to open an existing soundfile. If the function returns a path, the file is tested, but not opened!
 
=====Result:=====
 
The path of selected soundfile or empty string
 

==SELECT==
 
This function selects an already opened soundfile, making it the current soundfile. The soundfile path or the zero based index can be used to identify the soundfile. If the function returns 0 the soundfile is selected and its name (full path) and parameters are stored in the variables <code>CSF</code> and <code>CSFH</code>. The open-counter of the soundfile is not changed.
 
=====Usage:=====
 
<code>BSF SELECT <var>path</var>|<var>index</var></code>
 
=====Parameters:=====
 
;<var>path</var>
 
:The soundfile path.
 
;<var>index</var>
 
:The soundfile index (zero based).
 
=====Result:=====
 
0 for success or non-zero errorcode
 

==TRUNCATE path|index; segexp [; aset]==
 
The signal outside of the specified segment is removed from the soundfile. The segments stored in the audioset aset (if specified) are updated (deleted or shifted) if a valid aset is specified.
 
=====Usage:=====
 
;<var>path</var>
 
:The soundfile path.
 
;<var>index</var>
 
:The soundfile index (zero based).
 
;<var>segment</var>
 
:The segment expression for the remaining signal.
 
;<var>aset</var>
 
:The reference of audioset linked to soundfile path.
 
=====Result:=====
 
0 for success or non-zero errorcode
 
Array

Latest revision as of 11:12, 19 September 2018

File: BSF.STX, linked to library STX.LIB
See also: LOAD SOUNDFILE, UNLOAD SOUNDFILE,SFTRUNCATE, SEGMENT, WAVE items

Variables and items used by this library

name type description
@ParSoundFileNew variable default values for function NewDialog
@ListAudioSampleCode variable list of defined sample code keywords
@MaxAudioChannels variable maximum number of soundfile channels
@MaxAudioSRate variable maximum sampling rate in Hz
@ListAudioSRate variable list of standard sampling rate values
@TempDir variable directory for temporary files
BSFOpenList table item list of open soundfiles (only for BScript applications); this table is used by the functions Open, Close and CloseAll
SoundFileList variable name of the shared soundfile table item (see BSTXIni); the following information is stored in the table fields:

File = full path
SRate = sampling rate in Hz
Channels = number of channels
Length = signal length in seconds
Used = open-counter
Code = sampling code
Mode = accesss mode
Because the table is shared between shells, the LOCK/UNLOCK methods should be used to access the table. Do not change table content!

AutoCloseSF variable if set to 1all soundfiles opened by a BScript application are closed when the script exits.
CSF variable full path of the selected (active) soundfile.
CSFH variable header parameters of the selected (active) soundfile.

CSFH=srate nchannels nsamples code type accessmode

NewDialog

BSF NEWDIALOG [ srate [ ; nch ; code ]
Dialog to create a new soundfile.
argument description default
srate Sampling rate in Hz last dialog value
nch Number of channels. last dialog value
code Signal sample size (number of bits) and code. last dialog value
RESULT description
path The full pathname of the created soundfile.
empty string If the dialog was canceled or the creation fails.
Notes
  • This function uses a two step dialog. In the first the soundfile name is selected. The second is used to select the soundfile parameters.
  • The new soundfile is always created using the Windows WAVE format.
  • On return, the soundfile is created but not opened.

OpenDialog

BSF OPENDIALOG
Dialog to open an existing soundfile.
RESULT description
path The full pathname of the selected soundfile.
empty string If the dialog was canceled or the soundfile check fails.
Notes
  • The dialog can open soundfiles in the Windows WAVE and the S_TOOLS 5 format.
  • On return, the soundfile is checked (existence, format) but not opened.

Open

BSF OPEN path [ ; mode ; srate [ ; nch ; code ; type ; offset ]
Create and/or open a sound file.
argument description default
path Path of the sound file to be opened or created.
Note: If the asterisk * is used for path, an unique sound file is created in the temp. directory (@TempDir).
mode CREATE → Create a new sound file. If the file path exists, it is replaced by the new sound file.

READ → Open an existing sound file for readonly-access.
WRITE → Open a sound file for read- and write-access. The file is created if it do not exist!
AUTO → Open a sound file and select the access type automatically (try read/write first and than readonly).

WRITE
srate Sampling rate in Hz. 44100
nch Number of channels. 1
code Signal sample size (number of bits) and code.

PCM8, PCM16, PCM24, PCM32 → 8/16/24/32 bit integer twos-complement
FLOAT → 32 bit IEEE floating point

PCM16
type File format.

WAVE → Windows WAVE format (Microsoft)
ST5 → S_TOOLS 5 format (ISF)
BINARY → plain binary file; may contain a unknown offset-byte header

WAVE
offset The size of the header of a plain binary sound file. 0.
RESULT description
0 The sound file was successfully selected, opened or created.
0 It is not the case that the sound file was successfully selected, opened or created.

-2 → can not assign unique tempfile name
-3 → sampling rate out of range
-4 → number of channels out of range
-5 → header size of binary-files must be greater/equal 0
otherwise → a STx error code

Notes
  • If a sound file is already opened, it is selected and its open-counter is incremented. The variables CSF and CSFH contain the full path and the header parameters of the sound file.
  • If a sound file is opened the first time, the open-counter is set to 1.
  • This function updates the shared sound file table (SoundFileList) and the table BSFOpenList (if called in a BScript application)
  • The arguments srate, nch, code and type are only used if a new sound file is created (mode equals CREATE, WRITE or AUTO) or if the type BINARY is used. If an existing sound file with a known header format is opened, these parameters are read from the sound file header.
  • If a sound file of type BINARY is opened, the sound file parameters srate, nch and code must always be specified, because the format and content of the (optional) header of such files is not known. The header of binary files must be located at the file begin and must consist of offset (≥ 0) bytes. If a binary file is created, no header is written and offset must be 0.

Close

BSF CLOSE path [ ; del ]
BSF CLOSE index [ ; del ]
Decrement open-counter of the soundfile and close it, if the open-counter equals zero.
argument description default
path Path of the soundfile to be closed.
index Zero based index of the soundfile (see Select).
del Delete flag. If this flag is set to 1 (or yes) and the specified soundfile is closed by this function, the soundfile and all corresponding metadata files are deleted. 0
RESULT description
0 Success.
0 Failed. The return value is a STx error code.
Notes
  • First the open-counter of the specified soundfile is decremented. If the open-counter equals zero, the soundfile is closed and (optional) deleted.
  • This function updates the shared soundfile table (SoundFileList) and the table BSFOpenList (if called in a BScript application).
  • To really close a soundfile the number of CLOSE calls must be equal to the number of OPEN calls for this soundfile.

CloseAll

BSF CLOSEALL
Close all soundfiles opened by a BScript application. This function is automatically called at the end of the script execution if the variable AutoCloseSF equals 1.
RESULT description
0 Success. All soundfiles are closed.
0 Failed, some soundfiles are still open. The return value is a STx error code.

Select

BSF SELECT path|index
Select (activate) the specified soundfile.
argument description default
path Path of the soundfile to be selected.
index Zero based index of the soundfile to be selected.
RESULT description
0 Success.
0 Failed. The return value is a STx error code.
Notes
  • To select a soundfile, it must be open.
  • After a successful soundfile selection, the variables CSF and CSFH contain the full path and the header parameters of the soundfile.
  • This function do not change the open-counter of a soundfile.
  • Some commands (e.g. NEW WAVE or SEGMENT) are using the selected (activated) soundfile.

Truncate

BSF TRUNCATE path|index ; segexp [ ; afileref ]
Truncate the specified soundfile. Remove the signal outside the range specified by segexp from the soundfile.
argument description default
path Path of the soundfile to be truncate.
index Zero based index of the soundfile to be truncated (see Select).
segexp A segment expression (see SEGMENT) specifying the remaining signal range.
afileref A reference to the AFile element of the current project, containing the metadate (segments, etc.) of the specified soundfile (see BDataSet). If afileref is specified, the segment addresses are updated and segments addressing removed signal parts are deleted.
RESULT description
0 Success.
0 Failed. The return value is a STx error code.

Navigation menu

Personal tools