I'm writing a folder of single column text files out to samples. Like the Sample Editor in Kyma, I can process text files in a script one at a time (I have a working script for one text file writing out to buffer SampleFile). I'm looking to make this a batch process on a folder of text files. Currently, I can get an array of text files and loop through (debug is working through this loop of text files), but when I create a new SampleFile and stream values into a WriteBuffer (and then ultimately close), I don't see any .aif files in my Finder. Lots of searching to see if placed in another directory, but cannot find. Do I need to specify a folder for batch .aif files to be stored into first? Or perhaps a user error :) (if there's an easier way to copy code here, I've placed into block quote to make it easier to follow). Thanks for any ideas!
| f file fileName lineValArray samplesFile writeBuffer maxVal filenameArray filesInSameFolder |
maxVal := 0. "init maxVal"
fileName := '0.txt'.
file := Preferences
locateFileNamed: fileName
ofType: HostDriverInterface textFileType
locateMessage: ' locate the text file named '.
file == nil ifTrue: [^ self abortForKyma].
filesInSameFolder := file fileNamesInSameFolderWithSameExtension.
"for multiple files in one batch process we will need this."
"Collect the filenames into an array."
filenameArray := 1 to: filesInSameFolder size collect: [:i | i].
filenameArray do: [:i | | "no vars here" |
f := (filesInSameFolder at: i) asFilename textFileStream.
"read through file line by line"
"collect maximum for normalization in first iteration"
[f atEnd] whileFalse: [
lineValArray := f nextParameters. "index 1 for 0th element"
maxVal := maxVal vmax: ((lineValArray at: 1) abs).
].
f reset. "reset pointer to start of the text file"
"Create a SamplesFile and set up its header."
"self debugWithLabel: 'name' value: ((((filesInSameFolder at: i) asFilename)displayTailWithoutFileExtension))."
samplesFile := SamplesFile fromNewFilename: ((((filesInSameFolder at: i) asFilename)displayTailWithoutFileExtension)asString&'_batch.aif') asFilename.
"working up to this point!"
samplesFile
format: #aiff;
isBigEndian: true;
channels: 1;
sampleRate: SignalProcessor sampleRate;
sampleSize: 24;
sampleFrames: 0;
writeHeader.
"Get a stream to write samples."
writeBuffer := samplesFile writeBuffer.
"self debugWithLabel: 'maxVal' value: (maxVal inverse)."
maxVal := maxVal inverse. "use inverse for multiplication in writing to audio"
"loop through data again, write to audio file and normalize"
[f atEnd] whileFalse: [
lineValArray := f nextParameters.
"self debugWithLabel: 'lineValArray' value: (lineValArray at: 1)."
writeBuffer writeSample: (lineValArray at: 1) * ( maxVal). "audio is normalized to data's max value (abs so doesn't matter)"
].
"Close the stream."
writeBuffer close.
].