Programmer Guide/Programming a Toolbox/Example Toolbox function: Difference between revisions

From STX Wiki
Jump to navigationJump to search
No edit summary
No edit summary
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
{{DISPLAYTITLE:{{SUBPAGENAME}}}}
{{DISPLAYTITLE:{{SUBPAGENAME}}}}
{{Toolbox}}
{{PGToolbox}}
{{Examples}}
{{Examples}}
The function <code>ExportSegmentsToSoundfiles</code> exports the selected segments from the Workspace Detail to individual soundfiles. It is only available in the DataSet context.
The function <code>ExportSegmentsToSoundfiles</code> exports the selected segments from the Workspace Detail to individual soundfiles. It is only available in the DataSet context.


Line 9: Line 8:


/*
/*
This is a Toolbox definition and implementation file. Load this file into the
This is a Toolbox definition and implementation file. Place this file in the
Toolbox folder, and you can then run this function in
STx via the Special->Toolbox Function menu.
STx via the Special->Toolbox Function menu.
*/
*/
Line 18: Line 18:
                 by the Toolbox manager.
                 by the Toolbox manager.
Description:    The user can select segments in the Workspace Detail and  
Description:    The user can select segments in the Workspace Detail and  
                 export them to individual soundfiles (one per segment).
                 export them to individual sound files (one per segment).
                 By default, soundfiles are exported to the same directory  
                 By default, sound files are exported to the same directory  
                 as the segment's soundfile is in and sequences are exported
                 as the segment's sound file is in and sequences are exported
                 to the working directory.
                 to the working directory. See the ExportSegmentsToSoundfiles
                macro for the implementation details. See the comments below
                for details about the possible settings.
Author:        Jonnie White
Author:        Jonnie White
History:        2005-06-03    jw    Created
History:        2005-06-03    jw    Created
                 2005-09-21   jw    Version: 1.0
                 2005-06-17   jw    Added dialog telling user that files were
                                    successfully added.
                2005-07-25    jw    bugfix: bdataset::getsegment was no longer working
--*/
--*/
[Function ExportSegmentsToSoundfiles]
[Function ExportSegmentsToSoundfiles]
Line 30: Line 34:
*        ; ExportSegmentsToSoundfiles ; macro ; dataset ; *
*        ; ExportSegmentsToSoundfiles ; macro ; dataset ; *
// Description
// Description
Export the selected segments to individual soundfiles
Export the selected segments to individual sound files
// Setup Variables
// Setup Variables
//  
//  
// Variable: AddToDataset
AddToDataset = 1 // Values:  0|1
// Values:  0|1
// 0 - Do not add files to the dataset
//           0 - Do not add files to the dataset
//     1 - Ask the user if they want to add files to the dataset
//           1 - Ask the user if they want to add files to the dataset)
PathNameFlag = 0 // Values:  0|1
AddToDataset = 1
//     0 - The path should be the same as the source file
// Variable: PathFlags
//     1 - The path should be chosen by the user
// Values:  0|1|2|3
PathName = // Values:  empty or a valid directory path
//           0 - The path should be the same as the source file. The
FileNameFlag = 0 // Values: 0|1|2|3
//              value of Path is ignored.
// 0 - <segmentid>.wav
//           1 - The value of Path should be used, or if Path is empty,
// 1 - <soundfilename>.wav
//              the path of the sourcefile should be used.
// 2 - <soundfilename>.<segmentid>.wav
//          2 - The path should be chosen by the user and used for
// 3 - <audioref>.wav (1st "/" removed, all other "/" replaced ".")
//              all segements
//          3 - The user should choose a path for each segment.
PathFlags = 0
// Variable: Path
// Values:  empty or a valid directory path
Path =
// Variable: FileNameFormat
// Values:  String constants and the keywords <soundfile> and <segment> which
//           are replaced with the soundfile ID and the segment ID
//           respectively.
FileNameFormat = <soundfile>.<segment>  


