reliably detect number of IT channels?

Started by fractalgp,

fractalgp

Quite a bit off-topic, but I don't know of any other message boards where I might get an answer to this:

I'm writing a cataloguing tool for my module collection in PHP. Currently the tool is able to extract basic information from the files (module type, title, size, number of channels) and stuff the files into a MySQL database.

Everything works well, except I have a problem with counting the number of used IT channels properly. I get 64 channels on more or less all of the ITs I try (though not all).

So, I was wondering if someone here might know how to reliably detect number of used channels in an IT module?

The current code I'm using is
fseek($filehandle,0);
if(fread($filehandle,4) == "IMPM")
      {
      $found = "IT";
      $title = trim(fread($filehandle,26));
      $channels = 0;
      for($i = 64;$i<128;$i++)
            {
            fseek($filehandle,$i);
            if(hexdec(bin2hex(fread($filehandle,1))) != 255)
                  {
                  fseek($filehandle,$i);
                  if(hexdec(bin2hex(fread($filehandle,1))) != 128)
                        $channels++;
                  }
            }
      }

The code is based on the following from the IT format documentation:
Quote     Chnl Pan: Each byte contains a panning value for a channel. Ranges from
                 0 (absolute left) to 64 (absolute right). 32 = central pan,
                 100 = Surround sound.
                 +128 = disabled channel (notes will not be played, but note
                                          that effects in muted channels are
                                          still processed)
the channel pan info starts from pos 0x40 and goes on for 64 bytes.

btw; If someone could help me get rid of that ugly hexdec(bin2hex(fread())) construct, I would be glad too :P

Ian @ un4seen

You have to go through the pattern data to see which (and so how many) of the channels are used.

podobo

If you don't feel like writing or adapting a pattern parser, you could use BASS and let it do the work for you..

int numchannels = 0;
while (BASS_MusicGetChannelVol(handle, channels) != -1)
    numchannels += 1;

Irrational86

Quoteint numchannels = 0;
while (BASS_MusicGetChannelVol(handle, channels) != -1)
    numchannels += 1;

You have a typo, it should be
int numchannels = 0;
while (BASS_MusicGetChannelVol(handle, numchannels) != -1)
    numchannels += 1;