-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathAudioTensor.cs
More file actions
83 lines (72 loc) · 2.75 KB
/
AudioTensor.cs
File metadata and controls
83 lines (72 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright (c) TensorStack. All rights reserved.
// Licensed under the Apache 2.0 License.
using System;
using System.Collections.Generic;
namespace TensorStack.Common.Tensor
{
/// <summary>
/// Class to handle audio in Tensor format
/// Implements the <see cref="Tensor{float}" />
/// </summary>
/// <seealso cref="Tensor{float}" />
public class AudioTensor : Tensor<float>
{
protected int _sampleRate;
protected TimeSpan _duration;
/// <summary>
/// Initializes a new instance of the <see cref="AudioTensor"/> class.
/// </summary>
/// <param name="tensor">The tensor.</param>
/// <param name="sampleRate">The source audio sample rate.</param>
public AudioTensor(Tensor<float> tensor, int sampleRate = 16000)
: base(tensor.Memory, tensor.Dimensions)
{
_sampleRate = sampleRate;
ThrowIfInvalid();
}
/// <summary>
/// Gets the audio channel count (Mono, Stereo etc)
/// </summary>
public int Channels => Dimensions[0];
/// <summary>
/// Gets the sample count.
/// </summary>
public int Samples => Dimensions[1];
/// <summary>
/// Gets the sample rate.
/// </summary>
public int SampleRate => _sampleRate;
/// <summary>
/// Gets the duration.
/// </summary>
public TimeSpan Duration => TimeSpan.FromSeconds((double)Samples / SampleRate);
/// <summary>
/// Splits the Audio specified second chunks.
/// </summary>
/// <param name="seconds">The seconds.</param>
public IEnumerable<AudioTensor> Chunk(int seconds)
{
int channels = Channels;
int totalSamples = Samples;
int samplesPerChunk = seconds * SampleRate;
for (int start = 0; start < totalSamples; start += samplesPerChunk)
{
int length = Math.Min(samplesPerChunk, totalSamples - start);
var slice = new Tensor<float>([channels, length]);
for (int c = 0; c < channels; c++)
for (int i = 0; i < length; i++)
slice[c, i] = this[c, start + i];
yield return slice.AsAudioTensor(SampleRate);
}
}
/// <summary>
/// Throws if Dimensions are invalid.
/// </summary>
protected void ThrowIfInvalid()
{
ArgumentOutOfRangeException.ThrowIfLessThanOrEqual(Samples, 0, nameof(Samples));
ArgumentOutOfRangeException.ThrowIfLessThanOrEqual(Channels, 0, nameof(Channels));
ArgumentOutOfRangeException.ThrowIfLessThanOrEqual(SampleRate, 0, nameof(SampleRate));
}
}
}