/*++-------------------------------------------------------------------------//
/*++
Macro:          ExportSegmentsToSoundfiles
Macro:          ExportSegmentsToSoundfiles
Usage:          This is a Toolbox function implementation macro for the DataSet
Usage:          This is a Toolbox function implementation macro for the DataSet
                 context
                 context
Description:    This Toolbox function is called in the Dataset context and
Description:    This Toolbox function is called in the Dataset context and
                 exports any selected segments to soundfiles. The user can
                 exports any selected segments (in the table #etab) to  
                choose the file name format for the soundfiles which will be
                sound files. The user can choose the file name format for the  
                 created.
                sound files which will be created and whether they should
                 be stored in the same directory as the original sound file.
Author:        Jonnie White
Author:        Jonnie White
History:        2005-06-03   jw   Created
History:        2005-06-03 jw Created
2017-04-05 an Dialog added, some functional changes
--*/
--*/
[Macro ExportSegmentsToSoundfiles]
[Macro ExportSegmentsToSoundfiles]
Line 75: Line 70:
$@TBLOADSETUP  
$@TBLOADSETUP  


// if there are selected segments in the DataSet context table #etab
// at least one segment must be selected in the DataSet context table #etab
if '$#etab[?]' == 'table' && '$#etab[]' > 0 && '$#etag' == 'aseg' then
if '$#etab[?]' != 'table' || '$#etab[]' < 1 || '$#etag' != 'aseg' then
     $BDataset Attach // attach the dataset for the duration of this function
     BUtil 'MsgBox Msg; Please select at least one segment and try again.; ToolBox "$#function" Error!'
    // save the position so we can restore it at the end of the function
goto _tbexit
    #origPos := $($BDataset SavePos)
