Rss Categories

Convert buffer offset to milliseconds

Reference Number: AA-01882 Views: 3153 0 Rating/ Voters

The LumenVox API returns the offset for SSML markers and viseme markers in terms of a buffer offset i.e. number of bytes from the 0th byte. However, that number may not be meaningful without information regarding the sampling rate and audio format. It is sometimes more useful to keep track of the offset in terms of milliseconds.

This page provides some pseudo-code to describe the conversion from buffer offset to number of milliseconds for each of the audio formats we accept. The below pseudo-code is for the C interface, however, the same conversion formula would work for the C++ interface.

C Code

  1. int TTS_handle;  // Handle from LV_TTS_CreateClient in C interface
  2. int audio_format, sampling rate;  // Got from LumenVox API
  3. int buffer_offset;  // Offset returned for ssml or viseme markers
  4. float offset_ms;  // Output offset in milliseconds

  5. ////////////////////////////////////////////////////////////////////////////
  6. // Insert code that does the synthesis and gets the ssml or viseme
  7. // marker offsets using the LumenVox API
  8. ////////////////////////////////////////////////////////////////////////////

  9. // Get audio format and sample rate from TTS Server
  10. LV_TTS_GetSynthesizedAudioFormat(TTSClientHandle, audio_format);
  11. LV_TTS_GetSynthesizedAudioSampleRate(TTS_handle, sampling_rate);

  12. // Calculate milliseconds offset based on audio format
  13. // The below formulas are generic for the C and C++ interfaces
  14. switch(audio_format)
  15. {
  16. case SFMT_ULAW:
  17. case SFMT_ALAW:
  18. // We multiply by 1000 to convert seconds to milliseconds
  19. offset_ms = (float)buffer_offset * 1000 /sampling_rate;
  20. break;
  21. case SFMT_PCM:
  22. // We divide by 2 because there are 2 bytes per PCM sample
  23. offset_ms = (float)buffer_offset / 2 * 1000 / sampling_rate;
  24. break;
  25. }

  26. // Offset_ms now contains buffer_offset converted to milliseconds