Example Toolbox function
From STX Wiki
Jump to navigationJump to search
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)