Author Topic: Read a mp3 file .  (Read 185 times)

thib

  • Posts: 7
Read a mp3 file .
« on: 18 Jun '17 - 03:30 »
Hello iam new to the devellopement with BASS lib, so i encountered  some very basics problem.

i want to read and get the lenght of an mp3 file.The best i can do actualy in my code is that.......

Code: [Select]
    HSAMPLE hSample;
    HCHANNEL channel = NULL;


    hSample = BASS_SampleLoad(TRUE, "R.Kelly.wav", 0, 1, 1, BASS_SAMPLE_MONO);
    if(hSample == 0)
    {
        printf("Cant load the sample : %d\n", BASS_ErrorGetCode());
        exit(EXIT_FAILURE);
    }

    channel = BASS_SampleGetChannel(hSample, FALSE);
    if(channel == NULL)
    {
        printf("Can't create a channel : %d", BASS_ErrorGetCode());
        exit(EXIT_FAILURE);
    }

    BASS_Start();

    if (pos && pos!=-1)
songTime = (DWORD)BASS_ChannelBytes2Seconds(channel,pos);

    printf("%u:%02u\n", songTime / 60, songTime % 60);

i got the BASS_ERROR_FILEFORM error on return, but BASS can read wav or mp3 file , i tried with both.So the problem is somewhere else and to be honnest i dont know where.

Can someone can give me some help plz ?

Thanks !

PS : my coding langage is the C.

thib

  • Posts: 7
Re: Read a mp3 file .
« Reply #1 on: 18 Jun '17 - 05:01 »
Fi FI Fi Fi Fixedddddddddd  ;D
that work !!! i need now to find how to get the lenght of the playing track.

My actual code wish work for any beginner , i dunno that can help...maybe.

Code: [Select]
    HSAMPLE hSample;
    HCHANNEL channel = NULL;


    hSample = BASS_SampleLoad(FALSE, path, 0, 0, 1, BASS_SAMPLE_MONO);
    if(hSample == 0)
    {
        printf("Cant load the sample : %d\n", BASS_ErrorGetCode());
        exit(EXIT_FAILURE);
    }

    channel = BASS_SampleGetChannel(hSample, FALSE);
    if(channel == NULL)
    {
        printf("Can't create a channel : %d", BASS_ErrorGetCode());
        exit(EXIT_FAILURE);
    }

    BASS_ChannelPlay(hSample, FALSE);

    if (pos && pos!=-1)
        songTime = (DWORD)BASS_ChannelBytes2Seconds(channel,pos);

    printf("%u:%02u\n", songTime / 60, songTime % 60);

Ian @ un4seen

  • Administrator
  • Posts: 20336
Re: Read a mp3 file .
« Reply #2 on: 19 Jun '17 - 16:57 »
When you say you want to "read" an MP3 file, do you mean you want to decode it (eg. to memory), or do you want to play it? In either case, it is usually best to use a "stream" rather than a "sample" for that, ie. BASS_StreamCreateFile rather than BASS_SampleLoad. The difference is that a stream decodes the file while it plays, and a sample pre-decodes the entire file to memory. You can use BASS_ChannelGetLength to get its length:

Code: [Select]
stream = BASS_StreamCreateFile(FALSE, path, 0, 0, 0); // create a stream for the file
QWORD bytelen = BASS_ChannelGetLength(stream, BASS_POS_BYTE); // get its length in bytes
double seclen = BASS_ChannelBytes2Seconds(stream, bytelen); // translate it to seconds