23 May '13 - 02:38 *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
 
   Home   Help Search Login Register  
Pages: [1]
  Reply  |  Print  
Author Topic: Get current song position and length from internet stream  (Read 993 times)
Majkan
Posts: 29


« on: 23 Nov '11 - 18:44 »
Reply with quoteQuote

Hello

I'm trying to get the current time of the song, and the song length of the internet stream.

More precisely, i'm recording the stream and i want to split recoding by tracks.

I tried to get the song position but it not restarted on the next song.
Here is the code:

long decpos = Bass.BASS_ChannelGetPosition(_Stream,BASSMode.BASS_POS_BYTES | BASSMode.BASS_POS_DECODE);
TimeSpan.FromSeconds(Bass.BASS_ChannelBytes2Seconds(_Stream, decpos));

Also with the code below i'm getting some small time.
long decpos = Bass.BASS_StreamGetFilePosition(_Stream, BASSStreamFilePosition.BASS_FILEPOS_CURRENT);
TimeSpan.FromSeconds(Bass.BASS_ChannelBytes2Seconds(_Stream, decpos));
Thanks
« Last Edit: 27 Nov '11 - 12:11 by Majkan » Logged
Ian @ un4seen
Administrator
Posts: 15269


« Reply #1 on: 24 Nov '11 - 16:35 »
Reply with quoteQuote

BASS_ChannelGetPosition is the correct function to use. Are you wanting your position display to restart from 0 following a song change in a Shoutcast stream? If so, you could set a variable to the current position each time a new song starts, and subtract that from what BASS_ChannelGetPosition says. It could look something like this...

QWORD songstart; // current song start position

...

QWORD songpos=BASS_ChannelGetPosition(handle, BASS_POS_BYTE)-songstart; // get current song position
// display it...

...

//  META sync callback function
void CALLBACK MetaSyncProc(HSYNC handle, DWORD channel, DWORD data, void *user)
{
songstart=BASS_ChannelGetPosition(channel, BASS_POS_BYTE); // update song start position
// do your other new song stuff...
}
Logged
Majkan
Posts: 29


« Reply #2 on: 24 Nov '11 - 17:54 »
Reply with quoteQuote

Thank you Ian.

I thought that there was a more precise way that waiting for the Meta Data change.
Because it's sometimes 5 seconds difference.
Is this difference allways the same, or depending on the station?

Another question.
I'm recording the stream with the LAME encoder.
And i want to record full song from the begining.

How can i write recorded bytes with the lame and then continue to record same stream?
Should i use byte array or the BassBuffer in the DOWNLOADPROC?

Thanks
Logged
Ian @ un4seen
Administrator
Posts: 15269


« Reply #3 on: 25 Nov '11 - 14:31 »
Reply with quoteQuote

I thought that there was a more precise way that waiting for the Meta Data change.
Because it's sometimes 5 seconds difference.
Is this difference allways the same, or depending on the station?

Due to buffering, there will be some difference between when the metadata is received and when its position is atually heard. You could compensate for that in the "songstart" value. It isn't possible to know exactly how much decoded PCM data the buffered encoded data will produce, but you could estimate that using the stream's bitrate. For example, something like this...

QWORD delay=BASS_StreamGetFilePosition(channel, BASS_FILEPOS_DOWNLOAD) // downloading position
-BASS_StreamGetFilePosition(channel, BASS_FILEPOS_CURRENT); // minus decoding position
delay=BASS_ChannelSeconds2Bytes(channel, (double)delay*8/bps); // translate it to decoded PCM bytes
songstart=BASS_ChannelGetPosition(channel, BASS_POS_DECODE|BASS_POS_BYTE)+delay; // update song start position (current decoding position + buffer delay)

Where "bps" is the stream's bitrate in bits per second.

Another question.
I'm recording the stream with the LAME encoder.
And i want to record full song from the begining.

How can i write recorded bytes with the lame and then continue to record same stream?
Should i use byte array or the BassBuffer in the DOWNLOADPROC?

Is there a particular reason that you want to use LAME, eg. change the format/bitrate? If not, I would suggest just saving the already encoded data received in the DOWNLOADPROC function.

