Jack2  1.9.8
JackNetTool.cpp
1 /*
2 Copyright (C) 2008-2011 Romain Moret at Grame
3 
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 
18 */
19 
20 #include "JackNetTool.h"
21 
22 #ifdef __APPLE__
23 
24 #include <mach/mach_time.h>
25 
26 class HardwareClock
27 {
28  public:
29 
30  HardwareClock();
31 
32  void Reset();
33  void Update();
34 
35  float GetDeltaTime() const;
36  double GetTime() const;
37 
38  private:
39 
40  double m_clockToSeconds;
41 
42  uint64_t m_startAbsTime;
43  uint64_t m_lastAbsTime;
44 
45  double m_time;
46  float m_deltaTime;
47 };
48 
49 HardwareClock::HardwareClock()
50 {
51  mach_timebase_info_data_t info;
52  mach_timebase_info(&info);
53  m_clockToSeconds = (double)info.numer/info.denom/1000000000.0;
54  Reset();
55 }
56 
57 void HardwareClock::Reset()
58 {
59  m_startAbsTime = mach_absolute_time();
60  m_lastAbsTime = m_startAbsTime;
61  m_time = m_startAbsTime*m_clockToSeconds;
62  m_deltaTime = 1.0f/60.0f;
63 }
64 
65 void HardwareClock::Update()
66 {
67  const uint64_t currentTime = mach_absolute_time();
68  const uint64_t dt = currentTime - m_lastAbsTime;
69 
70  m_time = currentTime*m_clockToSeconds;
71  m_deltaTime = (double)dt*m_clockToSeconds;
72  m_lastAbsTime = currentTime;
73 }
74 
75 float HardwareClock::GetDeltaTime() const
76 {
77  return m_deltaTime;
78 }
79 
80 double HardwareClock::GetTime() const
81 {
82  return m_time;
83 }
84 
85 #endif
86 
87 using namespace std;
88 
89 namespace Jack
90 {
91 // NetMidiBuffer**********************************************************************************
92 
93  NetMidiBuffer::NetMidiBuffer(session_params_t* params, uint32_t nports, char* net_buffer)
94  {
95  fNPorts = nports;
96  fMaxBufsize = fNPorts * sizeof(sample_t) * params->fPeriodSize ;
97  fMaxPcktSize = params->fMtu - sizeof(packet_header_t);
98  fBuffer = new char[fMaxBufsize];
99  fPortBuffer = new JackMidiBuffer* [fNPorts];
100  for (int port_index = 0; port_index < fNPorts; port_index++) {
101  fPortBuffer[port_index] = NULL;
102  }
103  fNetBuffer = net_buffer;
104 
105  fCycleBytesSize = params->fMtu
106  * (max(params->fSendMidiChannels, params->fReturnMidiChannels)
107  * params->fPeriodSize * sizeof(sample_t) / (params->fMtu - sizeof(packet_header_t)));
108  }
109 
110  NetMidiBuffer::~NetMidiBuffer()
111  {
112  delete[] fBuffer;
113  delete[] fPortBuffer;
114  }
115 
116  size_t NetMidiBuffer::GetCycleSize()
117  {
118  return fCycleBytesSize;
119  }
120 
121  int NetMidiBuffer::GetNumPackets(int data_size, int max_size)
122  {
123  int res1 = data_size % max_size;
124  int res2 = data_size / max_size;
125  return (res1) ? res2 + 1 : res2;
126  }
127 
128  void NetMidiBuffer::SetBuffer(int index, JackMidiBuffer* buffer)
129  {
130  fPortBuffer[index] = buffer;
131  }
132 
133  JackMidiBuffer* NetMidiBuffer::GetBuffer(int index)
134  {
135  return fPortBuffer[index];
136  }
137 
138  void NetMidiBuffer::DisplayEvents()
139  {
140  for (int port_index = 0; port_index < fNPorts; port_index++) {
141  for (uint event = 0; event < fPortBuffer[port_index]->event_count; event++) {
142  if (fPortBuffer[port_index]->IsValid()) {
143  jack_info("port %d : midi event %u/%u -> time : %u, size : %u",
144  port_index + 1, event + 1, fPortBuffer[port_index]->event_count,
145  fPortBuffer[port_index]->events[event].time, fPortBuffer[port_index]->events[event].size);
146  }
147  }
148  }
149  }
150 
151  int NetMidiBuffer::RenderFromJackPorts()
152  {
153  int pos = 0;
154  size_t copy_size;
155 
156  for (int port_index = 0; port_index < fNPorts; port_index++) {
157  char* write_pos = fBuffer + pos;
158  copy_size = sizeof(JackMidiBuffer) + fPortBuffer[port_index]->event_count * sizeof(JackMidiEvent);
159  memcpy(fBuffer + pos, fPortBuffer[port_index], copy_size);
160  pos += copy_size;
161  memcpy(fBuffer + pos,
162  fPortBuffer[port_index] + (fPortBuffer[port_index]->buffer_size - fPortBuffer[port_index]->write_pos),
163  fPortBuffer[port_index]->write_pos);
164  pos += fPortBuffer[port_index]->write_pos;
165 
166  JackMidiBuffer* midi_buffer = reinterpret_cast<JackMidiBuffer*>(write_pos);
167  MidiBufferHToN(midi_buffer, midi_buffer);
168  }
169  return pos;
170  }
171 
172  void NetMidiBuffer::RenderToJackPorts()
173  {
174  int pos = 0;
175  size_t copy_size;
176 
177  for (int port_index = 0; port_index < fNPorts; port_index++) {
178  JackMidiBuffer* midi_buffer = reinterpret_cast<JackMidiBuffer*>(fBuffer + pos);
179  MidiBufferNToH(midi_buffer, midi_buffer);
180  copy_size = sizeof(JackMidiBuffer) + reinterpret_cast<JackMidiBuffer*>(fBuffer + pos)->event_count * sizeof(JackMidiEvent);
181  memcpy(fPortBuffer[port_index], fBuffer + pos, copy_size);
182  pos += copy_size;
183  memcpy(fPortBuffer[port_index] + (fPortBuffer[port_index]->buffer_size - fPortBuffer[port_index]->write_pos),
184  fBuffer + pos,
185  fPortBuffer[port_index]->write_pos);
186  pos += fPortBuffer[port_index]->write_pos;
187  }
188  }
189 
190  void NetMidiBuffer::RenderFromNetwork(int sub_cycle, size_t copy_size)
191  {
192  memcpy(fBuffer + sub_cycle * fMaxPcktSize, fNetBuffer, copy_size);
193  }
194 
195  int NetMidiBuffer::RenderToNetwork(int sub_cycle, size_t total_size)
196  {
197  int size = total_size - sub_cycle * fMaxPcktSize;
198  int copy_size = (size <= fMaxPcktSize) ? size : fMaxPcktSize;
199  memcpy(fNetBuffer, fBuffer + sub_cycle * fMaxPcktSize, copy_size);
200  return copy_size;
201  }
202 
203 // net audio buffer *********************************************************************************
204 
205  NetAudioBuffer::NetAudioBuffer(session_params_t* params, uint32_t nports, char* net_buffer)
206  {
207  fNPorts = nports;
208  fNetBuffer = net_buffer;
209 
210  fPortBuffer = new sample_t* [fNPorts];
211  fConnectedPorts = new bool[fNPorts];
212  for (int port_index = 0; port_index < fNPorts; port_index++) {
213  fPortBuffer[port_index] = NULL;
214  fConnectedPorts[port_index] = true;
215  }
216  }
217 
218  NetAudioBuffer::~NetAudioBuffer()
219  {
220  delete [] fConnectedPorts;
221  delete [] fPortBuffer;
222  }
223 
224  void NetAudioBuffer::SetBuffer(int index, sample_t* buffer)
225  {
226  fPortBuffer[index] = buffer;
227  }
228 
229  sample_t* NetAudioBuffer::GetBuffer(int index)
230  {
231  return fPortBuffer[index];
232  }
233 
234  int NetAudioBuffer::CheckPacket(int cycle, int sub_cycle)
235  {
236  int res;
237 
238  if (sub_cycle != fLastSubCycle + 1) {
239  jack_error("Packet(s) missing from... %d %d", fLastSubCycle, sub_cycle);
240  res = NET_PACKET_ERROR;
241  } else {
242  res = 0;
243  }
244 
245  fLastSubCycle = sub_cycle;
246  return res;
247  }
248 
249  void NetAudioBuffer::NextCycle()
250  {
251  // reset for next cycle
252  fLastSubCycle = -1;
253  }
254 
255  void NetAudioBuffer::Cleanup()
256  {
257  for (int port_index = 0; port_index < fNPorts; port_index++) {
258  if (fPortBuffer[port_index]) {
259  memset(fPortBuffer[port_index], 0, fPeriodSize * sizeof(sample_t));
260  }
261  }
262  }
263 
264  //network<->buffer
265 
266  int NetAudioBuffer::ActivePortsToNetwork(char* net_buffer)
267  {
268  int active_ports = 0;
269  int* active_port_address = (int*)net_buffer;
270 
271  for (int port_index = 0; port_index < fNPorts; port_index++) {
272  // Write the active port number
273  if (fPortBuffer[port_index]) {
274  *active_port_address = htonl(port_index);
275  active_port_address++;
276  active_ports++;
277  assert(active_ports < 256);
278  }
279  }
280 
281  return active_ports;
282  }
283 
284  void NetAudioBuffer::ActivePortsFromNetwork(char* net_buffer, uint32_t port_num)
285  {
286  int* active_port_address = (int*)net_buffer;
287 
288  for (int port_index = 0; port_index < fNPorts; port_index++) {
289  fConnectedPorts[port_index] = false;
290  }
291 
292  for (uint port_index = 0; port_index < port_num; port_index++) {
293  // Use -1 when port is actually connected on other side
294  int active_port = ntohl(*active_port_address);
295  if (active_port >= 0 && active_port < fNPorts) {
296  fConnectedPorts[active_port] = true;
297  } else {
298  jack_error("ActivePortsFromNetwork: incorrect port = %d", active_port);
299  }
300  active_port_address++;
301  }
302  }
303 
304  int NetAudioBuffer::RenderFromJackPorts()
305  {
306  // Count active ports
307  int active_ports = 0;
308  for (int port_index = 0; port_index < fNPorts; port_index++) {
309 
310  if (fPortBuffer[port_index]) {
311  active_ports++;
312  }
313  }
314  //jack_info("active_ports %d", active_ports);
315  return active_ports;
316  }
317 
318  void NetAudioBuffer::RenderToJackPorts()
319  {
320  // Nothing to do
321  NextCycle();
322  }
323 
324  // Float converter
325 
326  NetFloatAudioBuffer::NetFloatAudioBuffer(session_params_t* params, uint32_t nports, char* net_buffer)
327  : NetAudioBuffer(params, nports, net_buffer)
328  {
329  fPeriodSize = params->fPeriodSize;
330  fPacketSize = PACKET_AVAILABLE_SIZE(params);
331 
332  UpdateParams(max(params->fReturnAudioChannels, params->fSendAudioChannels));
333 
334  fSubPeriodBytesSize = fSubPeriodSize * sizeof(sample_t);
335 
336  fCycleDuration = float(fSubPeriodSize) / float(params->fSampleRate);
337  fCycleBytesSize = params->fMtu * (fPeriodSize / fSubPeriodSize);
338 
339  fLastSubCycle = -1;
340  }
341 
342  NetFloatAudioBuffer::~NetFloatAudioBuffer()
343  {}
344 
345  // needed size in bytes for an entire cycle
346  size_t NetFloatAudioBuffer::GetCycleSize()
347  {
348  return fCycleBytesSize;
349  }
350 
351  // cycle duration in sec
352  float NetFloatAudioBuffer::GetCycleDuration()
353  {
354  return fCycleDuration;
355  }
356 
357  void NetFloatAudioBuffer::UpdateParams(int active_ports)
358  {
359  if (active_ports == 0) {
360  fSubPeriodSize = fPeriodSize;
361  } else {
362  jack_nframes_t period = (int) powf(2.f, (int)(log(float(fPacketSize) / (active_ports * sizeof(sample_t))) / log(2.)));
363  fSubPeriodSize = (period > fPeriodSize) ? fPeriodSize : period;
364  }
365 
366  fSubPeriodBytesSize = fSubPeriodSize * sizeof(sample_t) + sizeof(int); // The port number in coded on 4 bytes
367  }
368 
369  int NetFloatAudioBuffer::GetNumPackets(int active_ports)
370  {
371  UpdateParams(active_ports);
372 
373  /*
374  jack_log("GetNumPackets packet = %d fPeriodSize = %d fSubPeriodSize = %d fSubPeriodBytesSize = %d",
375  fPeriodSize / fSubPeriodSize, fPeriodSize, fSubPeriodSize, fSubPeriodBytesSize);
376  */
377  return fPeriodSize / fSubPeriodSize; // At least one packet
378  }
379 
380  //jack<->buffer
381 
382  int NetFloatAudioBuffer::RenderFromNetwork(int cycle, int sub_cycle, uint32_t port_num)
383  {
384  // Cleanup all JACK ports at the beginning of the cycle
385  if (sub_cycle == 0) {
386  Cleanup();
387  }
388 
389  if (port_num > 0) {
390  UpdateParams(port_num);
391  for (uint32_t port_index = 0; port_index < port_num; port_index++) {
392  // Only copy to active ports : read the active port number then audio data
393  int* active_port_address = (int*)(fNetBuffer + port_index * fSubPeriodBytesSize);
394  int active_port = ntohl(*active_port_address);
395  RenderFromNetwork((char*)(active_port_address + 1), active_port, sub_cycle);
396  }
397  }
398 
399  return CheckPacket(cycle, sub_cycle);
400  }
401 
402 
403  int NetFloatAudioBuffer::RenderToNetwork(int sub_cycle, uint32_t port_num)
404  {
405  int active_ports = 0;
406 
407  for (int port_index = 0; port_index < fNPorts; port_index++) {
408  // Only copy from active ports : write the active port number then audio data
409  if (fPortBuffer[port_index]) {
410  int* active_port_address = (int*)(fNetBuffer + active_ports * fSubPeriodBytesSize);
411  *active_port_address = htonl(port_index);
412  RenderToNetwork((char*)(active_port_address + 1), port_index, sub_cycle);
413  active_ports++;
414  }
415  }
416 
417  return port_num * fSubPeriodBytesSize;
418  }
419 
420 #ifdef __BIG_ENDIAN__
421 
422  static inline jack_default_audio_sample_t SwapFloat(jack_default_audio_sample_t f)
423  {
424  union
425  {
426  jack_default_audio_sample_t f;
427  unsigned char b[4];
428  } dat1, dat2;
429 
430  dat1.f = f;
431  dat2.b[0] = dat1.b[3];
432  dat2.b[1] = dat1.b[2];
433  dat2.b[2] = dat1.b[1];
434  dat2.b[3] = dat1.b[0];
435  return dat2.f;
436  }
437 
438  void NetFloatAudioBuffer::RenderFromNetwork(char* net_buffer, int active_port, int sub_cycle)
439  {
440  if (fPortBuffer[active_port]) {
441  jack_default_audio_sample_t* src = (jack_default_audio_sample_t*)(net_buffer);
442  jack_default_audio_sample_t* dst = (jack_default_audio_sample_t*)(fPortBuffer[active_port] + sub_cycle * fSubPeriodSize);
443  for (unsigned int sample = 0; sample < (fSubPeriodBytesSize - sizeof(int)) / sizeof(jack_default_audio_sample_t); sample++) {
444  dst[sample] = SwapFloat(src[sample]);
445  }
446  }
447  }
448 
449  void NetFloatAudioBuffer::RenderToNetwork(char* net_buffer, int active_port, int sub_cycle)
450  {
451  for (int port_index = 0; port_index < fNPorts; port_index++ ) {
452  jack_default_audio_sample_t* src = (jack_default_audio_sample_t*)(fPortBuffer[active_port] + sub_cycle * fSubPeriodSize);
453  jack_default_audio_sample_t* dst = (jack_default_audio_sample_t*)(net_buffer);
454  for (unsigned int sample = 0; sample < (fSubPeriodBytesSize - sizeof(int)) / sizeof(jack_default_audio_sample_t); sample++) {
455  dst[sample] = SwapFloat(src[sample]);
456  }
457  }
458  }
459 
460 #else
461 
462  void NetFloatAudioBuffer::RenderFromNetwork(char* net_buffer, int active_port, int sub_cycle)
463  {
464  if (fPortBuffer[active_port]) {
465  memcpy(fPortBuffer[active_port] + sub_cycle * fSubPeriodSize, net_buffer, fSubPeriodBytesSize - sizeof(int));
466  }
467  }
468 
469  void NetFloatAudioBuffer::RenderToNetwork(char* net_buffer, int active_port, int sub_cycle)
470  {
471  memcpy(net_buffer, fPortBuffer[active_port] + sub_cycle * fSubPeriodSize, fSubPeriodBytesSize - sizeof(int));
472  }
473 
474 #endif
475  // Celt audio buffer *********************************************************************************
476 
477 #if HAVE_CELT
478 
479  #define KPS 32
480  #define KPS_DIV 8
481 
482  NetCeltAudioBuffer::NetCeltAudioBuffer(session_params_t* params, uint32_t nports, char* net_buffer, int kbps)
483  :NetAudioBuffer(params, nports, net_buffer)
484  {
485  fCeltMode = new CELTMode *[fNPorts];
486  fCeltEncoder = new CELTEncoder *[fNPorts];
487  fCeltDecoder = new CELTDecoder *[fNPorts];
488 
489  memset(fCeltMode, 0, fNPorts * sizeof(CELTMode*));
490  memset(fCeltEncoder, 0, fNPorts * sizeof(CELTEncoder*));
491  memset(fCeltDecoder, 0, fNPorts * sizeof(CELTDecoder*));
492 
493  int error = CELT_OK;
494 
495  for (int i = 0; i < fNPorts; i++) {
496  fCeltMode[i] = celt_mode_create(params->fSampleRate, params->fPeriodSize, &error);
497  if (error != CELT_OK) {
498  goto error;
499  }
500 
501  #if HAVE_CELT_API_0_11
502 
503  fCeltEncoder[i] = celt_encoder_create_custom(fCeltMode[i], 1, &error);
504  if (error != CELT_OK) {
505  goto error;
506  }
507  celt_encoder_ctl(fCeltEncoder[i], CELT_SET_COMPLEXITY(1));
508 
509  fCeltDecoder[i] = celt_decoder_create_custom(fCeltMode[i], 1, &error);
510  if (error != CELT_OK) {
511  goto error;
512  }
513  celt_decoder_ctl(fCeltDecoder[i], CELT_SET_COMPLEXITY(1));
514 
515  #elif HAVE_CELT_API_0_7 || HAVE_CELT_API_0_8
516 
517  fCeltEncoder[i] = celt_encoder_create(fCeltMode[i], 1, &error);
518  if (error != CELT_OK) {
519  goto error;
520  }
521  celt_encoder_ctl(fCeltEncoder[i], CELT_SET_COMPLEXITY(1));
522 
523  fCeltDecoder[i] = celt_decoder_create(fCeltMode[i], 1, &error);
524  if (error != CELT_OK) {
525  goto error;
526  }
527  celt_decoder_ctl(fCeltDecoder[i], CELT_SET_COMPLEXITY(1));
528 
529  #else
530 
531  fCeltEncoder[i] = celt_encoder_create(fCeltMode[i]);
532  if (error != CELT_OK) {
533  goto error;
534  }
535  celt_encoder_ctl(fCeltEncoder[i], CELT_SET_COMPLEXITY(1));
536 
537  fCeltDecoder[i] = celt_decoder_create(fCeltMode[i]);
538  if (error != CELT_OK) {
539  goto error;
540  }
541  celt_decoder_ctl(fCeltDecoder[i], CELT_SET_COMPLEXITY(1));
542 
543  #endif
544  }
545 
546  {
547  fPeriodSize = params->fPeriodSize;
548 
549  fCompressedSizeByte = (kbps * params->fPeriodSize * 1024) / (params->fSampleRate * 8);
550  jack_log("NetCeltAudioBuffer fCompressedSizeByte %d", fCompressedSizeByte);
551 
552  fCompressedBuffer = new unsigned char* [fNPorts];
553  for (int port_index = 0; port_index < fNPorts; port_index++) {
554  fCompressedBuffer[port_index] = new unsigned char[fCompressedSizeByte];
555  memset(fCompressedBuffer[port_index], 0, fCompressedSizeByte * sizeof(char));
556  }
557 
558  int res1 = (fNPorts * fCompressedSizeByte) % PACKET_AVAILABLE_SIZE(params);
559  int res2 = (fNPorts * fCompressedSizeByte) / PACKET_AVAILABLE_SIZE(params);
560 
561  fNumPackets = (res1) ? (res2 + 1) : res2;
562 
563  jack_log("NetCeltAudioBuffer res1 = %d res2 = %d", res1, res2);
564 
565  fSubPeriodBytesSize = fCompressedSizeByte / fNumPackets;
566  fLastSubPeriodBytesSize = fSubPeriodBytesSize + fCompressedSizeByte % fNumPackets;
567 
568  jack_log("NetCeltAudioBuffer fNumPackets = %d fSubPeriodBytesSize = %d, fLastSubPeriodBytesSize = %d", fNumPackets, fSubPeriodBytesSize, fLastSubPeriodBytesSize);
569 
570  fCycleDuration = float(fSubPeriodBytesSize / sizeof(sample_t)) / float(params->fSampleRate);
571  fCycleBytesSize = params->fMtu * fNumPackets;
572 
573  fLastSubCycle = -1;
574  return;
575  }
576 
577  error:
578 
579  FreeCelt();
580  throw std::bad_alloc();
581  }
582 
583  NetCeltAudioBuffer::~NetCeltAudioBuffer()
584  {
585  FreeCelt();
586 
587  for (int port_index = 0; port_index < fNPorts; port_index++) {
588  delete [] fCompressedBuffer[port_index];
589  }
590 
591  delete [] fCompressedBuffer;
592  }
593 
594  void NetCeltAudioBuffer::FreeCelt()
595  {
596  for (int i = 0; i < fNPorts; i++) {
597  if (fCeltEncoder[i]) {
598  celt_encoder_destroy(fCeltEncoder[i]);
599  }
600  if (fCeltDecoder[i]) {
601  celt_decoder_destroy(fCeltDecoder[i]);
602  }
603  if (fCeltMode[i]) {
604  celt_mode_destroy(fCeltMode[i]);
605  }
606  }
607 
608  delete [] fCeltMode;
609  delete [] fCeltEncoder;
610  delete [] fCeltDecoder;
611  }
612 
613  size_t NetCeltAudioBuffer::GetCycleSize()
614  {
615  return fCycleBytesSize;
616  }
617 
618  float NetCeltAudioBuffer::GetCycleDuration()
619  {
620  return fCycleDuration;
621  }
622 
623  int NetCeltAudioBuffer::GetNumPackets(int active_ports)
624  {
625  return fNumPackets;
626  }
627 
628  int NetCeltAudioBuffer::RenderFromJackPorts()
629  {
630  float buffer[fPeriodSize];
631 
632  for (int port_index = 0; port_index < fNPorts; port_index++) {
633  if (fPortBuffer[port_index]) {
634  memcpy(buffer, fPortBuffer[port_index], fPeriodSize * sizeof(sample_t));
635  } else {
636  memset(buffer, 0, fPeriodSize * sizeof(sample_t));
637  }
638  #if HAVE_CELT_API_0_8 || HAVE_CELT_API_0_11
639  int res = celt_encode_float(fCeltEncoder[port_index], buffer, fPeriodSize, fCompressedBuffer[port_index], fCompressedSizeByte);
640  #else
641  int res = celt_encode_float(fCeltEncoder[port_index], buffer, NULL, fCompressedBuffer[port_index], fCompressedSizeByte);
642  #endif
643  if (res != fCompressedSizeByte) {
644  jack_error("celt_encode_float error fCompressedSizeByte = %d res = %d", fCompressedSizeByte, res);
645  }
646  }
647 
648  // All ports active
649  return fNPorts;
650  }
651 
652  void NetCeltAudioBuffer::RenderToJackPorts()
653  {
654  for (int port_index = 0; port_index < fNPorts; port_index++) {
655  if (fPortBuffer[port_index]) {
656  #if HAVE_CELT_API_0_8 || HAVE_CELT_API_0_11
657  int res = celt_decode_float(fCeltDecoder[port_index], fCompressedBuffer[port_index], fCompressedSizeByte, fPortBuffer[port_index], fPeriodSize);
658  #else
659  int res = celt_decode_float(fCeltDecoder[port_index], fCompressedBuffer[port_index], fCompressedSizeByte, fPortBuffer[port_index]);
660  #endif
661  if (res != CELT_OK) {
662  jack_error("celt_decode_float error fCompressedSizeByte = %d res = %d", fCompressedSizeByte, res);
663  }
664  }
665  }
666 
667  NextCycle();
668  }
669 
670  //network<->buffer
671  int NetCeltAudioBuffer::RenderFromNetwork(int cycle, int sub_cycle, uint32_t port_num)
672  {
673  // Cleanup all JACK ports at the beginning of the cycle
674  if (sub_cycle == 0) {
675  Cleanup();
676  }
677 
678  if (port_num > 0) {
679  // Last packet of the cycle
680  if (sub_cycle == fNumPackets - 1) {
681  for (int port_index = 0; port_index < fNPorts; port_index++) {
682  memcpy(fCompressedBuffer[port_index] + sub_cycle * fSubPeriodBytesSize, fNetBuffer + port_index * fLastSubPeriodBytesSize, fLastSubPeriodBytesSize);
683  }
684  } else {
685  for (int port_index = 0; port_index < fNPorts; port_index++) {
686  memcpy(fCompressedBuffer[port_index] + sub_cycle * fSubPeriodBytesSize, fNetBuffer + port_index * fSubPeriodBytesSize, fSubPeriodBytesSize);
687  }
688  }
689  }
690 
691  return CheckPacket(cycle, sub_cycle);
692  }
693 
694  int NetCeltAudioBuffer::RenderToNetwork(int sub_cycle, uint32_t port_num)
695  {
696  // Last packet of the cycle
697  if (sub_cycle == fNumPackets - 1) {
698  for (int port_index = 0; port_index < fNPorts; port_index++) {
699  memcpy(fNetBuffer + port_index * fLastSubPeriodBytesSize, fCompressedBuffer[port_index] + sub_cycle * fSubPeriodBytesSize, fLastSubPeriodBytesSize);
700  }
701  return fNPorts * fLastSubPeriodBytesSize;
702  } else {
703  for (int port_index = 0; port_index < fNPorts; port_index++) {
704  memcpy(fNetBuffer + port_index * fSubPeriodBytesSize, fCompressedBuffer[port_index] + sub_cycle * fSubPeriodBytesSize, fSubPeriodBytesSize);
705  }
706  return fNPorts * fSubPeriodBytesSize;
707  }
708  }
709 
710 #endif
711 
712  NetIntAudioBuffer::NetIntAudioBuffer(session_params_t* params, uint32_t nports, char* net_buffer)
713  : NetAudioBuffer(params, nports, net_buffer)
714  {
715  fPeriodSize = params->fPeriodSize;
716 
717  fCompressedSizeByte = (params->fPeriodSize * sizeof(short));
718  jack_log("NetIntAudioBuffer fCompressedSizeByte %d", fCompressedSizeByte);
719 
720  fIntBuffer = new short* [fNPorts];
721  for (int port_index = 0; port_index < fNPorts; port_index++) {
722  fIntBuffer[port_index] = new short[fPeriodSize];
723  memset(fIntBuffer[port_index], 0, fPeriodSize * sizeof(short));
724  }
725 
726  int res1 = (fNPorts * fCompressedSizeByte) % PACKET_AVAILABLE_SIZE(params);
727  int res2 = (fNPorts * fCompressedSizeByte) / PACKET_AVAILABLE_SIZE(params);
728 
729  jack_log("NetIntAudioBuffer res1 = %d res2 = %d", res1, res2);
730 
731  fNumPackets = (res1) ? (res2 + 1) : res2;
732 
733  fSubPeriodBytesSize = fCompressedSizeByte / fNumPackets;
734  fLastSubPeriodBytesSize = fSubPeriodBytesSize + fCompressedSizeByte % fNumPackets;
735 
736  fSubPeriodSize = fSubPeriodBytesSize / sizeof(short);
737 
738  jack_log("NetIntAudioBuffer fNumPackets = %d fSubPeriodBytesSize = %d, fLastSubPeriodBytesSize = %d", fNumPackets, fSubPeriodBytesSize, fLastSubPeriodBytesSize);
739 
740  fCycleDuration = float(fSubPeriodBytesSize / sizeof(sample_t)) / float(params->fSampleRate);
741  fCycleBytesSize = params->fMtu * fNumPackets;
742 
743  fLastSubCycle = -1;
744  return;
745  }
746 
747  NetIntAudioBuffer::~NetIntAudioBuffer()
748  {
749  for (int port_index = 0; port_index < fNPorts; port_index++) {
750  delete [] fIntBuffer[port_index];
751  }
752 
753  delete [] fIntBuffer;
754  }
755 
756  size_t NetIntAudioBuffer::GetCycleSize()
757  {
758  return fCycleBytesSize;
759  }
760 
761  float NetIntAudioBuffer::GetCycleDuration()
762  {
763  return fCycleDuration;
764  }
765 
766  int NetIntAudioBuffer::GetNumPackets(int active_ports)
767  {
768  return fNumPackets;
769  }
770 
771  int NetIntAudioBuffer::RenderFromJackPorts()
772  {
773  for (int port_index = 0; port_index < fNPorts; port_index++) {
774  if (fPortBuffer[port_index]) {
775  for (uint frame = 0; frame < fPeriodSize; frame++) {
776  fIntBuffer[port_index][frame] = short(fPortBuffer[port_index][frame] * 32768.f);
777  }
778  }
779  }
780 
781  // All ports active
782  return fNPorts;
783  }
784 
785  void NetIntAudioBuffer::RenderToJackPorts()
786  {
787  float coef = 1.f / 32768.f;
788  for (int port_index = 0; port_index < fNPorts; port_index++) {
789  if (fPortBuffer[port_index]) {
790  for (uint frame = 0; frame < fPeriodSize; frame++) {
791  fPortBuffer[port_index][frame] = float(fIntBuffer[port_index][frame] * coef);
792  }
793  }
794  }
795 
796  NextCycle();
797  }
798 
799  //network<->buffer
800  int NetIntAudioBuffer::RenderFromNetwork(int cycle, int sub_cycle, uint32_t port_num)
801  {
802  // Cleanup all JACK ports at the beginning of the cycle
803  if (sub_cycle == 0) {
804  Cleanup();
805  }
806 
807  if (port_num > 0) {
808  if (sub_cycle == fNumPackets - 1) {
809  for (int port_index = 0; port_index < fNPorts; port_index++) {
810  memcpy(fIntBuffer[port_index] + sub_cycle * fSubPeriodSize, fNetBuffer + port_index * fLastSubPeriodBytesSize, fLastSubPeriodBytesSize);
811  }
812  } else {
813  for (int port_index = 0; port_index < fNPorts; port_index++) {
814  memcpy(fIntBuffer[port_index] + sub_cycle * fSubPeriodSize, fNetBuffer + port_index * fSubPeriodBytesSize, fSubPeriodBytesSize);
815  }
816  }
817  }
818 
819  return CheckPacket(cycle, sub_cycle);
820  }
821 
822  int NetIntAudioBuffer::RenderToNetwork(int sub_cycle, uint32_t port_num)
823  {
824  // Last packet of the cycle
825  if (sub_cycle == fNumPackets - 1) {
826  for (int port_index = 0; port_index < fNPorts; port_index++) {
827  memcpy(fNetBuffer + port_index * fLastSubPeriodBytesSize, fIntBuffer[port_index] + sub_cycle * fSubPeriodSize, fLastSubPeriodBytesSize);
828  }
829  return fNPorts * fLastSubPeriodBytesSize;
830  } else {
831  for (int port_index = 0; port_index < fNPorts; port_index++) {
832  memcpy(fNetBuffer + port_index * fSubPeriodBytesSize, fIntBuffer[port_index] + sub_cycle * fSubPeriodSize, fSubPeriodBytesSize);
833  }
834  return fNPorts * fSubPeriodBytesSize;
835  }
836  }
837 
838 // SessionParams ************************************************************************************
839 
840  SERVER_EXPORT void SessionParamsHToN(session_params_t* src_params, session_params_t* dst_params)
841  {
842  memcpy(dst_params, src_params, sizeof(session_params_t));
843  dst_params->fPacketID = htonl(src_params->fPacketID);
844  dst_params->fMtu = htonl(src_params->fMtu);
845  dst_params->fID = htonl(src_params->fID);
846  dst_params->fTransportSync = htonl(src_params->fTransportSync);
847  dst_params->fSendAudioChannels = htonl(src_params->fSendAudioChannels);
848  dst_params->fReturnAudioChannels = htonl(src_params->fReturnAudioChannels);
849  dst_params->fSendMidiChannels = htonl(src_params->fSendMidiChannels);
850  dst_params->fReturnMidiChannels = htonl(src_params->fReturnMidiChannels);
851  dst_params->fSampleRate = htonl(src_params->fSampleRate);
852  dst_params->fPeriodSize = htonl(src_params->fPeriodSize);
853  dst_params->fSampleEncoder = htonl(src_params->fSampleEncoder);
854  dst_params->fKBps = htonl(src_params->fKBps);
855  dst_params->fSlaveSyncMode = htonl(src_params->fSlaveSyncMode);
856  dst_params->fNetworkLatency = htonl(src_params->fNetworkLatency);
857  }
858 
859  SERVER_EXPORT void SessionParamsNToH(session_params_t* src_params, session_params_t* dst_params)
860  {
861  memcpy(dst_params, src_params, sizeof(session_params_t));
862  dst_params->fPacketID = ntohl(src_params->fPacketID);
863  dst_params->fMtu = ntohl(src_params->fMtu);
864  dst_params->fID = ntohl(src_params->fID);
865  dst_params->fTransportSync = ntohl(src_params->fTransportSync);
866  dst_params->fSendAudioChannels = ntohl(src_params->fSendAudioChannels);
867  dst_params->fReturnAudioChannels = ntohl(src_params->fReturnAudioChannels);
868  dst_params->fSendMidiChannels = ntohl(src_params->fSendMidiChannels);
869  dst_params->fReturnMidiChannels = ntohl(src_params->fReturnMidiChannels);
870  dst_params->fSampleRate = ntohl(src_params->fSampleRate);
871  dst_params->fPeriodSize = ntohl(src_params->fPeriodSize);
872  dst_params->fSampleEncoder = ntohl(src_params->fSampleEncoder);
873  dst_params->fKBps = ntohl(src_params->fKBps);
874  dst_params->fSlaveSyncMode = ntohl(src_params->fSlaveSyncMode);
875  dst_params->fNetworkLatency = ntohl(src_params->fNetworkLatency);
876  }
877 
878  SERVER_EXPORT void SessionParamsDisplay(session_params_t* params)
879  {
880  char encoder[16];
881  switch (params->fSampleEncoder)
882  {
883  case JackFloatEncoder:
884  strcpy(encoder, "float");
885  break;
886  case JackIntEncoder:
887  strcpy(encoder, "integer");
888  break;
889  case JackCeltEncoder:
890  strcpy(encoder, "CELT");
891  break;
892  }
893 
894  jack_info("**************** Network parameters ****************");
895  jack_info("Name : %s", params->fName);
896  jack_info("Protocol revision : %d", params->fProtocolVersion);
897  jack_info("MTU : %u", params->fMtu);
898  jack_info("Master name : %s", params->fMasterNetName);
899  jack_info("Slave name : %s", params->fSlaveNetName);
900  jack_info("ID : %u", params->fID);
901  jack_info("Transport Sync : %s", (params->fTransportSync) ? "yes" : "no");
902  jack_info("Send channels (audio - midi) : %d - %d", params->fSendAudioChannels, params->fSendMidiChannels);
903  jack_info("Return channels (audio - midi) : %d - %d", params->fReturnAudioChannels, params->fReturnMidiChannels);
904  jack_info("Sample rate : %u frames per second", params->fSampleRate);
905  jack_info("Period size : %u frames per period", params->fPeriodSize);
906  jack_info("Network latency : %u cycles", params->fNetworkLatency);
907  switch (params->fSampleEncoder) {
908  case (JackFloatEncoder):
909  jack_info("SampleEncoder : %s", "Float");
910  break;
911  case (JackIntEncoder):
912  jack_info("SampleEncoder : %s", "16 bits integer");
913  break;
914  case (JackCeltEncoder):
915  jack_info("SampleEncoder : %s", "CELT");
916  jack_info("kBits : %d", params->fKBps);
917  break;
918  };
919  jack_info("Slave mode : %s", (params->fSlaveSyncMode) ? "sync" : "async");
920  jack_info("****************************************************");
921  }
922 
923  SERVER_EXPORT sync_packet_type_t GetPacketType(session_params_t* params)
924  {
925  switch (params->fPacketID)
926  {
927  case 0:
928  return SLAVE_AVAILABLE;
929  case 1:
930  return SLAVE_SETUP;
931  case 2:
932  return START_MASTER;
933  case 3:
934  return START_SLAVE;
935  case 4:
936  return KILL_MASTER;
937  }
938  return INVALID;
939  }
940 
941  SERVER_EXPORT int SetPacketType(session_params_t* params, sync_packet_type_t packet_type)
942  {
943  switch (packet_type)
944  {
945  case INVALID:
946  return -1;
947  case SLAVE_AVAILABLE:
948  params->fPacketID = 0;
949  break;
950  case SLAVE_SETUP:
951  params->fPacketID = 1;
952  break;
953  case START_MASTER:
954  params->fPacketID = 2;
955  break;
956  case START_SLAVE:
957  params->fPacketID = 3;
958  break;
959  case KILL_MASTER:
960  params->fPacketID = 4;
961  }
962  return 0;
963  }
964 
965 // Packet header **********************************************************************************
966 
967  SERVER_EXPORT void PacketHeaderHToN(packet_header_t* src_header, packet_header_t* dst_header)
968  {
969  memcpy(dst_header, src_header, sizeof(packet_header_t));
970  dst_header->fID = htonl(src_header->fID);
971  dst_header->fNumPacket = htonl(src_header->fNumPacket);
972  dst_header->fPacketSize = htonl(src_header->fPacketSize);
973  dst_header->fActivePorts = htonl(src_header->fActivePorts);
974  dst_header->fCycle = htonl(src_header->fCycle);
975  dst_header->fSubCycle = htonl(src_header->fSubCycle);
976  dst_header->fIsLastPckt = htonl(src_header->fIsLastPckt);
977  }
978 
979  SERVER_EXPORT void PacketHeaderNToH(packet_header_t* src_header, packet_header_t* dst_header)
980  {
981  memcpy(dst_header, src_header, sizeof(packet_header_t));
982  dst_header->fID = ntohl(src_header->fID);
983  dst_header->fNumPacket = ntohl(src_header->fNumPacket);
984  dst_header->fPacketSize = ntohl(src_header->fPacketSize);
985  dst_header->fActivePorts = ntohl(src_header->fActivePorts);
986  dst_header->fCycle = ntohl(src_header->fCycle);
987  dst_header->fSubCycle = ntohl(src_header->fSubCycle);
988  dst_header->fIsLastPckt = ntohl(src_header->fIsLastPckt);
989  }
990 
991  SERVER_EXPORT void PacketHeaderDisplay(packet_header_t* header)
992  {
993  char bitdepth[16];
994  jack_info("********************Header********************");
995  jack_info("Data type : %c", header->fDataType);
996  jack_info("Data stream : %c", header->fDataStream);
997  jack_info("ID : %u", header->fID);
998  jack_info("Cycle : %u", header->fCycle);
999  jack_info("SubCycle : %u", header->fSubCycle);
1000  jack_info("Active ports : %u", header->fActivePorts);
1001  jack_info("DATA packets : %u", header->fNumPacket);
1002  jack_info("DATA size : %u", header->fPacketSize);
1003  jack_info("Last packet : '%s'", (header->fIsLastPckt) ? "yes" : "no");
1004  jack_info("Bitdepth : %s", bitdepth);
1005  jack_info("**********************************************");
1006  }
1007 
1008  SERVER_EXPORT void NetTransportDataDisplay(net_transport_data_t* data)
1009  {
1010  jack_info("********************Network Transport********************");
1011  jack_info("Transport new state : %u", data->fNewState);
1012  jack_info("Transport timebase master : %u", data->fTimebaseMaster);
1013  jack_info("Transport cycle state : %u", data->fState);
1014  jack_info("**********************************************");
1015  }
1016 
1017  SERVER_EXPORT void MidiBufferHToN(JackMidiBuffer* src_buffer, JackMidiBuffer* dst_buffer)
1018  {
1019  dst_buffer->magic = htonl(src_buffer->magic);
1020  dst_buffer->buffer_size = htonl(src_buffer->buffer_size);
1021  dst_buffer->nframes = htonl(src_buffer->nframes);
1022  dst_buffer->write_pos = htonl(src_buffer->write_pos);
1023  dst_buffer->event_count = htonl(src_buffer->event_count);
1024  dst_buffer->lost_events = htonl(src_buffer->lost_events);
1025  dst_buffer->mix_index = htonl(src_buffer->mix_index);
1026  }
1027 
1028  SERVER_EXPORT void MidiBufferNToH(JackMidiBuffer* src_buffer, JackMidiBuffer* dst_buffer)
1029  {
1030  dst_buffer->magic = ntohl(src_buffer->magic);
1031  dst_buffer->buffer_size = ntohl(src_buffer->buffer_size);
1032  dst_buffer->nframes = ntohl(src_buffer->nframes);
1033  dst_buffer->write_pos = ntohl(src_buffer->write_pos);
1034  dst_buffer->event_count = ntohl(src_buffer->event_count);
1035  dst_buffer->lost_events = ntohl(src_buffer->lost_events);
1036  dst_buffer->mix_index = ntohl(src_buffer->mix_index);
1037  }
1038 
1039  SERVER_EXPORT void TransportDataHToN(net_transport_data_t* src_params, net_transport_data_t* dst_params)
1040  {
1041  dst_params->fNewState = htonl(src_params->fNewState);
1042  dst_params->fTimebaseMaster = htonl(src_params->fTimebaseMaster);
1043  dst_params->fState = htonl(src_params->fState);
1044  dst_params->fPosition.unique_1 = htonll(src_params->fPosition.unique_1);
1045  dst_params->fPosition.usecs = htonl(src_params->fPosition.usecs);
1046  dst_params->fPosition.frame_rate = htonl(src_params->fPosition.frame_rate);
1047  dst_params->fPosition.frame = htonl(src_params->fPosition.frame);
1048  dst_params->fPosition.valid = (jack_position_bits_t)htonl((uint32_t)src_params->fPosition.valid);
1049  dst_params->fPosition.bar = htonl(src_params->fPosition.bar);
1050  dst_params->fPosition.beat = htonl(src_params->fPosition.beat);
1051  dst_params->fPosition.tick = htonl(src_params->fPosition.tick);
1052  dst_params->fPosition.bar_start_tick = htonll((uint64_t)src_params->fPosition.bar_start_tick);
1053  dst_params->fPosition.beats_per_bar = htonl((uint32_t)src_params->fPosition.beats_per_bar);
1054  dst_params->fPosition.beat_type = htonl((uint32_t)src_params->fPosition.beat_type);
1055  dst_params->fPosition.ticks_per_beat = htonll((uint64_t)src_params->fPosition.ticks_per_beat);
1056  dst_params->fPosition.beats_per_minute = htonll((uint64_t)src_params->fPosition.beats_per_minute);
1057  dst_params->fPosition.frame_time = htonll((uint64_t)src_params->fPosition.frame_time);
1058  dst_params->fPosition.next_time = htonll((uint64_t)src_params->fPosition.next_time);
1059  dst_params->fPosition.bbt_offset = htonl(src_params->fPosition.bbt_offset);
1060  dst_params->fPosition.audio_frames_per_video_frame = htonl((uint32_t)src_params->fPosition.audio_frames_per_video_frame);
1061  dst_params->fPosition.video_offset = htonl(src_params->fPosition.video_offset);
1062  dst_params->fPosition.unique_2 = htonll(src_params->fPosition.unique_2);
1063  }
1064 
1065  SERVER_EXPORT void TransportDataNToH(net_transport_data_t* src_params, net_transport_data_t* dst_params)
1066  {
1067  dst_params->fNewState = ntohl(src_params->fNewState);
1068  dst_params->fTimebaseMaster = ntohl(src_params->fTimebaseMaster);
1069  dst_params->fState = ntohl(src_params->fState);
1070  dst_params->fPosition.unique_1 = ntohll(src_params->fPosition.unique_1);
1071  dst_params->fPosition.usecs = ntohl(src_params->fPosition.usecs);
1072  dst_params->fPosition.frame_rate = ntohl(src_params->fPosition.frame_rate);
1073  dst_params->fPosition.frame = ntohl(src_params->fPosition.frame);
1074  dst_params->fPosition.valid = (jack_position_bits_t)ntohl((uint32_t)src_params->fPosition.valid);
1075  dst_params->fPosition.bar = ntohl(src_params->fPosition.bar);
1076  dst_params->fPosition.beat = ntohl(src_params->fPosition.beat);
1077  dst_params->fPosition.tick = ntohl(src_params->fPosition.tick);
1078  dst_params->fPosition.bar_start_tick = ntohll((uint64_t)src_params->fPosition.bar_start_tick);
1079  dst_params->fPosition.beats_per_bar = ntohl((uint32_t)src_params->fPosition.beats_per_bar);
1080  dst_params->fPosition.beat_type = ntohl((uint32_t)src_params->fPosition.beat_type);
1081  dst_params->fPosition.ticks_per_beat = ntohll((uint64_t)src_params->fPosition.ticks_per_beat);
1082  dst_params->fPosition.beats_per_minute = ntohll((uint64_t)src_params->fPosition.beats_per_minute);
1083  dst_params->fPosition.frame_time = ntohll((uint64_t)src_params->fPosition.frame_time);
1084  dst_params->fPosition.next_time = ntohll((uint64_t)src_params->fPosition.next_time);
1085  dst_params->fPosition.bbt_offset = ntohl(src_params->fPosition.bbt_offset);
1086  dst_params->fPosition.audio_frames_per_video_frame = ntohl((uint32_t)src_params->fPosition.audio_frames_per_video_frame);
1087  dst_params->fPosition.video_offset = ntohl(src_params->fPosition.video_offset);
1088  dst_params->fPosition.unique_2 = ntohll(src_params->fPosition.unique_2);
1089  }
1090 
1091 // Utility *******************************************************************************************************
1092 
1093  SERVER_EXPORT int SocketAPIInit()
1094  {
1095 #ifdef WIN32
1096  WORD wVersionRequested = MAKEWORD(2, 2);
1097  WSADATA wsaData;
1098 
1099  if (WSAStartup(wVersionRequested, &wsaData) != 0) {
1100  jack_error("WSAStartup error : %s", strerror(NET_ERROR_CODE));
1101  return -1;
1102  }
1103 
1104  if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2) {
1105  jack_error("Could not find a useable version of Winsock.dll\n");
1106  WSACleanup();
1107  return -1;
1108  }
1109 #endif
1110  return 0;
1111  }
1112 
1113  SERVER_EXPORT int SocketAPIEnd()
1114  {
1115 #ifdef WIN32
1116  return WSACleanup();
1117 #endif
1118  return 0;
1119  }
1120 
1121  SERVER_EXPORT const char* GetTransportState(int transport_state)
1122  {
1123  switch (transport_state)
1124  {
1125  case JackTransportRolling:
1126  return "rolling";
1127  case JackTransportStarting:
1128  return "starting";
1129  case JackTransportStopped:
1130  return "stopped";
1131  case JackTransportNetStarting:
1132  return "netstarting";
1133  }
1134  return NULL;
1135  }
1136 }