Author Topic: Adding and removing a playback offset to a BASS_StreamCreatePush  (Read 323 times)

Jared Lumen

  • Guest
I am building a AV recording system and plan to use Bass.Net as the audio API.
I need to synchronize the audio and video input and there is a delay in receiving the images from the cameras.

Is there a simple way to dynamically adjust the audio latency so I can manually synchronize it to the video input?

I currently have this code that partially works but Bass.BASS_ChannelSetPosition does not seem to set the playback position...

private float currentLatency = 0;

    private void OnAddOffset(float milliseconds)
        float new_latency = (milliseconds / 1000f);
        double difference_in_seconds = new_latency - currentLatency;

        if (difference_in_seconds < 0)
            long current_pos_in_bytes = Bass.BASS_ChannelGetPosition(outputID, BASSMode.BASS_POS_BYTES);
            long difference_in_bytes = Bass.BASS_ChannelSeconds2Bytes(outputID, -difference_in_seconds);

            long new_pos = current_pos_in_bytes - difference_in_bytes;

            // Move the play position back in time - this does not work!
            Bass.BASS_ChannelSetPosition(outputID, new_pos, BASSMode.BASS_POS_BYTES);
          // This works!
          // Add the difference_in_seconds to the BASS_StreamPutData by adding silence to the current buffer.
            long bytes_to_seconds = Bass.BASS_ChannelSeconds2Bytes(outputID, difference_in_seconds);
            byte[] buffer = new byte[(int)bytes_to_seconds];

            for (int i = 0; i < bytes_to_seconds; i++)
                buffer = 0;

            Bass.BASS_StreamPutData(outputID, buffer, (int)bytes_to_seconds);

        currentLatency = new_latency;

Any advice would be greatly appreciated!


Jared Lumen

  • Guest
I can see now why this wont work. I am trying to set the position in a playback buffer rather than a sample.
Adding silence to the buffer to create an offset works but this is possibly a hideous hack.

Ian @ un4seen

  • Administrator
  • Posts: 21461
Do you only need to add latency at the start, or do you need to constantly adjust it mid-playback? If it's only at the start, then feeding silence seems like a reasonable solution. If you need to make mid-playback adjustments, then you could do that without audible gaps/skips by slightly changing the playback rate via the stream's BASS_ATTRIB_FREQ setting, eg. a lower rate will increase latency.