One thing to be aware of is that metadata is very unlikely to be placed exactly on a song change in a Shoutcast stream. So if you want perfectly placed splits, you may need to make some manual adjustment of the split positions.
Logged
Majkan
Posts: 29


« Reply #4 on: 25 Nov '11 - 18:28 »
Reply with quoteQuote

Thanks Ian, this example code helped me to better estimate the time difference.

Is there a particular reason that you want to use LAME, eg. change the format/bitrate?

Yes, true. I want to be able to change format/bitrate of the output file.

Basically, my idea is to constantly record each song, and then, when i decide to record current song from the begining, use the captured bytes, record them, and continue with the steam recording.

So my question is, how can i write recorded bytes with the lame, and then continue to record the stream?

Thanks
Logged
jaromanda
Posts: 22


« Reply #5 on: 26 Nov '11 - 06:42 »
Reply with quoteQuote

The accuracy of metadata change time depends on the stream SOURCE - and many will deliberately skew the change to screw with streamrippers - because streamrippers are scum
Logged
Majkan
Posts: 29


« Reply #6 on: 1 Dec '11 - 22:16 »
Reply with quoteQuote

Hello again

Is there a particular reason that you want to use LAME, eg. change the format/bitrate? If not, I would suggest just saving the already encoded data received in the DOWNLOADPROC function.

One thing to be aware of is that metadata is very unlikely to be placed exactly on a song change in a Shoutcast stream. So if you want perfectly placed splits, you may need to make some manual adjustment of the split positions.

i have made pretty good accuracy with the meta data, but i want to do it as Ian adviced.. using the downloadproc.

Problem is, that some stations switch meta data often... and my current song duration timer resets also.

Can i get somehow precise flag from the download proc when new song begins, and to calculate duration, or get the end position of the song?

Thanks
Logged
Ian @ un4seen
Administrator
Posts: 15269


« Reply #7 on: 5 Dec '11 - 15:09 »
Reply with quoteQuote

A BASS_SYNC_META sync will be triggered as soon as metadata is received from the server, so you could create a new file at that point (for your DOWNLOADPROC to write to). That will give you splits at exactly where the server sent them (note that won't necessarily be when a new song begins).

I'm afraid it isn't possible to know in advance how long the next song will be (that information isn't included in the metadata). You will need to just wait for the next metadata to be received.

Regarding the timer display resetting, you could set a BASS_SYNC_POS sync at the "songstart" position (as calculated above) when metadata is received, and then reset the timer in the SYNCPROC. That way the timer display isn't reset until the "songstart" position is heard.
Logged
Majkan
Posts: 29


« Reply #8 on: 6 Dec '11 - 00:24 »
Reply with quoteQuote

Thanks for the answer.
I thought that there is a better way that using the MD.. because i saw this in one radio player, where meta data was changing but still, whole song was recorded and had precise timer.

If i put BASS_SYNC_POS on my channel, it triggers only on start of the channel play.

So, to clarify, there is no better way of getting the flag when new song begins that using the Meta data sync?
Logged
Ian @ un4seen
Administrator
Posts: 15269


« Reply #9 on: 6 Dec '11 - 15:34 »
Reply with quoteQuote

The metadata is generally the only indication of a track change that the server sends. You could perhaps also check the decoded sample data (via a DSPPROC) for hints of a track change, eg. you could look for gaps, but that might not be very reliable.

Regarding your earlier mention of "some stations switch meta data often", are you sure the metadata is changing and it isn't just the same metadata being sent again? A BASS_SYNC_META sync will be triggered whenever metadata is received from the server, even if it is hasn't changed.
Logged
jdg2011
Posts: 18


« Reply #10 on: 6 Dec '11 - 15:44 »
Reply with quoteQuote

Hello Ian this is true the meta-data is not changing but being resent! It will display the Artist - Track but then a blank appears and then Artist - Track reappears again.

Here is one station that is resending meta-data

Юнистар (Минск) 128kbps
http://213.184.232.36:8000/

Thanks!
Logged
Pages: [1]
  Reply  |  Print  
 
Jump to:  

Powered by SMF 1.1.18 | SMF © 2013, Simple Machines