@@ -109,7 +109,8 @@ void NetworkEvents::restartConnection()
109109
110110void NetworkEvents::createEventChannels ()
111111{
112- EventChannel* chan = new EventChannel (EventChannel::TEXT, 1 , MAX_MESSAGE_LENGTH, CoreServices::getGlobalSampleRate (), this );
112+ juce::int64 timestamp = CoreServices::getGlobalSampleRate ();
113+ EventChannel* chan = new EventChannel (EventChannel::TEXT, 1 , MAX_MESSAGE_LENGTH, timestamp, this );
113114 chan->setName (" Network messages" );
114115 chan->setDescription (" Messages received through the network events module" );
115116 chan->setIdentifier (" external.network.rawData" );
@@ -118,10 +119,10 @@ void NetworkEvents::createEventChannels()
118119 eventChannelArray.add (chan);
119120 messageChannel = chan;
120121
121- EventChannel* TTLchan = new EventChannel (EventChannel::TTL, 8 , 1 , CoreServices::getGlobalSampleRate () , this );
122+ EventChannel* TTLchan = new EventChannel (EventChannel::TTL, 8 , 1 , timestamp , this );
122123 TTLchan->setName (" Network Events output" );
123124 TTLchan->setDescription (" Triggers whenever \" TTL\" is received on the port." );
124- TTLchan->setIdentifier (" network.ttl.event " );
125+ TTLchan->setIdentifier (" external. network.ttl" );
125126 eventChannelArray.add (TTLchan);
126127 TTLChannel = TTLchan;
127128}
@@ -135,11 +136,11 @@ AudioProcessorEditor* NetworkEvents::createEditor ()
135136}
136137
137138
138- void NetworkEvents::postTimestamppedStringToMidiBuffer (const StringTS& s)
139+ void NetworkEvents::postTimestamppedStringToMidiBuffer (const StringTS& s, juce::int64 timestamp )
139140{
140141 MetaDataValueArray md;
141142 md.add (new MetaDataValue (MetaDataDescriptor::INT64, 1 , &s.timestamp ));
142- TextEventPtr event = TextEvent::createTextEvent (messageChannel, CoreServices::getGlobalTimestamp () , s.str , md);
143+ TextEventPtr event = TextEvent::createTextEvent (messageChannel, timestamp , s.str , md);
143144 addEvent (messageChannel, event, 0 );
144145}
145146
@@ -245,44 +246,50 @@ String NetworkEvents::handleSpecialMessages(const String& s)
245246 status += CoreServices::RecordNode::getExperimentNumber ();
246247 return status;
247248 }
248- else if (cmd.compareIgnoreCase (" TTL" ) == 0 )
249+ else if (cmd.compareIgnoreCase (" TTL" ) == 0 )
249250 {
250251 // Default to chan 0 and off
251- int currEventChannel = 0 ;
252+ int channel = 0 ;
252253 bool onOff = false ;
253254 /* * First set optional parameters (name/value pairs)*/
254- if (s.contains (" =" ))
255+ String params = s.substring (cmd.length ()).trim ();
256+ StringPairArray dict = parseNetworkMessage (params);
257+
258+ StringArray keys = dict.getAllKeys ();
259+ for (int i = 0 ; i < keys.size (); ++i)
255260 {
256- String params = s. substring (cmd. length ()). trim () ;
257- StringPairArray dict = parseNetworkMessage (params );
261+ String key = keys[i] ;
262+ int value = dict[key]. getIntValue ( );
258263
259- StringArray keys = dict.getAllKeys ();
260- for (int i = 0 ; i < keys.size (); ++i)
264+ if (key.compareIgnoreCase (" Channel" ) == 0 )
261265 {
262- String key = keys[i];
263- String value = dict[key];
264-
265- if (key.compareIgnoreCase (" EventChannel" ) == 0 )
266+ // Make sure in range
267+ if (value <= 8 && value >= 0 )
266268 {
267- currEventChannel = value. getIntValue () ;
269+ channel = value;
268270 }
269- else if (key. compareIgnoreCase ( " onOff " ) == 0 )
271+ else
270272 {
271- if (value.compareIgnoreCase (" on" ) == 0 )
272- {
273- onOff = true ;
274- }
275- else
276- {
277- onOff = false ;
278- }
273+ // Throw some kind of error here
279274 }
280275 }
276+ else if (key.compareIgnoreCase (" on" ) == 0 )
281277 {
282- ScopedLock TTLlock (TTLqueueLock);
283- TTLQueue.push ({ onOff, currEventChannel });
278+ onOff = value;
284279 }
285280 }
281+ {
282+ ScopedLock TTLlock (TTLqueueLock);
283+ if (CoreServices::getAcquisitionStatus ())
284+ {
285+ TTLQueue.push ({ onOff, channel });
286+ }
287+ else
288+ {
289+ // Throw some error
290+ }
291+ }
292+
286293
287294
288295 return " TTL Handled" ;
@@ -291,45 +298,36 @@ String NetworkEvents::handleSpecialMessages(const String& s)
291298 return String (" NotHandled" );
292299}
293300
294- void NetworkEvents::triggerEvent (StringTTL TTLmsg)
301+ void NetworkEvents::triggerEvent (StringTTL TTLmsg, juce::int64 timestamp )
295302{
296- if (TTLmsg.onOff )
297- {
298- juce::uint8 ttlDataOn = 1 << TTLmsg.eventChannel ;
299- int currEventChannel = TTLmsg.eventChannel ;
300- TTLEventPtr eventOn = TTLEvent::createTTLEvent (TTLChannel, CoreServices::getGlobalTimestamp (), &ttlDataOn, sizeof (juce::uint8), currEventChannel);
301- std::cout << " adding true event from ne" << std::endl;
302- addEvent (TTLChannel, eventOn, 0 );
303- }
304- else
305- {
306- juce::uint8 ttlDataOff = 0 ;
307- int currEventChannel = TTLmsg.eventChannel ;
308- TTLEventPtr eventOff = TTLEvent::createTTLEvent (TTLChannel, CoreServices::getGlobalTimestamp (), &ttlDataOff, sizeof (juce::uint8), currEventChannel);
309- std::cout << " adding false event from ne" << std::endl;
310- addEvent (TTLChannel, eventOff, 0 );
311- }
303+ juce::uint8 ttlData = TTLmsg.onOff << TTLmsg.eventChannel ;
304+ TTLEventPtr event = TTLEvent::createTTLEvent (TTLChannel, timestamp, &ttlData, sizeof (juce::uint8), TTLmsg.eventChannel );
305+ std::cout << " adding event from ne" << std::endl;
306+ addEvent (TTLChannel, event, 0 );
312307}
313308
314309
315310void NetworkEvents::process (AudioSampleBuffer& buffer)
316311{
317312 setTimestampAndSamples (CoreServices::getGlobalTimestamp (),0 );
313+ juce::int64 timestamp = CoreServices::getGlobalTimestamp ();
318314
319315 ScopedLock lock (queueLock);
320316 while (! networkMessagesQueue.empty ())
321317 {
322318 const StringTS& msg = networkMessagesQueue.front ();
323- postTimestamppedStringToMidiBuffer (msg);
319+ postTimestamppedStringToMidiBuffer (msg, timestamp );
324320 networkMessagesQueue.pop ();
325321 }
326322
327- ScopedLock TTLlock (TTLqueueLock);
328- while (!TTLQueue.empty ())
329323 {
330- const StringTTL& TTLmsg = TTLQueue.front ();
331- triggerEvent (TTLmsg);
332- TTLQueue.pop ();
324+ ScopedLock TTLlock (TTLqueueLock);
325+ while (!TTLQueue.empty ())
326+ {
327+ const StringTTL& TTLmsg = TTLQueue.front ();
328+ triggerEvent (TTLmsg, timestamp);
329+ TTLQueue.pop ();
330+ }
333331 }
334332}
335333
0 commit comments