end
    #tFiles := new table * // Add new file paths to this table
    while '$#etab[]' > 0 // if the user has selected any segments
        $BDataset SelectASeg * ; $#etab[0] // select the segment
        if '$result' == 0 then
            // get information about the audio set the segment is in
            readstr '$($BDataset GetASetInfo)' #';'#ASetId';'#';'#';'#ASetFilePath /Delete
            // get information about the id of the segment
            readstr '$($BDataset GetSegment $#ASetIRef ; * ; ID)' #ASegId';'# /Delete
            // select the output path depending on the user's settings
            gosubx _SelectOutputPath
            // format the output path depending on the user's settings
            #file := $#OutputPath'\'$(gosubx _FormatFileName $#ASetId;$#ASegId).wav
            // create a wave item based on this segment and save it to file
            #wsrc := $($BDataset CreateWave '$#ASetIRef ; $#ASegId ; *')
            if '$#wsrc[?]' == wave then
                if '$(bsf 'open $#file;create;~$#wsrc[!srate];~$#wsrc[!channels]')' == 0 then
                    #wdst := $(new wave * 0_~$#wsrc[!length])
                    if $rc == 0 then
                        if $($#wsrc copy $#wdst) == 0 $#tFiles * $#file
                        delete $#wdst
                    end
                    bsf 'close $#file'
                else
                    BUtil 'Msgbox Msg; Failed to create the file $#file ; ToolBox $#mac Error!'
                end
                $BDataset deletewave $#wsrc
            else
                BUtil 'Msgbox Msg; Create waveitem failed! ; ToolBox $#mac Error!'
            end
        end
        $#etab 0 /Delete
    end
    // Give the user the option to add the new soundfiles to the project.
    if '$#AddToDataset' > 0 then
        readstr '$($BDataset DialogSelectElement Select target set for new soundfiles; single; iref; Set; 1)' #targetSetRef
        if '$#read' > 0 then
            while $#tFiles[] > 0
                $BDataset AddASet '$#tFiles[0];$#targetSetRef;*'
                $#tFiles 0 /Delete
            end
            DatasetCmd RefreshAll
        end
    end


    $BDataset SelectIRef $#origPos
// check variables exist and have permissible values
    $BDataset SavePos $#origPos
if $(keyword '#$#AddToDataSet' #0 #1) < 0 #AddToDataSet := 1
    $BDataset Detach
if $(keyword '#$#PathNameFlag' #0 #1) < 0 #PathNameFlag := 1
else
if $(keyword '#$#FileNameFlag' #0 #1 #2) < 0 #FileNameFlag := 1
    BUtil 'MsgBox Msg; Please select at least one segment and try again.; ToolBox $#mac Error!'
 
#listPathNameFlag := set ' soundfile path; specified path'
#listFileNameFlag := set ' segment id; soundfile name + segment id; xml audio reference'
 
#txt := cond $#etab[] == 1 ? set '(1 segment)' : set '($#etab[] segments)'
#dlg := cdlgmap * dialog * ToolBox "$#function"
$($#dlg setci) combobox 0.0 0.0 'target directory' #PathNameFlag 14 1 #listPathNameFlag /a /d=;
$($#dlg setci) combobox 0.0 16.0 'file name format' #FileNameFlag 22 1 #listFileNameFlag /a /d=;
$($#dlg setci path) edit 2.0 0.0 '' #PathName 33 1
$($#dlg setci browse) button 2.0 35.0 '...' * 3 1
$($#dlg setci) checkbox 3.0 0.0 'add created to soundfiles' #AddToDataSet 30 1 /r
$($#dlg setci ok) button 4.5 0.0 'Export  $#txt' * 28 1
$($#dlg setci cancel) button 4.5 30.0 'Cancel' * 8 1
 
$#dlg begin okay
forever
$($#dlg setci path) $(word $#PathNameFlag v e) /n=2
$($#dlg dlg) * /w
readstr '$($#dlg loop ok cancel)' #id #par
$($#dlg dlg) * /r
if '$#id' == command then
if '$#par' == ok || '$#par' == cancel then
break
else if '$#par' == browse then
if '$(Butil DirectoryDialog Select Export Directory; $#PathName ; Yes ; No ; No)' != '' readvar result #PathName
end
end
end
end
$#dlg end


if '$#par' == cancel goto _tbexit
$BDataset Attach // attach the dataset for the duration of this function
#oldPos := $BDataset SavePos // save the position so we can restore it at the end of the function
#newFiles := new table * /g // Add new file paths to this table
for #i := 0 to $#i < '$#etab[]' step #i := int $#i+1 // if the user has selected any segments
$BDataset SelectASeg * ; $#etab[$#i] // select the segment
if '$result' == 0 then
readstr '$($BDataSet FormatIRef)' #ASegIRef
readstr '$(reverse '$#ASegIRef')' #ASegId'/'#ASetIRef
#ASetIRef := reverse '$#ASetIRef'
#ASegId := reverse '$#ASegId'
readstr '$($BDataset GetASetInfo)' #';'#ASetId';'#';'#';'#ASetFilePath /Delete
readstr '$(FileToolBox Tokenize '$#ASetFilePath')' #fdrive';'#fdir';'#fname';'#fext';'#
if '$#PathNameFlag' == 0 then
#FileName := '$#fdrive':'$#fdir'
else
readvar #PathName #FileName
end
if '$#FileNameFlag' == 0 then
#FileName := set '$#FileName' '\' '$#ASegId'
else if '$#FileNameFlag' == 1 then
#FileName := set '$#FileName' '\' '$#fname' '.' '$#ASegId'
else if '$#FileNameFlag' == 2 then
#FileName := set '$#FileName' '\' '$(translate '$(substr '$#ASegIRef' 1)' '/' '.')'
end
#FileName := '$#FileName' '.' '$(stxfiletype wave)'
#wsrc := $($BDataset CreateWave '$#ASetIRef ; $#ASegId ; *')
if '$#wsrc[?]' == wave then
if '$(bsf 'open $#FileName;create;~$#wsrc[!srate];~$#wsrc[!channels]')' == 0 then
#wdst := $(new wave * 0_~$#wsrc[!length])
if $rc == 0 then
if $($#wsrc copy $#wdst) == 0 $#newFiles * '$#FileName'
delete $#wdst
end
bsf 'close $#FileName'
else
BUtil 'Msgbox Msg; Segment "$#ASetIRef/$#ASegId" - failed to create file "$#FileName" ; ToolBox "$#function" Error!'
end
$BDataset deletewave $#wsrc           
else
BUtil 'Msgbox Msg; Segment "$#ASetIRef/$#ASegId" - create waveitem failed! ; ToolBox "$#function" Error!'
end
end
end
BUtil 'Msgbox Msg; $#newFiles[] of $#etab[] segments exported ; ToolBox "$#function"'
if '$#newFiles[]' > 0 && '$#AddToDataset' == 1 then
if $(readstr '$($BDataset DialogSelectElement Select target set for new sound files; single; iref; Set; 1)' #addSetRef) > 0 then
while $#newFiles[] > 0
$BDataset AddASet '$#newFiles[0];$#addSetRef;*'
$#newFiles 0 /Delete
end
DatasetCmd RefreshAll
end
end
$BDataset SelectIRef $#oldPos
$BDataset ResetPos $#oldPos
$BDataset ResetPos $#etab
$BDataset Detach
_tbexit:
$@TBSAVESETUP        // Save the settings (if any were changed)
$@TBSAVESETUP        // Save the settings (if any were changed)
exit 1 int $#changed // #changed must be set to 1 if any data was changed (otherwise 0)
exit 1 int $#changed // #changed must be set to 1 if any data was changed (otherwise 0)


//  The user can determine what the filename should look like, using
//  the keywords <soundfile> and <segment>. This function formats
//  the filename.
_FormatFileName:
    readvar #argv #soundfile';'#segment
    if '$#read' != 2 em -1 ; Not enough argument in $#mac
    #name := $#FileNameFormat
    readstr '$#name' #before'<soundfile>'#after /Delete
    if '$#read' > 0 && '$#before' != '$#name' #name := $#before'$#soundfile'$#after
    readstr '$#name' #before'<segment>'#after /Delete
    if '$#read' > 0 && '$#before' != '$#name'  #name := $#before'$#segment'$#after
    exit 1 set '$#name'
//  Select the output path based on the user's settings.
_SelectOutputPath:
    if '$#PathFlags' == 0 || '$#PathFlags' == 1 then
        // User settings path
        if '$#PathFlags' == 1 && '$#Path' != '' then
            #OutputPath := $(BUtil GetDirectory $#Path)
            if '$#OutputPath' != '$#Path' then
                #Path := $(Butil DirectoryDialog Please select an existing directory ; $#Path ; Yes ; No ; No)
                if '$#Path' != '' then
                    #OutputPath := $#Path
                else
                    exit 2 // User has failed to select a path, so we will export nothing.
                end
            end
        // Automatic output path
        else
            if '$#ASetFilePath' != '' then // AFile
                readstr '$(FileToolBox Tokenize $#ASetFilePath)' #drive';'#dir';'#name';'#ext /Delete
                #OutputPath := '$#drive':'$#dir'
            else // ASeq: sequences are stored in the @WORK directory by default
                #OutputPath := $@WORK'\'
            end
        end
    else if '$#PathFlags' == 2 || '$#PathFlags' == 3 then
        if '$#OutputPath' == '' || '$#PathFlags' == 3 then
            #OutputPath := $(butil 'directorydialog Export path for soundfiles; *; No; No; No')
        end
    end
    exit
</pre>
</pre>

Latest revision as of 17:32, 3 July 2019

The function ExportSegmentsToSoundfiles exports the selected segments from the Workspace Detail to individual soundfiles. It is only available in the DataSet context.

//{{3.7.0 849}{2005.05.30  15.31.20} - automatically created version information - do not change or delete this line}

/*
This is a Toolbox definition and implementation file. Place this file in the
Toolbox folder, and you can then run this function in
STx via the Special->Toolbox Function menu.
*/

/*++-------------------------------------------------------------------------//
Function:       ExportSegmentsToSoundfiles
Usage:          This is a Toolbox function definition and is called
                by the Toolbox manager.
Description:    The user can select segments in the Workspace Detail and 
                export them to individual sound files (one per segment).
                By default, sound files are exported to the same directory 
                as the segment's sound file is in and sequences are exported
                to the working directory. See the ExportSegmentsToSoundfiles
                macro for the implementation details. See the comments below
                for details about the possible settings.
Author:         Jonnie White
History:        2005-06-03    jw    Created
                2005-06-17    jw    Added dialog telling user that files were
                                    successfully added.
                2005-07-25    jw    bugfix: bdataset::getsegment was no longer working
--*/
[Function ExportSegmentsToSoundfiles]
// Hotkey ; Command                    ; Type  ; Context ; Applications
*         ; ExportSegmentsToSoundfiles ; macro ; dataset ; *
// Description
Export the selected segments to individual sound files
// Setup Variables
// 
AddToDataset = 1	// 	Values:   0|1
					// 		0 - Do not add files to the dataset
					//      1 - Ask the user if they want to add files to the dataset
PathNameFlag = 0	// 	Values:   0|1
					//      0 - The path should be the same as the source file
					//      1 - The path should be chosen by the user
PathName = 			//	Values:   empty or a valid directory path
FileNameFlag = 0	//	Values:	0|1|2|3	
					//		0 - <segmentid>.wav
					//		1 - <soundfilename>.wav
					//		2 - <soundfilename>.<segmentid>.wav
					//		3 - <audioref>.wav (1st "/" removed, all other "/" replaced ".")

/*++
Macro:          ExportSegmentsToSoundfiles
Usage:          This is a Toolbox function implementation macro for the DataSet
                context
Description:    This Toolbox function is called in the Dataset context and
                exports any selected segments (in the table #etab) to 
                sound files. The user can choose the file name format for the 
                sound files which will be created and whether they should
                be stored in the same directory as the original sound file.
Author:         Jonnie White
History:        2005-06-03	jw	Created
				2017-04-05	an	Dialog added, some functional changes
--*/
[Macro ExportSegmentsToSoundfiles]
// Parses the arguments depending on the context.
// This must be the first command
$@TBPARSE '$#argv'
// Loads the settings (if any are defined)
$@TBLOADSETUP 

// at least one segment must be selected in the DataSet context table #etab
if '$#etab[?]' != 'table' || '$#etab[]' < 1 || '$#etag' != 'aseg' then
    BUtil 'MsgBox Msg; Please select at least one segment and try again.; ToolBox "$#function" Error!'
	goto _tbexit
end

// check variables exist and have permissible values
if $(keyword '#$#AddToDataSet' #0 #1) < 0 #AddToDataSet := 1
if $(keyword '#$#PathNameFlag' #0 #1) < 0 #PathNameFlag := 1
if $(keyword '#$#FileNameFlag' #0 #1 #2) < 0 #FileNameFlag := 1

#listPathNameFlag := set ' soundfile path; specified path'
#listFileNameFlag := set ' segment id; soundfile name + segment id; xml audio reference'

#txt := cond $#etab[] == 1 ? set '(1 segment)' : set '($#etab[] segments)'
#dlg := cdlgmap * dialog * ToolBox "$#function"
$($#dlg setci) 			combobox	0.0	0.0		'target directory' #PathNameFlag 14 1 #listPathNameFlag /a /d=;
$($#dlg setci) 			combobox	0.0	16.0	'file name format' #FileNameFlag 22 1 #listFileNameFlag /a /d=;
$($#dlg setci path)		edit		2.0	0.0		'' #PathName 33 1
$($#dlg setci browse) 	button		2.0	35.0	'...' * 3 1
$($#dlg setci) 			checkbox	3.0 0.0		'add created to soundfiles' #AddToDataSet 30 1 /r
$($#dlg setci ok) 		button		4.5	 0.0	'Export  $#txt' * 28 1
$($#dlg setci cancel) 	button		4.5	30.0	'Cancel' * 8 1

$#dlg begin okay
forever
	$($#dlg setci path) $(word $#PathNameFlag v e) /n=2
	$($#dlg dlg) * /w
	readstr '$($#dlg loop ok cancel)' #id #par
	$($#dlg dlg) * /r
	if '$#id' == command then
		if '$#par' == ok || '$#par' == cancel then
			break
		else if '$#par' == browse then
			if '$(Butil DirectoryDialog Select Export Directory; $#PathName ; Yes ; No ; No)' != '' readvar result #PathName
		end
	end
end
$#dlg end

if '$#par' == cancel goto _tbexit
	
$BDataset Attach 				// attach the dataset for the duration of this function
#oldPos := $BDataset SavePos	// save the position so we can restore it at the end of the function
#newFiles := new table * /g		// Add new file paths to this table
for #i := 0 to $#i < '$#etab[]' step #i := int $#i+1 // if the user has selected any segments
	$BDataset SelectASeg * ; $#etab[$#i] // select the segment
	if '$result' == 0 then
		readstr '$($BDataSet FormatIRef)' #ASegIRef
		readstr '$(reverse '$#ASegIRef')' #ASegId'/'#ASetIRef
		#ASetIRef := reverse '$#ASetIRef'
		#ASegId := reverse '$#ASegId'
		readstr '$($BDataset GetASetInfo)' #';'#ASetId';'#';'#';'#ASetFilePath /Delete
		readstr '$(FileToolBox Tokenize '$#ASetFilePath')' #fdrive';'#fdir';'#fname';'#fext';'#
		if '$#PathNameFlag' == 0 then
			#FileName := '$#fdrive':'$#fdir'
		else
			readvar #PathName #FileName
		end
		if '$#FileNameFlag' == 0 then
			#FileName := set '$#FileName' '\' '$#ASegId'
		else if '$#FileNameFlag' == 1 then
			#FileName := set '$#FileName' '\' '$#fname' '.' '$#ASegId'
		else if '$#FileNameFlag' == 2 then
			#FileName := set '$#FileName' '\' '$(translate '$(substr '$#ASegIRef' 1)' '/' '.')'
		end
		#FileName := '$#FileName' '.' '$(stxfiletype wave)'
		#wsrc := $($BDataset CreateWave '$#ASetIRef ; $#ASegId ; *') 
		if '$#wsrc[?]' == wave then
			if '$(bsf 'open $#FileName;create;~$#wsrc[!srate];~$#wsrc[!channels]')' == 0 then
				#wdst := $(new wave * 0_~$#wsrc[!length])
				if $rc == 0 then
					if $($#wsrc copy $#wdst) == 0 $#newFiles * '$#FileName'
					delete $#wdst
				end
				bsf 'close $#FileName'
			else
				BUtil 'Msgbox Msg; Segment "$#ASetIRef/$#ASegId" - failed to create file "$#FileName" ; ToolBox "$#function" Error!'
			end
			$BDataset deletewave $#wsrc            
		else
			BUtil 'Msgbox Msg; Segment "$#ASetIRef/$#ASegId" - create waveitem failed! ; ToolBox "$#function" Error!'
		end
	end
end

BUtil 'Msgbox Msg; $#newFiles[] of $#etab[] segments exported ; ToolBox "$#function"'
if '$#newFiles[]' > 0 && '$#AddToDataset' == 1 then
	if $(readstr '$($BDataset DialogSelectElement Select target set for new sound files; single; iref; Set; 1)' #addSetRef) > 0 then
		while $#newFiles[] > 0
			$BDataset AddASet '$#newFiles[0];$#addSetRef;*'
			$#newFiles 0 /Delete
		end
		DatasetCmd RefreshAll
	end
end

$BDataset SelectIRef $#oldPos
$BDataset ResetPos $#oldPos
$BDataset ResetPos $#etab
$BDataset Detach

_tbexit:
$@TBSAVESETUP        // Save the settings (if any were changed)
exit 1 int $#changed // #changed must be set to 1 if any data was changed (otherwise 0)

Navigation menu

Personal tools