First time here? Check out the FAQ!
x

Is it possible to write a wavetable using smalltalk?

0 votes
856 views
Just reading through the smalltalk appendix again... I was wondering if I can generate a smalltalk collection I can write to disk during compile time. maybe streams? they seem to be able to read them at least..

Thanks!
asked Apr 22, 2016 in Capytalk & Smalltalk by kymaguy (Virtuoso) (10,580 points)

1 Answer

+2 votes

Here is a simple example (also see Chris's answer from last year):

| samplesFilename samplesFile writeBuffer |

"Ask for the file name."
samplesFilename :=
    HostDriverInterface
        putFileWithPrompt: 'Save as samples file named:'
        initially: 'untitled.aif' asFilename
        fileTypes: (Array with: HostDriverInterface aiffFileType)
        ifCancel: [^ self abortForKyma].

"Create a SamplesFile and set up its header."
samplesFile :=  SamplesFile fromNewFilename: samplesFilename.
samplesFile
    format: #aiff;
    isBigEndian: true;
    channels: 1;
    sampleRate: SignalProcessor sampleRate;
    sampleSize: 24;
    sampleFrames: 0;
    writeHeader.

"Get a stream to write samples."
writeBuffer := samplesFile writeBuffer.

"Output the sample values."
0 to: 4095 do: [ :i | writeBuffer writeSample: i / 4095.0].

"Close the stream."
writeBuffer close.

answered Apr 22, 2016 by ssc (Savant) (126,620 points)
whoops, sorry for asking the same thing twice.. Your simple example is great! Is there a way to write that wavetable to RAM so I can access it with the WaveShaper with FromMemoryWriter ticked?
or write a temporary wav to disk? but how can I make sure that the written wav is only temporary?
Use the same file name each time instead of prompting for a new file name.
sure, but I was going to use that in an encapsulated sound: so when using more than one instance I need to write unique wavetables. is there a way to delete the file when stopping the sound? any other ideas?
Thank you SSC for this example. It proved invaluable when I was working on a concatenation Script. I've placed in the Community Library – http://kyma.symbolicsound.com/library/concatenate-a-folder-of-files-into-one-audio-file/ – may serve others looking for examples that includes your code above.
I didn't try yet but I think you'll get problems when the files are mixed in samplerate and/or mono/stereo. Also I remember when creating the ROMTools it's easy to crash the smalltalk environment when reading big files. Or did you take care of that?
Thanks Gustav. I checked it out again this morning more thoroughly. Mixed mono/stereo has no issues (although I added a separate Sound to output stereo instead of writing everything down to mono). Samplerate does throw an error, even if I try to match the samplerate on the writeBuffer. I haven't tried on big files, just on items under 2 min.

Thanks for the heads up!
Jon, Thanks for the useful concatenation Script in the Community Library! A handy one to have in the library.
Regarding the mixed sample rates question, if you don't mind stepping out for a coffee, the easiest way around that would be to use the Script to schedule Samples to play one after another and then select Record to disk from the Action menu. Of course, this solution would record in real time so if you have longish samples, you would just start it up and go do something else for a while.  On the other hand, if you're just concatenating 4096-sample wavetables, it won't take long at all to record in real time.
Thank you SSC! Record-To-Disk is such a good tool. That makes perfect sense.
...