Skip to content

Commit c297e15

Browse files
committed
Change routine to deliver back PCM rather than creating a wav and directly playing
1 parent 80b127f commit c297e15

File tree

4 files changed

+240
-22
lines changed

4 files changed

+240
-22
lines changed

Assets/Libraries/eSpeakWrapper/Client.cs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,20 +59,46 @@ enum SpeechFlags
5959

6060
static bool Initialized = false;
6161

62+
public static int sampleRate;
63+
6264
public static void Initialize(string path)
6365
{
64-
var result = espeak_Initialize(AudioOutput.SynchronousPlayback, 0, path, 0);
65-
//var result = espeak_Initialize(AudioOutput.Retrieval, 0, path, 0); // TODO allow receiving audio data..
66+
//var result = espeak_Initialize(AudioOutput.SynchronousPlayback, 0, path, 0);
67+
var result = espeak_Initialize(AudioOutput.Retrieval, 0, path, 0); // TODO allow receiving audio data..
6668
if (result == (int)Error.EE_INTERNAL_ERROR)
6769
{
6870
throw new Exception(string.Format("Could not initialize ESpeak. Maybe there is no espeak data at {0}?", path));
71+
} else {
72+
sampleRate = result;
6973
}
7074

7175
espeak_SetSynthCallback(EventHandler.Handle);
7276

7377
Initialized = true;
7478
}
7579

