mackron / miniaudio

Audio playback and capture library written in C, in a single source file.
https://miniaud.io
Other
4.07k stars 361 forks source link

Playback continues past range end #630

Closed erikjalevik closed 1 year ago

erikjalevik commented 1 year ago

Using the high-level API, I seem to be unable to restrict playback to a range of a sound. Playback starts at the range start point, but doesn't stop at the range's end point.

Assuming an initialised engine, here's a simplified version of the code:

  const ma_uint32 flags = 0;
  auto result = ma_sound_init_from_file(
    mpEngine.get(), "1234.wav", flags, NULL, NULL, mpSound.get());
  if (result != MA_SUCCESS) {
    printf("Sound init failed: %i\n", result);
    return result;
  }

  ma_uint64 rangeStart = 40000;
  ma_uint64 rangeEnd = 60000;

  // Check length just to be sure we're within it
  ma_uint64 numFrames;
  ma_sound_get_length_in_pcm_frames(mpSound.get(), &numFrames);
  printf("Range: %llu - %llu (%llu)\n", rangeStart, rangeEnd, numFrames);
  // Outputs: Range: 40000 - 60000 (92425)

  result = ma_data_source_set_range_in_pcm_frames(
    mpSound->pDataSource, rangeStart, rangeEnd);
  if (result != MA_SUCCESS) {
    printf("Setting range failed: %i\n", result);
    return result;
  }

  result = ma_sound_start(mpSound.get());
  if (result != MA_SUCCESS) {
    printf("Sound start failed: %i\n", result);
    return result;
  }

This will always play the sound to the end of the file.

I had a quick look around the source code, and from what I can tell, it looks like ma_data_source_read_pcm_frames_within_range is supposed to return MA_AT_END when hitting the end of the range, but it never does. Neither frameCount nor framesRead ever seem to become 0.

Debug output:

DEBUG: Failed to initialize WASAPI backend.
DEBUG: Failed to initialize DirectSound backend.
DEBUG: Failed to initialize WinMM backend.
DEBUG: Attempting to initialize Core Audio backend...
DEBUG: Loading library: CoreFoundation.framework/CoreFoundation
DEBUG: Loading symbol: CFStringGetCString
DEBUG: Loading symbol: CFRelease
DEBUG: Loading library: CoreAudio.framework/CoreAudio
DEBUG: Loading symbol: AudioObjectGetPropertyData
DEBUG: Loading symbol: AudioObjectGetPropertyDataSize
DEBUG: Loading symbol: AudioObjectSetPropertyData
DEBUG: Loading symbol: AudioObjectAddPropertyListener
DEBUG: Loading symbol: AudioObjectRemovePropertyListener
DEBUG: Loading library: AudioUnit.framework/AudioUnit
DEBUG: Loading symbol: AudioComponentFindNext
DEBUG: Loading symbol: AudioComponentFindNext
DEBUG: Loading symbol: AudioComponentInstanceDispose
DEBUG: Loading symbol: AudioComponentInstanceNew
DEBUG: Loading symbol: AudioOutputUnitStart
DEBUG: Loading symbol: AudioOutputUnitStop
DEBUG: Loading symbol: AudioUnitAddPropertyListener
DEBUG: Loading symbol: AudioUnitGetPropertyInfo
DEBUG: Loading symbol: AudioUnitGetProperty
DEBUG: Loading symbol: AudioUnitSetProperty
DEBUG: Loading symbol: AudioUnitInitialize
DEBUG: Loading symbol: AudioUnitRender
DEBUG: System Architecture:
DEBUG:   Endian: LE
DEBUG:   SSE2:   NO
DEBUG:   AVX2:   NO
DEBUG:   NEON:   YES
INFO: [Core Audio]
INFO:   External Headphones (Playback)
INFO:     Format:      32-bit IEEE Floating Point -> 32-bit IEEE Floating Point
INFO:     Channels:    2 -> 2
INFO:     Sample Rate: 48000 -> 48000
INFO:     Buffer Size: 480*3 (1440)
INFO:     Conversion:
INFO:       Pre Format Conversion:  NO
INFO:       Post Format Conversion: NO
INFO:       Channel Routing:        NO
INFO:       Resampling:             NO
INFO:       Passthrough:            YES
INFO:       Channel Map In:         {CHANNEL_FRONT_LEFT CHANNEL_FRONT_RIGHT}
INFO:       Channel Map Out:        {CHANNEL_FRONT_LEFT CHANNEL_FRONT_RIGHT}
mackron commented 1 year ago

Are you able to try the dev branch? I think I fixed an issue relating to this. I really need to get a new release done...

erikjalevik commented 1 year ago

A cursory test does indeed seem to indicate it's been fixed in the dev branch!

mackron commented 1 year ago

Thanks for checking. I'll go ahead and close this one.