Can't change filenames when saving a file with SoundFile?


so I have this piece of code which I understood fairly well from a tutorial:

( // run once to convert and resample wavetable files
var paths, file, data, n, newData, outFile;
paths = "/mnt/4/sound/wtables/AKWF/AKWF_0002/*.wav".pathMatch;
Routine({ { |it i|
		// 'protect' guarantees the file objects will be closed in case of error
		protect {
			// Read original size of data
			file = SoundFile.openRead(paths[i]);
			data = Signal.newClear(file.numFrames);
			// Convert to n = some power of 2 samples.
			// n = data.size.nextPowerOfTwo;
			n = 4096;
			newData = data.resamp1(n);
			// Convert the resampled signal into a Wavetable.
			// resamp1 outputs an Array, so we have to reconvert to Signal
			newData =;

			// save to disk.
			outFile = SoundFile(paths[i] ++ "_4096.wtable")
			if(outFile.openWrite.notNil) {
			} {
				"Couldn't write output file".warn;
		} {
			if(outFile.notNil) { outFile.close };


it basically converts a bank of wavetables (stored in AKWF_0002) which are 600 samples long to proper 4096 samps wavetables (double that cause of how wavetables work in SC) and saves them. The problem is something doesn’t work in the saving process, in the final piece of code (outFile variable), it adds the string after the header, so I get lots of “.wav_4096.wavetable” files. Is there any way to solve this? I can’t seem to find a solution. Thanks!

I usually write paths[i].splitext[0] ++ "_4096.wtable". .splitext[0] removes the old extension.


Doesn’t work. If I apply splitext to paths[i] only, I get .wtable files, if I apply it to both paths[i] and the string, I get files with no extension. I need .wav files.

The best way to ask a tech question is:

  • Here is the information I have.
  • Here is the information (or format of information) that I want.

Anything less than this is guesswork and will waste time with vague or incorrect responses.

It’s not clear to me how the original filename looks – the whole filename, with extension – and also not clear to me what you would like to convert it to. So I don’t feel able to give more specific advice, based on the available information.


I explained it in the first post, maybe I wasn’t clear enough. The code converts 600 samples long .wav files (wavetables) to 4096 samples long .wav files. I just need to save them to .wav again with a different name, and that’s where the problem is (last piece of code, explained by comments). Basically when saving, SoundFile first adds the .wav extension, then appends the string, and I need the other way around.

Without testing I would suspect that keeping two dots in the filename could cause problems. It looks like you a going for something like: somename_4096.wtable.wav, what about if you try somename_4096wtable.wav (without the dot between 4096 and wtable)?

At least for me it wasn’t clear. It’s really helpful to be specific, like “I have a 600-sample wavetable file called xyz.wav, and I want my new file to be called xyz-4096.wav.”

So it sounds like what you need to do is 1/ strip off the extension, 2/ add something to the base name, 3/ add “.wav” again.

.splitext[0] takes care of #1. The others, I guess you can do on your own with some ++ operations.


Nope. Just gives me files with no extension ending with 4096wtable.

Solved it:

outFile = SoundFile(paths[i].splitext[0] ++ “_4096.wtable.wav”)

files seem okay.

I tested your original code a bit. The problem is that paths[i] points to the filename including .wav so anything after that is appended after .wav (as you experienced). The splitext gets rid of the extension. The other problems is that AFAIK there is no way of making .openWrite implicitly add the extension (on Mac OS at least) so .wav has to be explicitly added to the file name.