Skip to content

Commit 0fc7c82

Browse files
committed
Modified a few for Example1
1 parent e80633b commit 0fc7c82

File tree

4 files changed

+82
-81
lines changed

4 files changed

+82
-81
lines changed

Example1/AudioMessage.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ namespace Example1
44
{
55
internal class AudioMessage
66
{
7-
public uint user_id;
8-
public byte ch_num;
9-
public uint buffer_length;
10-
public float [,] buffer_array;
7+
public uint user_id;
8+
public byte ch_num;
9+
public uint buffer_length;
10+
public float[,] buffer_array;
1111
}
1212
}

Example1/AudioStreamer.cs

Lines changed: 72 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using System;
44
using System.Collections;
55
using System.Collections.Generic;
6-
using System.IO;
76
using System.Text;
87
using System.Threading;
98
using WebSocketSharp;
@@ -31,65 +30,6 @@ public AudioStreamer (string url)
3130
configure ();
3231
}
3332

34-
private AudioMessage acceptBinaryMessage (byte [] data)
35-
{
36-
var id = data.SubArray (0, 4).To<uint> (ByteOrder.Big);
37-
var chNum = data.SubArray (4, 1) [0];
38-
var bufferLength = data.SubArray (5, 4).To<uint> (ByteOrder.Big);
39-
var bufferArray = new float [chNum, bufferLength];
40-
41-
var offset = 9;
42-
((int) chNum).Times (
43-
i => bufferLength.Times (
44-
j => {
45-
bufferArray [i, j] = data.SubArray (offset, 4).To<float> (ByteOrder.Big);
46-
offset += 4;
47-
}));
48-
49-
return new AudioMessage {
50-
user_id = id,
51-
ch_num = chNum,
52-
buffer_length = bufferLength,
53-
buffer_array = bufferArray
54-
};
55-
}
56-
57-
private NotificationMessage acceptTextMessage (string data)
58-
{
59-
var json = JObject.Parse (data);
60-
var id = (uint) json ["user_id"];
61-
var name = (string) json ["name"];
62-
var type = (string) json ["type"];
63-
64-
string message;
65-
if (type == "message")
66-
message = String.Format ("{0}: {1}", name, (string) json ["message"]);
67-
else if (type == "start_music")
68-
message = String.Format ("{0}: Started playing music!", name);
69-
else if (type == "connection") {
70-
var users = (JArray) json ["message"];
71-
var msg = new StringBuilder ("Now keeping connections:");
72-
foreach (JToken user in users)
73-
msg.AppendFormat (
74-
"\n- user_id: {0} name: {1}", (uint) user ["user_id"], (string) user ["name"]);
75-
76-
message = msg.ToString ();
77-
}
78-
else if (type == "connected") {
79-
_id = id;
80-
_heartbeatTimer.Change (30000, 30000);
81-
message = String.Format ("user_id: {0} name: {1}", id, name);
82-
}
83-
else
84-
message = "Received unknown type message.";
85-
86-
return new NotificationMessage {
87-
Summary = String.Format ("AudioStreamer ({0})", type),
88-
Body = message,
89-
Icon = "notification-message-im"
90-
};
91-
}
92-
9333
private void configure ()
9434
{
9535
#if DEBUG
@@ -99,15 +39,16 @@ private void configure ()
9939
_websocket.Send (createTextMessage ("connection", String.Empty));
10040

10141
_websocket.OnMessage += (sender, e) => {
102-
if (e.Type == Opcode.Text)
103-
_notifier.Notify (acceptTextMessage (e.Data));
42+
if (e.Type == Opcode.Text) {
43+
_notifier.Notify (convertTextMessage (e.Data));
44+
}
10445
else {
105-
var msg = acceptBinaryMessage (e.RawData);
46+
var msg = convertBinaryMessage (e.RawData);
10647
if (msg.user_id == _id)
10748
return;
10849

10950
if (_audioBox.ContainsKey (msg.user_id)) {
110-
_audioBox [msg.user_id].Enqueue (msg.buffer_array);
51+
_audioBox[msg.user_id].Enqueue (msg.buffer_array);
11152
return;
11253
}
11354

@@ -134,21 +75,83 @@ private void configure ()
13475
});
13576
}
13677