80+
public static bool VoiceFinished() {
81+
bool ret = false;
82+
EventHandler.audio_files_mutex.WaitOne();
83+
if(EventHandler.audio_files.Count > 0) {
84+
ret = true;
85+
}
86+
EventHandler.audio_files_mutex.ReleaseMutex();
87+
return ret;
88+
}
89+
90+
public static byte[] PopVoice() {
91+
byte[] ret = null;
92+
EventHandler.audio_files_mutex.WaitOne();
93+
if(EventHandler.audio_files.Count > 0) {
94+
ret = EventHandler.audio_files[0];
95+
EventHandler.audio_files.RemoveAt(0);
96+
}
97+
EventHandler.audio_files_mutex.ReleaseMutex();
98+
return ret;
99+
100+
}
101+
76102
public static bool SetRate(int rate)
77103
{
78104
if (rate < 80 && rate > 450)

Assets/Libraries/eSpeakWrapper/EventHandler.cs

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,20 @@
44
using System.Runtime.InteropServices;
55
using System.Collections.Generic;
66
using System.Text;
7+
using System.Threading;
78

89
namespace ESpeakWrapper
910
{
1011
class EventHandler
1112
{
12-
1313
public delegate int SynthCallback(IntPtr wavePtr, int bufferLength, IntPtr eventsPtr);
14+
public delegate void OnVoiceFinished();
15+
16+
public static OnVoiceFinished ovf = null;
1417

1518
static MemoryStream Stream;
19+
public static Mutex audio_files_mutex = new Mutex();
20+
public static List<byte[]> audio_files = new List<byte[]>();
1621

1722
public static int Handle(IntPtr wavePtr, int bufferLength, IntPtr eventsPtr)
1823
{
@@ -22,14 +27,20 @@ public static int Handle(IntPtr wavePtr, int bufferLength, IntPtr eventsPtr)
2227
// crash, but console log gets printed to output.log
2328
//Debug.Log("Buffer length is " + bufferLength);
2429

30+
//Assume that synthesiz is final if buffer length is zero?
2531
if (bufferLength == 0)
2632
{
2733
//var file = new FileStream("alarm01.wav", FileMode.Open);
2834
//Stream.Seek(0, SeekOrigin.Begin);
2935
//file.CopyTo(Stream);
3036

31-
PlayAudio();
32-
Console.Write(ConvertHeadersToString(Stream.GetBuffer()));
37+
//PlayAudio();
38+
//Console.Write(ConvertHeadersToString(Stream.GetBuffer()));
39+
40+
Stream.Flush();
41+
audio_files_mutex.WaitOne();
42+
audio_files.Add(Stream.ToArray());
43+
audio_files_mutex.ReleaseMutex();
3344
Stream.Dispose();
3445
return 0;
3546
}
@@ -76,7 +87,7 @@ static int WriteAudioToStream(IntPtr wavePtr, int bufferLength)
7687
if (Stream == null)
7788
{
7889
Stream = new MemoryStream();
79-
InitializeStream();
90+
//InitializeStream();
8091
}
8192

8293
byte[] audio = new byte[bufferLength * 2];
@@ -86,6 +97,7 @@ static int WriteAudioToStream(IntPtr wavePtr, int bufferLength)
8697
return 0;
8798
}
8899

100+
/*
89101
static void InitializeStream()
90102
{
91103
var ascii = Encoding.ASCII;
@@ -115,7 +127,9 @@ static void InitializeStream()
115127
// audio size: will fill this in the PlayAudio() method
116128
Stream.Write(BitConverter.GetBytes(0), 0, 4);
117129
}
130+
*/
118131

132+
/*
119133
static void PlayAudio()
120134
{
121135
Stream.Seek(4, SeekOrigin.Begin);
@@ -125,23 +139,28 @@ static void PlayAudio()
125139
Stream.Write(BitConverter.GetBytes(Stream.Length - 44), 0, 4);
126140
127141
Stream.Seek(0, SeekOrigin.Begin); // have to do this, otherwise the player will give a bogus error
128-
var player = new SoundPlayer(Stream);
142+
//var player = new SoundPlayer(Stream);
129143
130-
try
131-
{
132-
player.Play();
133-
}
134-
catch (Exception e)
135-
{
136-
Console.WriteLine(e);
137-
}
144+
//try
145+
//{
146+
// player.Play();
147+
//}
148+
//catch (Exception e)
149+
//{
150+
// Console.WriteLine(e);
151+
//}
138152
139153
// NOTE this gets always generated.. we dont need?
140-
using (var file = new FileStream("test.wav", FileMode.Create))
141-
{
142-
Stream.WriteTo(file);
154+
//using (var file = new FileStream("test.wav", FileMode.Create))
155+
//{
156+
// Stream.WriteTo(file);
157+
//}
158+
159+
if(ovf != null) {
160+
ovf();
143161
}
144162
}
163+
*/
145164

146165
static string PrintBytes(byte[] byteArray)
147166
{

Assets/Scenes/espeak.unity

Lines changed: 141 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,15 @@ LightmapSettings:
5454
m_EnableBakedLightmaps: 0
5555
m_EnableRealtimeLightmaps: 0
5656
m_LightmapEditorSettings:
57-
serializedVersion: 10
57+
serializedVersion: 12
5858
m_Resolution: 2
5959
m_BakeResolution: 10
6060
m_AtlasSize: 512
6161
m_AO: 0
6262
m_AOMaxDistance: 1
6363
m_CompAOExponent: 1
6464
m_CompAOExponentDirect: 0
65+
m_ExtractAmbientOcclusion: 0
6566
m_Padding: 2
6667
m_LightmapParameters: {fileID: 0}
6768
m_LightmapsBakeMode: 1
@@ -76,10 +77,16 @@ LightmapSettings:
7677
m_PVRDirectSampleCount: 32
7778
m_PVRSampleCount: 256
7879
m_PVRBounces: 2
80+
m_PVREnvironmentSampleCount: 256
81+
m_PVREnvironmentReferencePointCount: 2048
82+
m_PVRFilteringMode: 2
83+
m_PVRDenoiserTypeDirect: 0
84+
m_PVRDenoiserTypeIndirect: 0
85+
m_PVRDenoiserTypeAO: 0
7986
m_PVRFilterTypeDirect: 0
8087
m_PVRFilterTypeIndirect: 0
8188
m_PVRFilterTypeAO: 0
82-
m_PVRFilteringMode: 1
89+
m_PVREnvironmentMIS: 0
8390
m_PVRCulling: 0
8491
m_PVRFilteringGaussRadiusDirect: 1
8592
m_PVRFilteringGaussRadiusIndirect: 5
@@ -88,6 +95,7 @@ LightmapSettings:
8895
m_PVRFilteringAtrousPositionSigmaIndirect: 2
8996
m_PVRFilteringAtrousPositionSigmaAO: 1
9097
m_ShowResolutionOverlay: 1
98+
m_ExportTrainingData: 0
9199
m_LightingDataAsset: {fileID: 0}
92100
m_UseShadowmask: 1
93101
--- !u!196 &4
@@ -150,9 +158,10 @@ Camera:
150158
m_ClearFlags: 2
151159
m_BackGroundColor: {r: 0.19215687, g: 0.3019608, b: 0.4745098, a: 0}
152160
m_projectionMatrixMode: 1
161+
m_GateFitMode: 2
162+
m_FOVAxisMode: 0
153163
m_SensorSize: {x: 36, y: 24}
154164
m_LensShift: {x: 0, y: 0}
155-
m_GateFitMode: 2
156165
m_FocalLength: 50
157166
m_NormalizedViewPortRect:
158167
serializedVersion: 2
@@ -223,7 +232,7 @@ MonoBehaviour:
223232
m_Script: {fileID: 11500000, guid: 0896d1324347bd349886948e2acc0cbe, type: 3}
224233
m_Name:
225234
m_EditorClassIdentifier:
226-
sayAtStart: Welcome!
235+
sayAtStart: Testing
227236
--- !u!4 &548749181
228237
Transform:
229238
m_ObjectHideFlags: 0
@@ -238,6 +247,133 @@ Transform:
238247
m_Father: {fileID: 0}
239248
m_RootOrder: 2
240249
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
250+
--- !u!1 &1440771956
251+
GameObject:
252+
m_ObjectHideFlags: 0
253+
m_CorrespondingSourceObject: {fileID: 0}
254+
m_PrefabInstance: {fileID: 0}
255+
m_PrefabAsset: {fileID: 0}
256+
serializedVersion: 6
257+
m_Component:
258+
- component: {fileID: 1440771958}
259+
- component: {fileID: 1440771957}
260+
m_Layer: 0
261+
m_Name: Audio Source
262+
m_TagString: Untagged
263+
m_Icon: {fileID: 0}
264+
m_NavMeshLayer: 0
265+
m_StaticEditorFlags: 0
266+
m_IsActive: 1
267+
--- !u!82 &1440771957
268+
AudioSource:
269+
m_ObjectHideFlags: 0
270+
m_CorrespondingSourceObject: {fileID: 0}
271+
m_PrefabInstance: {fileID: 0}
272+
m_PrefabAsset: {fileID: 0}
273+
m_GameObject: {fileID: 1440771956}
274+
m_Enabled: 1
275+
serializedVersion: 4
276+
OutputAudioMixerGroup: {fileID: 0}
277+
m_audioClip: {fileID: 0}
278+
m_PlayOnAwake: 1
279+
m_Volume: 1
280+
m_Pitch: 1
281+
Loop: 0
282+
Mute: 0
283+
Spatialize: 0
284+
SpatializePostEffects: 0
285+
Priority: 128
286+
DopplerLevel: 1
287+
MinDistance: 1
288+
MaxDistance: 500
289+
Pan2D: 0
290+
rolloffMode: 0
291+
BypassEffects: 0
292+
BypassListenerEffects: 0
293+
BypassReverbZones: 0
294+
rolloffCustomCurve:
295+
serializedVersion: 2
296+
m_Curve:
297+
- serializedVersion: 3
298+
time: 0
299+
value: 1
300+
inSlope: 0
301+
outSlope: 0
302+
tangentMode: 0
303+
weightedMode: 0
304+
inWeight: 0.33333334
305+
outWeight: 0.33333334
306+
- serializedVersion: 3
307+
time: 1
308+
value: 0
309+
inSlope: 0
310+
outSlope: 0
311+
tangentMode: 0
312+
weightedMode: 0
313+
inWeight: 0.33333334
314+
outWeight: 0.33333334
315+
m_PreInfinity: 2
316+
m_PostInfinity: 2
317+
m_RotationOrder: 4
318+
panLevelCustomCurve:
319+
serializedVersion: 2
320+
m_Curve:
321+
- serializedVersion: 3
322+
time: 0
323+
value: 0
324+
inSlope: 0
325+
outSlope: 0
326+
tangentMode: 0
327+
weightedMode: 0
328+
inWeight: 0.33333334
329+
outWeight: 0.33333334
330+
m_PreInfinity: 2
331+
m_PostInfinity: 2
332+
m_RotationOrder: 4
333+
spreadCustomCurve:
334+
serializedVersion: 2
335+
m_Curve:
336+
- serializedVersion: 3
337+
time: 0
338+
value: 0
339+
inSlope: 0
340+
outSlope: 0
341+
tangentMode: 0
342+
weightedMode: 0
343+
inWeight: 0.33333334
344+
outWeight: 0.33333334
345+
m_PreInfinity: 2
346+
m_PostInfinity: 2
347+
m_RotationOrder: 4
348+
reverbZoneMixCustomCurve:
349+
serializedVersion: 2
350+
m_Curve:
351+
- serializedVersion: 3
352+
time: 0
353+
value: 1
354+
inSlope: 0
355+
outSlope: 0
356+
tangentMode: 0
357+
weightedMode: 0
358+
inWeight: 0.33333334
359+
outWeight: 0.33333334
360+
m_PreInfinity: 2
361+
m_PostInfinity: 2
362+
m_RotationOrder: 4
363+
--- !u!4 &1440771958
364+
Transform:
365+
m_ObjectHideFlags: 0
366+
m_CorrespondingSourceObject: {fileID: 0}
367+
m_PrefabInstance: {fileID: 0}
368+
m_PrefabAsset: {fileID: 0}
369+
m_GameObject: {fileID: 1440771956}
370+
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
371+
m_LocalPosition: {x: -2.093978, y: 2.580687, z: 1.2901945}
372+
m_LocalScale: {x: 1, y: 1, z: 1}
373+
m_Children: []
374+
m_Father: {fileID: 0}
375+
m_RootOrder: 3
376+
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
241377
--- !u!1 &1665498520
242378
GameObject:
243379
m_ObjectHideFlags: 0
@@ -292,6 +428,7 @@ MonoBehaviour:
292428
m_Name:
293429
m_EditorClassIdentifier:
294430
voiceID: Tweaky
431+
audio_source: {fileID: 1440771957}
295432
--- !u!4 &1665498523
296433
Transform:
297434
m_ObjectHideFlags: 0

0 commit comments

Comments
 (0)