Author Topic: Identifying the type of music file  (Read 2654 times)

0 Members and 1 Guest are viewing this topic.

Offline Bert22306

  • Forum Regular
  • Posts: 206
    • View Profile
Identifying the type of music file
« on: October 14, 2018, 11:41:49 pm »
Hi guys, maybe this is obvious, but not to me. The motivation here is to cobble together a music player, shamelessly plagiarizing Fellippe's and Petr's code, which will play .wav and all other music files correctly.

Is it possible to load a sound file, say SoundFile$, and then have QB64 automatically determine to what subroutine to send that file, based on its extension?

Something along these lines:

INPUT "Enter path and sound file name with extension -> ", SoundFile$
(Appropriate error notifications if file is unavailable)
IF SoundFile$ = "*.wav" THEN
    CALL PetrSubroutine
    CALL NiceWavGraphicsSubroutine
ELSE
    CALL FellippeSubroutine
    CALL NiceGraphicsSubroutine
END IF

That wildcard * is just notional. It doesn't work. It's easy enough to have the user explicitly say whether the sound file is .wav or not, but that's rather brute force.

(Note that as of now, it looks like _SNDOPEN, _SNDLEN, and _SNDGETPOS don't work at all, with these 24-bit or 32-bit, or whatever they are .wav files. That's why I don't think my time bar will work with Petr's latest creation. To duplicate the display, I'll have to figure out how to obtain the length of the music clip, and the current position while it's playing.)

Thanks!!

FellippeHeitor

  • Guest
Re: Identifying the type of music file
« Reply #1 on: October 15, 2018, 12:10:50 am »
IF LCASE$(RIGHT$(fileName$, 4)) = “.wav” THEN
    'Act on wav file
ELSE
    'act on file with different extension
END IF

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Identifying the type of music file
« Reply #2 on: October 15, 2018, 12:30:21 am »
The best method would be to read the header of each file and look for the proper ID tags, in case the extension has been renamed. 

For example, for *.wav files, the first 4 bytes are "RIFF", and bytes 9-12 are "WAVE".  (http://www.topherlee.com/software/pcm-tut-wavformat.html )

Seems a little more reliable than just looking for ".wav" as an extension.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Identifying the type of music file
« Reply #3 on: October 15, 2018, 01:52:25 am »
Hi. It has several pitfalls. The first major problem is that _SNDRAW still may contain a bug and it does not play in stereo if it is not related to the SNDOPENRAW command that opens the next channel. I did not really try that. Another major problem is that there is no way to get RAW data from QB64 decoders such as MP3 or OGG. You need RAW data for visualization. You can not make a visualization based on the volume because you get a constant number, not a sound. The only possible way to get audio RAW data for visualization is _SNDRAW. Then you sort the sound according to the frequency (I did it according to the number of the passage by zero - or by changing the sign in a second) which is not absolutely correct. So, if you want to make a visualization,  use third-party decoders after converting to WAV or RAW. RAW is the same as WAV, but without the head, so without information about data stream, format (mono-stereo) and about biterate. This RAW information must therefore be retrieved from the original compressed audio file. The method of identifying the audio file is definitely the one described by Steve and this is really best way. It means reading all the audio formats you want to support in detail. Depending on the file suffix, Windows only makes it a stupid way. The method of determining the position of the sound in the file I think count as LOF (sound_file) \ SEEK (sound_file) now I do not know exactly. If you look into my SNDRAW in action program! which is here somewhere, there is SUB MP3HEADER. It reads just the required MP3 data. MP3 compression but may provide poor data for correct reading, so more MP3 blocks have to be read and the most used values ​​are then used for playback. Just the MP3 decoding heads themselves (yeah, there are more) is a mad job. I'm not talking about the need to avoid added information such as ID3 title information, photo cover, and so on. ID3 can be used in OGG format, new WAV format...

Offline Bert22306

  • Forum Regular
  • Posts: 206
    • View Profile
Re: Identifying the type of music file
« Reply #4 on: October 15, 2018, 05:02:56 pm »
Thank you very much, Fellippe, Petr, and Steve, for you useful and learned replies. I'll try different approaches, and see what's doable. Thanks again!