Skip to content

Commit

Permalink
[AudioIO] Add new internal API for play audio with repetitions
Browse files Browse the repository at this point in the history
  • Loading branch information
hsgwon committed Oct 7, 2024
1 parent 2fa4078 commit 6c5c7ca
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 4 deletions.
4 changes: 4 additions & 0 deletions src/Tizen.Multimedia.AudioIO/Interop/Interop.WavPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ internal static partial class WavPlayer
internal static extern WavPlayerError Start(string filePath, AudioStreamPolicyHandle streamInfoHandle,
WavPlayerCompletedCallback completedCallback, IntPtr userData, out int id);

[DllImport(Libraries.WavPlayer, EntryPoint = "wav_player_start_loop")]
internal static extern WavPlayerError StartLoop(string filePath, AudioStreamPolicyHandle streamInfoHandle, uint count,
WavPlayerCompletedCallback completedCallback, IntPtr userData, out int id);

[DllImport(Libraries.WavPlayer, EntryPoint = "wav_player_stop")]
internal static extern WavPlayerError Stop(int id);
}
Expand Down
58 changes: 54 additions & 4 deletions src/Tizen.Multimedia.AudioIO/WavPlayer/WavPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

using System;
using System.ComponentModel;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -85,20 +86,69 @@ public static Task StartAsync(string path, AudioStreamPolicy streamPolicy,
}

return cancellationToken.IsCancellationRequested ? Task.FromCanceled(cancellationToken) :
StartAsyncCore(path, streamPolicy, cancellationToken);
StartAsyncCore(path, streamPolicy, 1, cancellationToken);
}

private static async Task StartAsyncCore(string path, AudioStreamPolicy streamPolicy,
/// <summary>
/// Plays a wav file based on the specified <see cref="AudioStreamPolicy"/> with given repetition number.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
/// <param name="path">A file path to play.</param>
/// <param name="streamPolicy">A <see cref="AudioStreamPolicy"/>.</param>
/// <param name="loopCount">A number of repetitions.</param>
/// <param name="cancellationToken">A cancellation token which can be used to stop.</param>
/// <exception cref="ArgumentNullException">
/// <paramref name="path"/> is null.
/// <para>-or-</para>
/// <paramref name="streamPolicy"/> is null.
/// </exception>
/// <exception cref="InvalidOperationException">An internal error occurs.</exception>
/// <exception cref="FileNotFoundException"><paramref name="path"/> does not exists.</exception>
/// <exception cref="FileFormatException">The format of <paramref name="path"/> is not supported.</exception>
/// <exception cref="ObjectDisposedException"><paramref name="streamPolicy"/> has already been disposed of.</exception>
[EditorBrowsable(EditorBrowsableState.Never)]
public static Task StartAsync(string path, AudioStreamPolicy streamPolicy, uint loopCount,
CancellationToken cancellationToken)
{
if (path == null)
{
throw new ArgumentNullException(nameof(path));
}

if (streamPolicy == null)
{
throw new ArgumentNullException(nameof(streamPolicy));
}

if (File.Exists(path) == false)
{
throw new FileNotFoundException("File does not exists.", path);
}

return cancellationToken.IsCancellationRequested ? Task.FromCanceled(cancellationToken) :
StartAsyncCore(path, streamPolicy, loopCount, cancellationToken);
}

private static async Task StartAsyncCore(string path, AudioStreamPolicy streamPolicy, uint loopCount,
CancellationToken cancellationToken)
{
int id = 0;
var tcs = new TaskCompletionSource<bool>();

Native.WavPlayerCompletedCallback cb = (id_, _) => tcs.TrySetResult(true);

using (var cbKeeper = ObjectKeeper.Get(cb))
{
Native.Start(path, streamPolicy.Handle, cb, IntPtr.Zero, out var id).
Validate("Failed to play.");
if (loopCount > 1)
{
Native.StartLoop(path, streamPolicy.Handle, loopCount, cb, IntPtr.Zero, out id).
Validate("Failed to play with loop.");
}
else
{
Native.Start(path, streamPolicy.Handle, cb, IntPtr.Zero, out id).
Validate("Failed to play.");
}

using (RegisterCancellationAction(tcs, cancellationToken, id))
{
Expand Down

0 comments on commit 6c5c7ca

Please sign in to comment.