AudioFormatWriter::writeFromAudioSource fails for user?


#1

Hello, Juceys. Back in Brooklyn again.

I have a reliable user who’s reporting an error saving that amounts to the fact that AudioFormatWriter::writeFromAudioSource is returning false. This feature has been reliable in testing on Mac/PC. Unfortunately, there’s no way to get any other sort of information from Juce other than it failed…

The code is fairly simple and I have pasted some of it below. I essentially create a new FileOutputStream for the file, use it to create an AudioFormatWriter, and then starting writing to it. It seems to me that that it must be the case that the virtual function

virtual bool AudioFormatWriter::write (const int** samplesToWrite, int numSamples) = 0; most be returning false, but since this is at a client I’m sort of stymied.

Question:

Could the filename be somehow invalid (it looks valid in the logs but…)? If the file were somehow impossible to write to, would I not get an error before this point, when I created the FileOutputStream or the AudioFormatWriter?

Here’s some of the code:[code]
ptr fos(new FileOutputStream(f));
ptr writer(fmt->createWriterFor(fos.transfer(), sampleRate,
channels, 16,
StringPairArray(),
quality));
[…]

for (SampleTime toCopy = length_;
     !threadShouldExit() && toCopy > 0; toCopy -= COPY_UPDATE_SIZE) {
  if (writer->writeFromAudioSource(*source_,
                                    std::min(COPY_UPDATE_SIZE, toCopy.toInt()),
                                    COPY_BLOCK_SIZE)) {
    setProgress(1.0 - toCopy / (1.0 * length_));
  } else {
    writer.reset();
    file_.deleteFile();
    String error = String::formatted(FILE_SAVE_FAILED_FULL,
                                     c_str(file_.getFileName()));
    AlertWindow::showMessageBox(AlertWindow::InfoIcon, FILE_SAVE_FAILED,
                                error, OK);
    return false;
  }
}
return true;[/code]

#2

Well… I guess that since you’re not checking FileOutputStream::openedOk() then it could indeed be a duff stream that’s getting passed in there.


#3

Oh, interesting, but does it give me any better idea what would have went wrong?

I’m believing that the FileDialog allowed the user to enter a file name with a " in it - is this possible?


#4

Yeah, you’re allowed quotes in a filename.


#5

I’ve fixed the issue by calling File::createLegalPathName on the file that gets returned from the FileBrowserComponent:

    File f =  dialogBox.show() ? fileBrowser.getSelectedFile(0) : File::nonexistent;
    file = File(File::createLegalPathName(file.getFullPathName()));

which is works fine - but I believe that the problem is in FileBrowserComponent and FileChooser, neither of which should allow you to enter a file which is not a legal path name. Quotation marks are not legal in files, they are eliminated by File::createLegalPathName, they should not appear in file names coming from a chooser.


#6

I can create filenames with quotes in them! (Perhaps it’s a Windows restriction?)


#7

Wait, I thought you were on Windows?

Well, apparently you can’t. And my code sort of encourages you to try. Ooops!

But I guess I’m the only one who’s really tested it, and I’m on the Mac.

Regardless, were I you I’d throw the results of any FileBrowser through File::makeLegalPathName and advertise that this is in fact a legal pathname…