137-
private byte [] createAudioMessage (float [,] bufferArray)
78+
private AudioMessage convertBinaryMessage (byte[] data)
79+
{
80+
var id = data.SubArray (0, 4).To<uint> (ByteOrder.Big);
81+
var chNum = data.SubArray (4, 1)[0];
82+
var buffLen = data.SubArray (5, 4).To<uint> (ByteOrder.Big);
83+
var buffArr = new float[chNum, buffLen];
84+
85+
var offset = 9;
86+
((int) chNum).Times (
87+
i => buffLen.Times (
88+
j => {
89+
buffArr[i, j] = data.SubArray (offset, 4).To<float> (ByteOrder.Big);
90+
offset += 4;
91+
}));
92+
93+
return new AudioMessage {
94+
user_id = id,
95+
ch_num = chNum,
96+
buffer_length = buffLen,
97+
buffer_array = buffArr
98+
};
99+
}
100+
101+
private NotificationMessage convertTextMessage (string data)
102+
{
103+
var json = JObject.Parse (data);
104+
var id = (uint) json["user_id"];
105+
var name = (string) json["name"];
106+
var type = (string) json["type"];
107+
108+
string body;
109+
if (type == "message") {
110+
body = String.Format ("{0}: {1}", name, (string) json["message"]);
111+
}
112+
else if (type == "start_music") {
113+
body = String.Format ("{0}: Started playing music!", name);
114+
}
115+
else if (type == "connection") {
116+
var users = (JArray) json["message"];
117+
var buff = new StringBuilder ("Now keeping connections:");
118+
foreach (JToken user in users)
119+
buff.AppendFormat (
120+
"\n- user_id: {0} name: {1}", (uint) user["user_id"], (string) user["name"]);
121+
122+
body = buff.ToString ();
123+
}
124+
else if (type == "connected") {
125+
_id = id;
126+
_heartbeatTimer.Change (30000, 30000);
127+
body = String.Format ("user_id: {0} name: {1}", id, name);
128+
}
129+
else {
130+
body = "Received unknown type message.";
131+
}
132+
133+
return new NotificationMessage {
134+
Summary = String.Format ("AudioStreamer ({0})", type),
135+
Body = body,
136+
Icon = "notification-message-im"
137+
};
138+
}
139+
140+
private byte[] createBinaryMessage (float[,] bufferArray)
138141
{
139142
var msg = new List<byte> ();
140143

141144
var id = (uint) _id;
142145
var chNum = bufferArray.GetLength (0);
143-
var bufferLength = bufferArray.GetLength (1);
146+
var buffLen = bufferArray.GetLength (1);
144147

145148
msg.AddRange (id.ToByteArray (ByteOrder.Big));
146149
msg.Add ((byte) chNum);
147-
msg.AddRange (((uint) bufferLength).ToByteArray (ByteOrder.Big));
150+
msg.AddRange (((uint) buffLen).ToByteArray (ByteOrder.Big));
148151

149152
chNum.Times (
150-
i => bufferLength.Times (
151-
j => msg.AddRange (bufferArray [i, j].ToByteArray (ByteOrder.Big))));
153+
i => buffLen.Times (
154+
j => msg.AddRange (bufferArray[i, j].ToByteArray (ByteOrder.Big))));
152155

153156
return msg.ToArray ();
154157
}

Example1/Notifier.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ public Notifier ()
3535
Console.WriteLine (msg);
3636
#endif
3737
}
38-
else
38+
else {
3939
Thread.Sleep (500);
40+
}
4041
}
4142

4243
_waitHandle.Set ();
@@ -45,19 +46,17 @@ public Notifier ()
4546

4647
public int Count {
4748
get {
48-
lock (_sync) {
49+
lock (_sync)
4950
return _queue.Count;
50-
}
5151
}
5252
}
5353

5454
private NotificationMessage dequeue ()
5555
{
56-
lock (_sync) {
56+
lock (_sync)
5757
return _queue.Count > 0
5858
? _queue.Dequeue ()
5959
: null;
60-
}
6160
}
6261

6362
public void Close ()
@@ -69,10 +68,9 @@ public void Close ()
6968

7069
public void Notify (NotificationMessage message)
7170
{
72-
lock (_sync) {
71+
lock (_sync)
7372
if (_enabled)
7473
_queue.Enqueue (message);
75-
}
7674
}
7775

7876
void IDisposable.Dispose ()

Example1/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace Example1
55
{
66
public class Program
77
{
8-
public static void Main (string [] args)
8+
public static void Main (string[] args)
99
{
1010
using (var streamer = new AudioStreamer ("ws://agektmr.node-ninja.com:3000/socket"))
1111
//using (var streamer = new AudioStreamer ("ws://localhost:3000/socket"))

0 commit comments

Comments
 (0)