-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
57 lines (47 loc) · 1.98 KB
/
Program.cs
File metadata and controls
57 lines (47 loc) · 1.98 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
using System;
using System.IO;
using System.Reflection;
namespace accEncoderTest
{
class Program
{
private static readonly int _sampleRate = 8000;
private static readonly int _bitRate = 16000;
private static readonly int _channels = 1;
static void Main(string[] args)
{
Console.WriteLine("AAC Encoder test!");
DllMap.Register(Assembly.GetExecutingAssembly());
if (!AacEncoder.EncoderInit(_sampleRate, _channels, _bitRate))
{
Console.WriteLine("AAC Encoder NOT initialized!");
return;
}
var inputBufferSize = AacEncoder.GetInputBufferSize();
var outputBufferMaxSize = AacEncoder.GetMaxOutputBufferSize();
if (inputBufferSize <= 0 || outputBufferMaxSize <= 0)
{
Console.WriteLine("AAC Encoder inputBufferSize or outputBufferMaxSize not initialized!");
return;
}
var inputBuffer = new byte[inputBufferSize];
var outputBuffer = new byte[outputBufferMaxSize];
var outputPtr = InteropHelper.AllocByteArray(outputBuffer);
var stream = new FileStream("input.wav", FileMode.Open, FileAccess.Read);
var aacStream = new FileStream("output.aac", FileMode.Create, FileAccess.Write);
int read;
while ((read = stream.Read(inputBuffer, 0, inputBufferSize)) > 0)
{
var inputPtr = InteropHelper.AllocByteArray(inputBuffer);
var size = AacEncoder.EncodeBuffer(inputPtr, read, outputPtr, outputBufferMaxSize);
InteropHelper.FreeObjectArray(inputPtr, typeof(byte), inputBufferSize);
if (size <= 0) continue;
if (InteropHelper.ParsePtrToByteArray(outputPtr, size, out var result))
{
aacStream.Write(result);
}
}
aacStream.Close();
}
}
}