TimeConverter.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. namespace Bmp.Core.FFMpeg.CsCorePorts;
  2. /// <summary>
  3. /// Defines a base class for all time converts. A time converter can be used to convert raw positions (depending on the implementation i.e. bytes or samples) to a human
  4. /// readable <see cref="TimeSpan"/>.
  5. /// </summary>
  6. public abstract class TimeConverter
  7. {
  8. /// <summary>
  9. /// A <see cref="TimeConverter"/> for <see cref="ISampleSource"/> objects.
  10. /// </summary>
  11. public static readonly TimeConverter SampleSourceTimeConverter = new _SampleSourceTimeConverter();
  12. /// <summary>
  13. /// A <see cref="TimeConverter"/> for <see cref="IWaveSource"/> objects.
  14. /// </summary>
  15. public static readonly TimeConverter WaveSourceTimeConverter = new _WaveSourceTimeConverter();
  16. /// <summary>
  17. /// Converts a <see cref="TimeSpan"/> back to raw elements, a source works with. The unit of these raw elements depends on the implementation. For more information, see <see cref="TimeConverter"/>.
  18. /// </summary>
  19. /// <param name="waveFormat">The <see cref="WaveFormat"/> of the source which gets used to convert the <paramref name="timeSpan"/>.</param>
  20. /// <param name="timeSpan">The <see cref="TimeSpan"/> to convert to raw elements.</param>
  21. /// <returns>The converted <see cref="TimeSpan"/> in raw elements.</returns>
  22. public abstract long ToRawElements(WaveFormat waveFormat, TimeSpan timeSpan);
  23. /// <summary>
  24. /// Converts raw elements to a <see cref="TimeSpan"/> value. The unit of these raw elements depends on the implementation. For more information, see <see cref="TimeConverter"/>.
  25. /// </summary>
  26. /// <param name="waveFormat">The <see cref="WaveFormat"/> of the source which gets used to convert the <paramref name="rawElements"/>. </param>
  27. /// <param name="rawElements">The raw elements to convert to a <see cref="TimeSpan"/>.</param>
  28. /// <returns>The <see cref="TimeSpan"/>.</returns>
  29. public abstract TimeSpan ToTimeSpan(WaveFormat waveFormat, long rawElements);
  30. internal class _WaveSourceTimeConverter : TimeConverter
  31. {
  32. public override long ToRawElements(WaveFormat waveFormat, TimeSpan timeSpan)
  33. {
  34. return waveFormat.MillisecondsToBytes(timeSpan.TotalMilliseconds);
  35. }
  36. public override TimeSpan ToTimeSpan(WaveFormat waveFormat, long rawElements)
  37. {
  38. return TimeSpan.FromMilliseconds(waveFormat.BytesToMilliseconds(rawElements));
  39. }
  40. }
  41. internal class _SampleSourceTimeConverter : TimeConverter
  42. {
  43. public override long ToRawElements(WaveFormat waveFormat, TimeSpan timeSpan)
  44. {
  45. return waveFormat.MillisecondsToBytes(timeSpan.TotalMilliseconds) / waveFormat.BytesPerSample;
  46. }
  47. public override TimeSpan ToTimeSpan(WaveFormat waveFormat, long rawElements)
  48. {
  49. return TimeSpan.FromMilliseconds(waveFormat.BytesToMilliseconds(rawElements * waveFormat.BytesPerSample));
  50. }
  51. }
  52. }