|
| 1 | +// Copyright (c) TensorStack. All rights reserved. |
| 2 | +// Licensed under the Apache 2.0 License. |
| 3 | +using System.Threading; |
| 4 | +using System.Threading.Tasks; |
| 5 | +using TensorStack.Audio.Windows; |
| 6 | +using TensorStack.Common.Tensor; |
| 7 | + |
| 8 | +namespace TensorStack.Audio |
| 9 | +{ |
| 10 | + /// <summary> |
| 11 | + /// Class to handle processing of a audio from file. |
| 12 | + /// </summary> |
| 13 | + public class AudioInput : AudioInputBase |
| 14 | + { |
| 15 | + private readonly string _sourceFile; |
| 16 | + |
| 17 | + /// <summary> |
| 18 | + /// Initializes a new instance of the <see cref="AudioInput"/> class. |
| 19 | + /// </summary> |
| 20 | + /// <param name="filename">The filename.</param> |
| 21 | + public AudioInput(string filename, string audioCodec = "pcm_s16le", int sampleRate = 16000, int channels = 1) |
| 22 | + : this(filename, AudioManager.LoadTensor(filename, audioCodec, sampleRate, channels)) { } |
| 23 | + |
| 24 | + /// <summary> |
| 25 | + /// Initializes a new instance of the <see cref="AudioInput"/> class. |
| 26 | + /// </summary> |
| 27 | + /// <param name="filename">The filename.</param> |
| 28 | + /// <param name="audioTensor">The audio tensor.</param> |
| 29 | + protected AudioInput(string filename, AudioTensor audioTensor) |
| 30 | + : base(audioTensor) |
| 31 | + { |
| 32 | + _sourceFile = filename; |
| 33 | + } |
| 34 | + |
| 35 | + /// <summary> |
| 36 | + /// Gets the source audio filename. |
| 37 | + /// </summary> |
| 38 | + public override string SourceFile => _sourceFile; |
| 39 | + |
| 40 | + |
| 41 | + /// <summary> |
| 42 | + /// Save the Audio to file |
| 43 | + /// </summary> |
| 44 | + /// <param name="filename">The filename.</param> |
| 45 | + public override void Save(string filename) |
| 46 | + { |
| 47 | + AudioManager.SaveAudio(filename, this); |
| 48 | + } |
| 49 | + |
| 50 | + |
| 51 | + /// <summary> |
| 52 | + /// Save the Audio to file asynchronously |
| 53 | + /// </summary> |
| 54 | + /// <param name="filename">The filename.</param> |
| 55 | + /// <param name="cancellationToken">The cancellation token.</param> |
| 56 | + public override async Task SaveAsync(string filename, CancellationToken cancellationToken = default) |
| 57 | + { |
| 58 | + await AudioManager.SaveAudioAync(filename, this, cancellationToken); |
| 59 | + } |
| 60 | + |
| 61 | + |
| 62 | + /// <summary> |
| 63 | + /// Create a AudioInput asynchronously |
| 64 | + /// </summary> |
| 65 | + /// <param name="filename">The filename.</param> |
| 66 | + /// <param name="cancellationToken">The cancellation token.</param> |
| 67 | + public static async Task<AudioInput> CreateAsync(string filename, string audioCodec = "pcm_s16le", int sampleRate = 16000, int channels = 1, CancellationToken cancellationToken = default) |
| 68 | + { |
| 69 | + return new AudioInput(filename, await AudioManager.LoadTensorAsync(filename, audioCodec, sampleRate, channels, cancellationToken)); |
| 70 | + } |
| 71 | + } |
| 72 | +} |
0 commit comments