Skip to content

Commit 797205c

Browse files
committed
2 parents e89ee07 + 062e434 commit 797205c

File tree

8 files changed

+487
-55
lines changed

8 files changed

+487
-55
lines changed

Assets/Libraries/eSpeakWrapper/Client.cs

Lines changed: 82 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,28 +59,107 @@ 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)
79105
{
80106
throw new Exception("The rate must be between 80 and 450.");
81107
}
82108

83-
var result = espeak_SetParameter(Parameter.Rate, rate, ParameterType.Absolute);
109+
Error result = espeak_SetParameter(Parameter.Rate, rate, ParameterType.Absolute);
110+
return CheckResult(result);
111+
}
112+
113+
public static bool SetRange(int range) {
114+
if(range < 0) {
115+
range = 0;
116+
}
117+
if(range > 100) {
118+
range = 100;
119+
}
120+
121+
Error result = espeak_SetParameter(Parameter.Range, range, ParameterType.Absolute);
122+
return CheckResult(result);
123+
}
124+
125+
public static bool SetPitch(int pitch) {
126+
if(pitch < 0) {
127+
pitch = 0;
128+
}
129+
if(pitch > 100) {
130+
pitch = 100;
131+
}
132+
133+
Error result = espeak_SetParameter(Parameter.Pitch, pitch, ParameterType.Absolute);
134+
return CheckResult(result);
135+
}
136+
137+
public static bool SetWordgap(int wordgap) {
138+
if(wordgap < 0) {
139+
wordgap = 0;
140+
}
141+
Error result = espeak_SetParameter(Parameter.WordGap, wordgap, ParameterType.Absolute);
142+
return CheckResult(result);
143+
}
144+
145+
public static bool SetVolume(int volume) {
146+
if(volume < 0) {
147+
volume = 0;
148+
}
149+
if(volume > 200) {
150+
volume = 200;
151+
}
152+
Error result = espeak_SetParameter(Parameter.Volume, volume, ParameterType.Absolute);
153+
return CheckResult(result);
154+
}
155+
156+
public static bool SetIntonation(int intonation) {
157+
Error result = espeak_SetParameter(Parameter.Intonation, intonation, ParameterType.Absolute);
158+
return CheckResult(result);
159+
}
160+
161+
public static bool SetCapitals(int capitals) {
162+
Error result = espeak_SetParameter(Parameter.Capitals, capitals, ParameterType.Absolute);
84163
return CheckResult(result);
85164
}
86165

Assets/Libraries/eSpeakWrapper/EventHandler.cs

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,51 @@
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
{
19-
Console.WriteLine("Received event!");
20-
Console.WriteLine("Buffer length is " + bufferLength);
24+
//Console.WriteLine("Received event!");
25+
//Console.WriteLine("Buffer length is " + bufferLength);
2126

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()));
33-
Stream.Dispose();
37+
//PlayAudio();
38+
//Console.Write(ConvertHeadersToString(Stream.GetBuffer()));
39+
if(Stream != null) {
40+
Stream.Flush();
41+
audio_files_mutex.WaitOne();
42+
audio_files.Add(Stream.ToArray());
43+
audio_files_mutex.ReleaseMutex();
44+
Stream.Dispose();
45+
Stream = null;
46+
}
3447
return 0;
48+
} else {
49+
WriteAudioToStream(wavePtr, bufferLength);
3550
}
3651

37-
WriteAudioToStream(wavePtr, bufferLength);
38-
3952
var events = MarshalEvents(eventsPtr);
4053

4154
foreach (Event anEvent in events)
@@ -76,7 +89,7 @@ static int WriteAudioToStream(IntPtr wavePtr, int bufferLength)
7689
if (Stream == null)
7790
{
7891
Stream = new MemoryStream();
79-
InitializeStream();
92+
//InitializeStream();
8093
}
8194

8295
byte[] audio = new byte[bufferLength * 2];
@@ -86,6 +99,7 @@ static int WriteAudioToStream(IntPtr wavePtr, int bufferLength)
8699
return 0;
87100
}
88101

102+
/*
89103
static void InitializeStream()
90104
{
91105
var ascii = Encoding.ASCII;
@@ -115,7 +129,9 @@ static void InitializeStream()
115129
// audio size: will fill this in the PlayAudio() method
116130
Stream.Write(BitConverter.GetBytes(0), 0, 4);
117131
}
132+
*/
118133

134+
/*
119135
static void PlayAudio()
120136
{
121137
Stream.Seek(4, SeekOrigin.Begin);
@@ -125,23 +141,28 @@ static void PlayAudio()
125141
Stream.Write(BitConverter.GetBytes(Stream.Length - 44), 0, 4);
126142
127143
Stream.Seek(0, SeekOrigin.Begin); // have to do this, otherwise the player will give a bogus error
128-
var player = new SoundPlayer(Stream);
144+
//var player = new SoundPlayer(Stream);
129145
130-
try
131-
{
132-
player.Play();
133-
}
134-
catch (Exception e)
135-
{
136-
Console.WriteLine(e);
137-
}
146+
//try
147+
//{
148+
// player.Play();
149+
//}
150+
//catch (Exception e)
151+
//{
152+
// Console.WriteLine(e);
153+
//}
138154
139155
// NOTE this gets always generated.. we dont need?
140-
using (var file = new FileStream("test.wav", FileMode.Create))
141-
{
142-
Stream.WriteTo(file);
156+
//using (var file = new FileStream("test.wav", FileMode.Create))
157+
//{
158+
// Stream.WriteTo(file);
159+
//}
160+
161+
if(ovf != null) {
162+
ovf();
143163
}
144164
}
165+
*/
145166

146167
static string PrintBytes(byte[] byteArray)
147168
{

0 commit comments

Comments
 (0)