com.adrenak.univoice 4.8.0 → 4.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
using System;
|
|
2
|
+
using System.IO;
|
|
3
|
+
using System.Text;
|
|
4
|
+
|
|
5
|
+
/*
|
|
6
|
+
Example script for usage with UniMic. Just add it to a scene,
|
|
7
|
+
play and then quit/exit play mode:
|
|
8
|
+
|
|
9
|
+
public class WavFileWriterTest : MonoBehaviour {
|
|
10
|
+
WavFileWriter writer;
|
|
11
|
+
|
|
12
|
+
void Start() {
|
|
13
|
+
string path = string.Empty;
|
|
14
|
+
if (Application.isEditor)
|
|
15
|
+
path = Application.dataPath.Replace("Assets", "output.wav");
|
|
16
|
+
else
|
|
17
|
+
path = Path.Combine(Application.persistentDataPath, "output.wav");
|
|
18
|
+
|
|
19
|
+
writer = new WavFileWriter(path);
|
|
20
|
+
|
|
21
|
+
Mic.Init();
|
|
22
|
+
|
|
23
|
+
Mic.AvailableDevices[0].OnFrameCollected += OnFrameCollected;
|
|
24
|
+
Mic.AvailableDevices[0].StartRecording(20);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
private void OnFrameCollected(int arg1, int arg2, float[] arg3) {
|
|
28
|
+
writer.Write(arg1, arg2, arg3);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
private void OnDestroy() {
|
|
32
|
+
writer.Dispose();
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
*/
|
|
36
|
+
|
|
37
|
+
namespace Adrenak.UniVoice {
|
|
38
|
+
/// <summary>
|
|
39
|
+
/// A utility to write audio samples to a file on disk.
|
|
40
|
+
/// Construct using the path you want to store the audio file at.
|
|
41
|
+
/// Invoke Write with the sampling frequency, channel count and PCM samples
|
|
42
|
+
/// and it will lazily initialize.
|
|
43
|
+
/// </summary>
|
|
44
|
+
public class WavFileWriter : IDisposable {
|
|
45
|
+
FileStream fileStream;
|
|
46
|
+
int sampleRate;
|
|
47
|
+
short channels;
|
|
48
|
+
readonly short bitsPerSample = 16;
|
|
49
|
+
|
|
50
|
+
long dataChunkPos;
|
|
51
|
+
int totalSampleCount = 0;
|
|
52
|
+
bool isInitialized = false;
|
|
53
|
+
readonly string path;
|
|
54
|
+
|
|
55
|
+
public WavFileWriter(string path) {
|
|
56
|
+
this.path = path;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
public void Write(int frequency, int channelCount, float[] samples) {
|
|
60
|
+
if (!isInitialized) {
|
|
61
|
+
sampleRate = frequency;
|
|
62
|
+
channels = (short)channelCount;
|
|
63
|
+
fileStream = new FileStream(path, FileMode.Create, FileAccess.Write);
|
|
64
|
+
WriteWavHeader();
|
|
65
|
+
isInitialized = true;
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
if (frequency != sampleRate || channelCount != channels)
|
|
69
|
+
throw new InvalidOperationException("Inconsistent frequency or channel count between calls.");
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
byte[] buffer = new byte[samples.Length * 2]; // 2 bytes per sample (16-bit PCM)
|
|
73
|
+
for (int i = 0; i < samples.Length; i++) {
|
|
74
|
+
short intSample = (short)Math.Clamp(samples[i] * short.MaxValue, short.MinValue, short.MaxValue);
|
|
75
|
+
buffer[i * 2] = (byte)(intSample & 0xff);
|
|
76
|
+
buffer[i * 2 + 1] = (byte)((intSample >> 8) & 0xff);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
fileStream.Write(buffer, 0, buffer.Length);
|
|
80
|
+
totalSampleCount += samples.Length;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
void WriteWavHeader() {
|
|
84
|
+
var writer = new BinaryWriter(fileStream, Encoding.ASCII, true);
|
|
85
|
+
|
|
86
|
+
writer.Write(Encoding.ASCII.GetBytes("RIFF"));
|
|
87
|
+
writer.Write(0); // placeholder for file size
|
|
88
|
+
writer.Write(Encoding.ASCII.GetBytes("WAVE"));
|
|
89
|
+
|
|
90
|
+
// fmt chunk
|
|
91
|
+
writer.Write(Encoding.ASCII.GetBytes("fmt "));
|
|
92
|
+
writer.Write(16); // PCM header size
|
|
93
|
+
writer.Write((short)1); // PCM format
|
|
94
|
+
writer.Write(channels);
|
|
95
|
+
writer.Write(sampleRate);
|
|
96
|
+
int byteRate = sampleRate * channels * bitsPerSample / 8;
|
|
97
|
+
writer.Write(byteRate);
|
|
98
|
+
short blockAlign = (short)(channels * bitsPerSample / 8);
|
|
99
|
+
writer.Write(blockAlign);
|
|
100
|
+
writer.Write(bitsPerSample);
|
|
101
|
+
|
|
102
|
+
// data chunk
|
|
103
|
+
writer.Write(Encoding.ASCII.GetBytes("data"));
|
|
104
|
+
dataChunkPos = fileStream.Position;
|
|
105
|
+
writer.Write(0); // placeholder for data chunk size
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
public void Dispose() {
|
|
109
|
+
if (!isInitialized) return;
|
|
110
|
+
|
|
111
|
+
long dataSize = totalSampleCount * bitsPerSample / 8;
|
|
112
|
+
|
|
113
|
+
fileStream.Seek(dataChunkPos, SeekOrigin.Begin);
|
|
114
|
+
fileStream.Write(BitConverter.GetBytes((int)dataSize), 0, 4);
|
|
115
|
+
|
|
116
|
+
fileStream.Seek(4, SeekOrigin.Begin);
|
|
117
|
+
int fileSize = (int)(fileStream.Length - 8);
|
|
118
|
+
fileStream.Write(BitConverter.GetBytes(fileSize), 0, 4);
|
|
119
|
+
|
|
120
|
+
fileStream.Dispose();
|
|
121
|
+
isInitialized = false;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|