How does audacity detect endianness and channel number in imported raw audio data?

When importing raw audio file into audacity, it has a feature of detecting the endianness and number of channels. I was wondering how it does this. Can someone please give me a general overview of how this is even possible, and links to materials to read up on?
raw_import_detect.png
Windows: Windows 10 Pro ver. 21H2, OS build 19044.2486
Audacity: 3.1.3

Is that new? I don’t remember seeing it.

I don’t know but just thinking about it… If the audio data is random, with 16-bit data, 1/2 of the high-bytes bytes would be zero and the low-bytes will rarely be zero. With 24-bit data 1/3rd of the high-bytes would be zero and 2/3rds if the mid-bytes. With real audio the same trend should continue with highest bytes containing the majority of the zeros and the least significant byte more random.

Detecting the number of channels might not be so simple because there is usually a lot of correlation between the channels, and it seems really difficult to guess the sample rate.

It’s here in the Manual
https://alphamanual.audacityteam.org/man/File_Menu:_Import#raw_data

But I note that 2.4.2 did not have this Detect button …

UPDATE
Testing shows that the Detect button was added in 3.1.0

Peter.

Detecting the correct format isn’t 100% reliable. Automatic format detection is useful when it works, but for technical reasons it isn’t possible to automatically deduce the format with 100% reliability.

Probably the most annoying case is if you have a bunch of RAW audio files, and you know the format, but Audacity gets it wrong. In 2.4.2 and earlier, you would have to correct Audacity’s mistake for every file. Now that there’s a “Detect” button Audacity only attempts to guess the format if you press the button, so in this situation you don’t press the button, and you only need to (manually) set the correct format once.

Isn’t a simple explanation of how Audacity does this available?

I managed to some parts in the source code that might be relevant, but can’t understand what’s going on.
https://github.com/audacity/audacity/blob/master/src/import/ImportRaw.cpp

      S.StartTwoColumn();
      {
         /* i18n-hint: Guess format of raw file */
         S.Id(DetectID).AddButton(XXO("Detect"));

         //
         // Preview Pane goes here
         //

         S.AddStandardButtons();
      }
      S.EndTwoColumn();

      // Find the OK button, and change its text to 'Import'.
      // We MUST set mOK because it is used later.
      mOK = (wxButton *)wxWindow::FindWindowById(wxID_OK, this);
      mOK->SetLabel(_("&Import"));
   }



void ImportRawDialog::OnDetect(wxCommandEvent & event)
{
   try {
      // Yes, FormatClassifier currently handles filenames in UTF8 format only, that's a TODO ...
      FormatClassifier theClassifier(mFileName.utf8_str());
      mEncoding = theClassifier.GetResultFormatLibSndfile();
      mChannels = theClassifier.GetResultChannels();
   } catch (...) {
      // Something went wrong in FormatClassifier, abort.
      return;
   }

   int selection = 0;
   auto iter = std::find(mEncodingSubtype.begin(), mEncodingSubtype.end(), mEncoding & SF_FORMAT_SUBMASK);
   if (iter != mEncodingSubtype.end())   // subtype found
       selection = std::distance(mEncodingSubtype.begin(), iter);

   int endian = getEndianChoice(mEncoding);

   mEncodingChoice->SetSelection(selection);
   mEndianChoice->SetSelection(endian);
   mChannelChoice->SetSelection(mChannels - 1);

   OnChoice(event);
}

If you’re a developer, look at the code.
If you want to work on this code but require help with it, try asking on the developer’s discord channel: Audacity dev

If you’re not a developer, why does it matter?

There aren’t any Audacity developers on this forum (it’s a community forum where Audacity users help each other), but from a quick look at the code myself, I notice:

mEncoding = theClassifier.GetResultFormatLibSndfile();

So looking for “GetResultFormatLibSndfile” I find that it is defined in https://github.com/audacity/audacity/blob/master/src/import/FormatClassifier.cpp

Also, see: RawAudioGuess https://github.com/audacity/audacity/blob/master/src/import/RawAudioGuess.cpp