AudioFormat Initial Check

So I’m trialing the FileDragAndDropTarget class… dragging an audio file, inside **bool** AudioTrack::isInterestedInFileDrag( **const** juce::StringArray &files) I would like to check if the incoming file’s format matches the registered formats.

Currently I’m doing a simple check from the filename

**for** ( **auto** file : files)

**if** (! file.endsWith (".wav"))

**return** **false** ;

However, this only pertains to the ‘wav’ file… and would have to write more for each individual audio format that I drag in.

I’ve gone through this process also:

   >  auto* reader = formatManager.createReaderFor(file);
    String filename = reader->getFormatName();

And then checking against that, which works however when dragging in a random file it obviously crashes because the ‘createReaderFor(file)’ has asserted that the file format does not contain in the listed formats.

Maybe I’m over complicating this. Does anyone have any initial format checks against the known registered formats?

1 Like

So here is my updated function:

**bool** AudioTrack::isInterestedInFileDrag( **const** juce::StringArray &files)

{

**for** ( **auto** file : files)

{

**auto** * reader = formatManager.createReaderFor(file);

/** If reader returns a nullptr, an incorrect file type has been dragged and function should return false */

**if** (reader == **nullptr** ) { **return** **false** ; }

}

**return** **true** ;

}

If you call createReaderFor(), it actually reads the beginning of the file, and checks whether there’s e.g. a valid WAV header. But if you just want to check for the file extension (.wav, .aiff, etc.) this would work:

WildcardFileFilter filter(formatManager.getWildcardForAllFormats(), {}, {});

for(auto file : files) {
    if (!filter.isFileSuitable(file))
        return false;
}

Just a sidenote: If you’re calling createReaderFor(), you’re responsible for deleting the returned reader. A good way to do this is to write unique_ptr<AudioFormatReader> reader(formatManager.createReaderFor(file));.

1 Like

Ok thank you. That makes more sense.

If I did want to create an AudioFormatReader object in the header file,

std::unique_ptr<AudioFormatReader> audioReader;

then instantiate it inside the **bool** AudioTrack::isInterestedInFileDrag( **const** juce::StringArray &files) function, so I can refer to it throughout the application… how would I go about this?

This is what I thought should work: audioReader.reset( new AudioFormatReader(formatManager.createReaderFor(file))); but doesn’t.

Is there a way I can achieve this?

audioReader.reset( new AudioFormatReader(formatManager.createReaderFor(file)));

Almost right. Just audioReader.reset(formatManager.createReaderFor(file)); should work.

Thank you. Appreciate the help :slight_smile: