2 Copyright (C) 2008-2011 Romain Moret at Grame
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.
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.
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.
20 #include "JackNetTool.h"
21 #include "JackError.h"
25 #include <mach/mach_time.h>
36 float GetDeltaTime() const;
37 double GetTime() const;
41 double m_clockToSeconds
;
43 uint64_t m_startAbsTime
;
44 uint64_t m_lastAbsTime
;
50 HardwareClock::HardwareClock()
52 mach_timebase_info_data_t info
;
53 mach_timebase_info(&info
);
54 m_clockToSeconds
= (double)info
.numer
/info
.denom
/1000000000.0;
58 void HardwareClock::Reset()
60 m_startAbsTime
= mach_absolute_time();
61 m_lastAbsTime
= m_startAbsTime
;
62 m_time
= m_startAbsTime
*m_clockToSeconds
;
63 m_deltaTime
= 1.0f
/60.0f
;
66 void HardwareClock::Update()
68 const uint64_t currentTime
= mach_absolute_time();
69 const uint64_t dt
= currentTime
- m_lastAbsTime
;
71 m_time
= currentTime
*m_clockToSeconds
;
72 m_deltaTime
= (double)dt
*m_clockToSeconds
;
73 m_lastAbsTime
= currentTime
;
76 float HardwareClock::GetDeltaTime() const
81 double HardwareClock::GetTime() const
92 // NetMidiBuffer**********************************************************************************
94 NetMidiBuffer::NetMidiBuffer(session_params_t
* params
, uint32_t nports
, char* net_buffer
)
97 fMaxBufsize
= fNPorts
* sizeof(sample_t
) * params
->fPeriodSize
;
98 fMaxPcktSize
= params
->fMtu
- sizeof(packet_header_t
);
99 fBuffer
= new char[fMaxBufsize
];
100 fPortBuffer
= new JackMidiBuffer
* [fNPorts
];
101 for (int port_index
= 0; port_index
< fNPorts
; port_index
++) {
102 fPortBuffer
[port_index
] = NULL
;
104 fNetBuffer
= net_buffer
;
106 fCycleBytesSize
= params
->fMtu
107 * (max(params
->fSendMidiChannels
, params
->fReturnMidiChannels
)
108 * params
->fPeriodSize
* sizeof(sample_t
) / (params
->fMtu
- sizeof(packet_header_t
)));
111 NetMidiBuffer::~NetMidiBuffer()
114 delete[] fPortBuffer
;
117 size_t NetMidiBuffer::GetCycleSize()
119 return fCycleBytesSize
;
122 int NetMidiBuffer::GetNumPackets(int data_size
, int max_size
)
124 int res1
= data_size
% max_size
;
125 int res2
= data_size
/ max_size
;
126 return (res1
) ? res2
+ 1 : res2
;
129 void NetMidiBuffer::SetBuffer(int index
, JackMidiBuffer
* buffer
)
131 fPortBuffer
[index
] = buffer
;
134 JackMidiBuffer
* NetMidiBuffer::GetBuffer(int index
)
136 return fPortBuffer
[index
];
139 void NetMidiBuffer::DisplayEvents()
141 for (int port_index
= 0; port_index
< fNPorts
; port_index
++) {
142 for (uint event
= 0; event
< fPortBuffer
[port_index
]->event_count
; event
++) {
143 if (fPortBuffer
[port_index
]->IsValid()) {
144 jack_info("port %d : midi event %u/%u -> time : %u, size : %u",
145 port_index
+ 1, event
+ 1, fPortBuffer
[port_index
]->event_count
,
146 fPortBuffer
[port_index
]->events
[event
].time
, fPortBuffer
[port_index
]->events
[event
].size
);
152 int NetMidiBuffer::RenderFromJackPorts()
157 for (int port_index
= 0; port_index
< fNPorts
; port_index
++) {
158 char* write_pos
= fBuffer
+ pos
;
159 copy_size
= sizeof(JackMidiBuffer
) + fPortBuffer
[port_index
]->event_count
* sizeof(JackMidiEvent
);
160 memcpy(fBuffer
+ pos
, fPortBuffer
[port_index
], copy_size
);
162 memcpy(fBuffer
+ pos
,
163 fPortBuffer
[port_index
] + (fPortBuffer
[port_index
]->buffer_size
- fPortBuffer
[port_index
]->write_pos
),
164 fPortBuffer
[port_index
]->write_pos
);
165 pos
+= fPortBuffer
[port_index
]->write_pos
;
167 JackMidiBuffer
* midi_buffer
= reinterpret_cast<JackMidiBuffer
*>(write_pos
);
168 MidiBufferHToN(midi_buffer
, midi_buffer
);
173 void NetMidiBuffer::RenderToJackPorts()
178 for (int port_index
= 0; port_index
< fNPorts
; port_index
++) {
179 JackMidiBuffer
* midi_buffer
= reinterpret_cast<JackMidiBuffer
*>(fBuffer
+ pos
);
180 MidiBufferNToH(midi_buffer
, midi_buffer
);
181 copy_size
= sizeof(JackMidiBuffer
) + reinterpret_cast<JackMidiBuffer
*>(fBuffer
+ pos
)->event_count
* sizeof(JackMidiEvent
);
182 memcpy(fPortBuffer
[port_index
], fBuffer
+ pos
, copy_size
);
184 memcpy(fPortBuffer
[port_index
] + (fPortBuffer
[port_index
]->buffer_size
- fPortBuffer
[port_index
]->write_pos
),
186 fPortBuffer
[port_index
]->write_pos
);
187 pos
+= fPortBuffer
[port_index
]->write_pos
;
191 void NetMidiBuffer::RenderFromNetwork(int sub_cycle
, size_t copy_size
)
193 memcpy(fBuffer
+ sub_cycle
* fMaxPcktSize
, fNetBuffer
, copy_size
);
196 int NetMidiBuffer::RenderToNetwork(int sub_cycle
, size_t total_size
)
198 int size
= total_size
- sub_cycle
* fMaxPcktSize
;
199 int copy_size
= (size
<= fMaxPcktSize
) ? size
: fMaxPcktSize
;
200 memcpy(fNetBuffer
, fBuffer
+ sub_cycle
* fMaxPcktSize
, copy_size
);
204 // net audio buffer *********************************************************************************
206 NetAudioBuffer::NetAudioBuffer(session_params_t
* params
, uint32_t nports
, char* net_buffer
)
209 fNetBuffer
= net_buffer
;
211 fPortBuffer
= new sample_t
* [fNPorts
];
212 fConnectedPorts
= new bool[fNPorts
];
213 for (int port_index
= 0; port_index
< fNPorts
; port_index
++) {
214 fPortBuffer
[port_index
] = NULL
;
215 fConnectedPorts
[port_index
] = true;
219 NetAudioBuffer::~NetAudioBuffer()
221 delete [] fConnectedPorts
;
222 delete [] fPortBuffer
;
225 void NetAudioBuffer::SetBuffer(int index
, sample_t
* buffer
)
227 fPortBuffer
[index
] = buffer
;
230 sample_t
* NetAudioBuffer::GetBuffer(int index
)
232 return fPortBuffer
[index
];
235 int NetAudioBuffer::CheckPacket(int cycle
, int sub_cycle
)
239 if (sub_cycle
!= fLastSubCycle
+ 1) {
240 jack_error("Packet(s) missing from... %d %d", fLastSubCycle
, sub_cycle
);
241 res
= NET_PACKET_ERROR
;
246 fLastSubCycle
= sub_cycle
;
250 void NetAudioBuffer::NextCycle()
252 // reset for next cycle
256 void NetAudioBuffer::Cleanup()
258 for (int port_index
= 0; port_index
< fNPorts
; port_index
++) {
259 if (fPortBuffer
[port_index
]) {
260 memset(fPortBuffer
[port_index
], 0, fPeriodSize
* sizeof(sample_t
));
267 int NetAudioBuffer::ActivePortsToNetwork(char* net_buffer
)
269 int active_ports
= 0;
270 int* active_port_address
= (int*)net_buffer
;
272 for (int port_index
= 0; port_index
< fNPorts
; port_index
++) {
273 // Write the active port number
274 if (fPortBuffer
[port_index
]) {
275 *active_port_address
= htonl(port_index
);
276 active_port_address
++;
278 assert(active_ports
< 256);
285 void NetAudioBuffer::ActivePortsFromNetwork(char* net_buffer
, uint32_t port_num
)
287 int* active_port_address
= (int*)net_buffer
;
289 for (int port_index
= 0; port_index
< fNPorts
; port_index
++) {
290 fConnectedPorts
[port_index
] = false;
293 for (uint port_index
= 0; port_index
< port_num
; port_index
++) {
294 // Use -1 when port is actually connected on other side
295 int active_port
= ntohl(*active_port_address
);
296 if (active_port
>= 0 && active_port
< fNPorts
) {
297 fConnectedPorts
[active_port
] = true;
299 jack_error("ActivePortsFromNetwork: incorrect port = %d", active_port
);
301 active_port_address
++;
305 int NetAudioBuffer::RenderFromJackPorts()
307 // Count active ports
308 int active_ports
= 0;
309 for (int port_index
= 0; port_index
< fNPorts
; port_index
++) {
310 if (fPortBuffer
[port_index
]) {
314 //jack_info("active_ports %d", active_ports);
318 void NetAudioBuffer::RenderToJackPorts()
326 NetFloatAudioBuffer::NetFloatAudioBuffer(session_params_t
* params
, uint32_t nports
, char* net_buffer
)
327 : NetAudioBuffer(params
, nports
, net_buffer
)
329 fPeriodSize
= params
->fPeriodSize
;
330 fPacketSize
= PACKET_AVAILABLE_SIZE(params
);
332 UpdateParams(max(params
->fReturnAudioChannels
, params
->fSendAudioChannels
));
334 fSubPeriodBytesSize
= fSubPeriodSize
* sizeof(sample_t
);
336 fCycleDuration
= float(fSubPeriodSize
) / float(params
->fSampleRate
);
337 fCycleBytesSize
= params
->fMtu
* (fPeriodSize
/ fSubPeriodSize
);
342 NetFloatAudioBuffer::~NetFloatAudioBuffer()
345 // needed size in bytes for an entire cycle
346 size_t NetFloatAudioBuffer::GetCycleSize()
348 return fCycleBytesSize
;
351 // cycle duration in sec
352 float NetFloatAudioBuffer::GetCycleDuration()
354 return fCycleDuration
;
357 void NetFloatAudioBuffer::UpdateParams(int active_ports
)
359 if (active_ports
== 0) {
360 fSubPeriodSize
= fPeriodSize
;
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
;
366 fSubPeriodBytesSize
= fSubPeriodSize
* sizeof(sample_t
) + sizeof(int); // The port number in coded on 4 bytes
369 int NetFloatAudioBuffer::GetNumPackets(int active_ports
)
371 UpdateParams(active_ports
);
374 jack_log("GetNumPackets packet = %d fPeriodSize = %d fSubPeriodSize = %d fSubPeriodBytesSize = %d",
375 fPeriodSize / fSubPeriodSize, fPeriodSize, fSubPeriodSize, fSubPeriodBytesSize);
377 return fPeriodSize
/ fSubPeriodSize
; // At least one packet
382 int NetFloatAudioBuffer::RenderFromNetwork(int cycle
, int sub_cycle
, uint32_t port_num
)
384 // Cleanup all JACK ports at the beginning of the cycle
385 if (sub_cycle
== 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
);
399 return CheckPacket(cycle
, sub_cycle
);
403 int NetFloatAudioBuffer::RenderToNetwork(int sub_cycle
, uint32_t port_num
)
405 int active_ports
= 0;
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
);
417 return port_num
* fSubPeriodBytesSize
;
420 #ifdef __BIG_ENDIAN__
422 static inline jack_default_audio_sample_t
SwapFloat(jack_default_audio_sample_t f
)
426 jack_default_audio_sample_t 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];
438 void NetFloatAudioBuffer::RenderFromNetwork(char* net_buffer
, int active_port
, int sub_cycle
)
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
]);
449 void NetFloatAudioBuffer::RenderToNetwork(char* net_buffer
, int active_port
, int sub_cycle
)
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
]);
462 void NetFloatAudioBuffer::RenderFromNetwork(char* net_buffer
, int active_port
, int sub_cycle
)
464 if (fPortBuffer
[active_port
]) {
465 memcpy(fPortBuffer
[active_port
] + sub_cycle
* fSubPeriodSize
, net_buffer
, fSubPeriodBytesSize
- sizeof(int));
469 void NetFloatAudioBuffer::RenderToNetwork(char* net_buffer
, int active_port
, int sub_cycle
)
471 memcpy(net_buffer
, fPortBuffer
[active_port
] + sub_cycle
* fSubPeriodSize
, fSubPeriodBytesSize
- sizeof(int));
475 // Celt audio buffer *********************************************************************************
482 NetCeltAudioBuffer::NetCeltAudioBuffer(session_params_t
* params
, uint32_t nports
, char* net_buffer
, int kbps
)
483 :NetAudioBuffer(params
, nports
, net_buffer
)
485 fCeltMode
= new CELTMode
*[fNPorts
];
486 fCeltEncoder
= new CELTEncoder
*[fNPorts
];
487 fCeltDecoder
= new CELTDecoder
*[fNPorts
];
489 memset(fCeltMode
, 0, fNPorts
* sizeof(CELTMode
*));
490 memset(fCeltEncoder
, 0, fNPorts
* sizeof(CELTEncoder
*));
491 memset(fCeltDecoder
, 0, fNPorts
* sizeof(CELTDecoder
*));
495 for (int i
= 0; i
< fNPorts
; i
++) {
496 fCeltMode
[i
] = celt_mode_create(params
->fSampleRate
, params
->fPeriodSize
, &error
);
497 if (error
!= CELT_OK
) {
501 #if HAVE_CELT_API_0_11
503 fCeltEncoder
[i
] = celt_encoder_create_custom(fCeltMode
[i
], 1, &error
);
504 if (error
!= CELT_OK
) {
507 celt_encoder_ctl(fCeltEncoder
[i
], CELT_SET_COMPLEXITY(1));
509 fCeltDecoder
[i
] = celt_decoder_create_custom(fCeltMode
[i
], 1, &error
);
510 if (error
!= CELT_OK
) {
513 celt_decoder_ctl(fCeltDecoder
[i
], CELT_SET_COMPLEXITY(1));
515 #elif HAVE_CELT_API_0_7 || HAVE_CELT_API_0_8
517 fCeltEncoder
[i
] = celt_encoder_create(fCeltMode
[i
], 1, &error
);
518 if (error
!= CELT_OK
) {
521 celt_encoder_ctl(fCeltEncoder
[i
], CELT_SET_COMPLEXITY(1));
523 fCeltDecoder
[i
] = celt_decoder_create(fCeltMode
[i
], 1, &error
);
524 if (error
!= CELT_OK
) {
527 celt_decoder_ctl(fCeltDecoder
[i
], CELT_SET_COMPLEXITY(1));
531 fCeltEncoder
[i
] = celt_encoder_create(fCeltMode
[i
]);
532 if (error
!= CELT_OK
) {
535 celt_encoder_ctl(fCeltEncoder
[i
], CELT_SET_COMPLEXITY(1));
537 fCeltDecoder
[i
] = celt_decoder_create(fCeltMode
[i
]);
538 if (error
!= CELT_OK
) {
541 celt_decoder_ctl(fCeltDecoder
[i
], CELT_SET_COMPLEXITY(1));
547 fPeriodSize
= params
->fPeriodSize
;
549 fCompressedSizeByte
= (kbps
* params
->fPeriodSize
* 1024) / (params
->fSampleRate
* 8);
550 jack_log("NetCeltAudioBuffer fCompressedSizeByte %d", fCompressedSizeByte
);
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));
558 int res1
= (fNPorts
* fCompressedSizeByte
) % PACKET_AVAILABLE_SIZE(params
);
559 int res2
= (fNPorts
* fCompressedSizeByte
) / PACKET_AVAILABLE_SIZE(params
);
561 fNumPackets
= (res1
) ? (res2
+ 1) : res2
;
563 jack_log("NetCeltAudioBuffer res1 = %d res2 = %d", res1
, res2
);
565 fSubPeriodBytesSize
= fCompressedSizeByte
/ fNumPackets
;
566 fLastSubPeriodBytesSize
= fSubPeriodBytesSize
+ fCompressedSizeByte
% fNumPackets
;
568 jack_log("NetCeltAudioBuffer fNumPackets = %d fSubPeriodBytesSize = %d, fLastSubPeriodBytesSize = %d", fNumPackets
, fSubPeriodBytesSize
, fLastSubPeriodBytesSize
);
570 fCycleDuration
= float(fSubPeriodBytesSize
/ sizeof(sample_t
)) / float(params
->fSampleRate
);
571 fCycleBytesSize
= params
->fMtu
* fNumPackets
;
580 throw std::bad_alloc();
583 NetCeltAudioBuffer::~NetCeltAudioBuffer()
587 for (int port_index
= 0; port_index
< fNPorts
; port_index
++) {
588 delete [] fCompressedBuffer
[port_index
];
591 delete [] fCompressedBuffer
;
594 void NetCeltAudioBuffer::FreeCelt()
596 for (int i
= 0; i
< fNPorts
; i
++) {
597 if (fCeltEncoder
[i
]) {
598 celt_encoder_destroy(fCeltEncoder
[i
]);
600 if (fCeltDecoder
[i
]) {
601 celt_decoder_destroy(fCeltDecoder
[i
]);
604 celt_mode_destroy(fCeltMode
[i
]);
609 delete [] fCeltEncoder
;
610 delete [] fCeltDecoder
;
613 size_t NetCeltAudioBuffer::GetCycleSize()
615 return fCycleBytesSize
;
618 float NetCeltAudioBuffer::GetCycleDuration()
620 return fCycleDuration
;
623 int NetCeltAudioBuffer::GetNumPackets(int active_ports
)
628 int NetCeltAudioBuffer::RenderFromJackPorts()
630 float buffer
[BUFFER_SIZE_MAX
];
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
));
636 memset(buffer
, 0, fPeriodSize
* sizeof(sample_t
));
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
);
641 int res
= celt_encode_float(fCeltEncoder
[port_index
], buffer
, NULL
, fCompressedBuffer
[port_index
], fCompressedSizeByte
);
643 if (res
!= fCompressedSizeByte
) {
644 jack_error("celt_encode_float error fCompressedSizeByte = %d res = %d", fCompressedSizeByte
, res
);
652 void NetCeltAudioBuffer::RenderToJackPorts()
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
);
659 int res
= celt_decode_float(fCeltDecoder
[port_index
], fCompressedBuffer
[port_index
], fCompressedSizeByte
, fPortBuffer
[port_index
]);
661 if (res
!= CELT_OK
) {
662 jack_error("celt_decode_float error fCompressedSizeByte = %d res = %d", fCompressedSizeByte
, res
);
671 int NetCeltAudioBuffer::RenderFromNetwork(int cycle
, int sub_cycle
, uint32_t port_num
)
673 // Cleanup all JACK ports at the beginning of the cycle
674 if (sub_cycle
== 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
);
685 for (int port_index
= 0; port_index
< fNPorts
; port_index
++) {
686 memcpy(fCompressedBuffer
[port_index
] + sub_cycle
* fSubPeriodBytesSize
, fNetBuffer
+ port_index
* fSubPeriodBytesSize
, fSubPeriodBytesSize
);
691 return CheckPacket(cycle
, sub_cycle
);
694 int NetCeltAudioBuffer::RenderToNetwork(int sub_cycle
, uint32_t port_num
)
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
);
701 return fNPorts
* fLastSubPeriodBytesSize
;
703 for (int port_index
= 0; port_index
< fNPorts
; port_index
++) {
704 memcpy(fNetBuffer
+ port_index
* fSubPeriodBytesSize
, fCompressedBuffer
[port_index
] + sub_cycle
* fSubPeriodBytesSize
, fSubPeriodBytesSize
);
706 return fNPorts
* fSubPeriodBytesSize
;
713 #define CDO (sizeof(short)) ///< compressed data offset (first 2 bytes are length)
715 NetOpusAudioBuffer::NetOpusAudioBuffer(session_params_t
* params
, uint32_t nports
, char* net_buffer
, int kbps
)
716 :NetAudioBuffer(params
, nports
, net_buffer
)
718 fOpusMode
= new OpusCustomMode
*[fNPorts
];
719 fOpusEncoder
= new OpusCustomEncoder
*[fNPorts
];
720 fOpusDecoder
= new OpusCustomDecoder
*[fNPorts
];
721 fCompressedSizesByte
= new unsigned short [fNPorts
];
723 memset(fOpusMode
, 0, fNPorts
* sizeof(OpusCustomMode
*));
724 memset(fOpusEncoder
, 0, fNPorts
* sizeof(OpusCustomEncoder
*));
725 memset(fOpusDecoder
, 0, fNPorts
* sizeof(OpusCustomDecoder
*));
726 memset(fCompressedSizesByte
, 0, fNPorts
* sizeof(int));
730 for (int i
= 0; i
< fNPorts
; i
++) {
731 /* Allocate en/decoders */
732 fOpusMode
[i
] = opus_custom_mode_create(
733 params
->fSampleRate
, params
->fPeriodSize
, &error
);
734 if (error
!= OPUS_OK
) {
738 fOpusEncoder
[i
] = opus_custom_encoder_create(fOpusMode
[i
], 1,&error
);
739 if (error
!= OPUS_OK
) {
743 fOpusDecoder
[i
] = opus_custom_decoder_create(fOpusMode
[i
], 1, &error
);
744 if (error
!= OPUS_OK
) {
748 opus_custom_encoder_ctl(fOpusEncoder
[i
], OPUS_SET_BITRATE(kbps
*1024)); // bits per second
749 opus_custom_encoder_ctl(fOpusEncoder
[i
], OPUS_SET_COMPLEXITY(10));
750 opus_custom_encoder_ctl(fOpusEncoder
[i
], OPUS_SET_SIGNAL(OPUS_SIGNAL_MUSIC
));
751 opus_custom_encoder_ctl(fOpusEncoder
[i
], OPUS_SET_SIGNAL(OPUS_APPLICATION_RESTRICTED_LOWDELAY
));
753 /* initilize decoders */
754 error
= opus_custom_encoder_init(fOpusEncoder
[i
], fOpusMode
[i
], 1);
755 error
= opus_custom_decoder_init(fOpusDecoder
[i
], fOpusMode
[i
], 1);
759 fCompressedMaxSizeByte
= (kbps
* params
->fPeriodSize
* 1024) / (params
->fSampleRate
* 8);
760 fPeriodSize
= params
->fPeriodSize
;
761 jack_log("NetOpusAudioBuffer fCompressedMaxSizeByte %d", fCompressedMaxSizeByte
);
763 fCompressedBuffer
= new unsigned char* [fNPorts
];
764 for (int port_index
= 0; port_index
< fNPorts
; port_index
++) {
765 fCompressedBuffer
[port_index
] = new unsigned char[fCompressedMaxSizeByte
];
766 memset(fCompressedBuffer
[port_index
], 0, fCompressedMaxSizeByte
* sizeof(char));
769 int res1
= (fNPorts
* fCompressedMaxSizeByte
+ CDO
) % PACKET_AVAILABLE_SIZE(params
);
770 int res2
= (fNPorts
* fCompressedMaxSizeByte
+ CDO
) / PACKET_AVAILABLE_SIZE(params
);
772 fNumPackets
= (res1
) ? (res2
+ 1) : res2
;
774 jack_log("NetOpusAudioBuffer res1 = %d res2 = %d", res1
, res2
);
776 fSubPeriodBytesSize
= (fCompressedMaxSizeByte
+ CDO
) / fNumPackets
;
777 fLastSubPeriodBytesSize
= fSubPeriodBytesSize
+ (fCompressedMaxSizeByte
+ CDO
) % fNumPackets
;
779 if (fNumPackets
== 1) {
780 fSubPeriodBytesSize
= fLastSubPeriodBytesSize
;
783 jack_log("NetOpusAudioBuffer fNumPackets = %d fSubPeriodBytesSize = %d, fLastSubPeriodBytesSize = %d", fNumPackets
, fSubPeriodBytesSize
, fLastSubPeriodBytesSize
);
785 fCycleDuration
= float(fSubPeriodBytesSize
/ sizeof(sample_t
)) / float(params
->fSampleRate
);
786 fCycleBytesSize
= params
->fMtu
* fNumPackets
;
795 throw std::bad_alloc();
798 NetOpusAudioBuffer::~NetOpusAudioBuffer()
802 for (int port_index
= 0; port_index
< fNPorts
; port_index
++) {
803 delete [] fCompressedBuffer
[port_index
];
806 delete [] fCompressedBuffer
;
807 delete [] fCompressedSizesByte
;
810 void NetOpusAudioBuffer::FreeOpus()
812 for (int i
= 0; i
< fNPorts
; i
++) {
813 if (fOpusEncoder
[i
]) {
814 opus_custom_encoder_destroy(fOpusEncoder
[i
]);
817 if (fOpusDecoder
[i
]) {
818 opus_custom_decoder_destroy(fOpusDecoder
[i
]);
822 opus_custom_mode_destroy(fOpusMode
[i
]);
827 delete [] fOpusEncoder
;
828 delete [] fOpusDecoder
;
832 size_t NetOpusAudioBuffer::GetCycleSize()
834 return fCycleBytesSize
;
837 float NetOpusAudioBuffer::GetCycleDuration()
839 return fCycleDuration
;
842 int NetOpusAudioBuffer::GetNumPackets(int active_ports
)
847 int NetOpusAudioBuffer::RenderFromJackPorts()
849 float buffer
[BUFFER_SIZE_MAX
];
851 for (int port_index
= 0; port_index
< fNPorts
; port_index
++) {
852 if (fPortBuffer
[port_index
]) {
853 memcpy(buffer
, fPortBuffer
[port_index
], fPeriodSize
* sizeof(sample_t
));
855 memset(buffer
, 0, fPeriodSize
* sizeof(sample_t
));
857 int res
= opus_custom_encode_float(fOpusEncoder
[port_index
], buffer
, fPeriodSize
, fCompressedBuffer
[port_index
], fCompressedMaxSizeByte
);
858 if (res
<0 || res
>= 65535) {
859 fCompressedSizesByte
[port_index
] = 0;
861 fCompressedSizesByte
[port_index
] = res
;
869 void NetOpusAudioBuffer::RenderToJackPorts()
871 for (int port_index
= 0; port_index
< fNPorts
; port_index
++) {
872 if (fPortBuffer
[port_index
]) {
873 int res
= opus_custom_decode_float(fOpusDecoder
[port_index
], fCompressedBuffer
[port_index
], fCompressedSizesByte
[port_index
], fPortBuffer
[port_index
], fPeriodSize
);
874 if (res
< 0 || res
!= fPeriodSize
) {
875 jack_error("opus_decode_float error fCompressedSizeByte = %d res = %d", fCompressedSizesByte
[port_index
], res
);
884 int NetOpusAudioBuffer::RenderFromNetwork(int cycle
, int sub_cycle
, uint32_t port_num
)
886 // Cleanup all JACK ports at the beginning of the cycle
887 if (sub_cycle
== 0) {
892 if (sub_cycle
== 0) {
893 for (int port_index
= 0; port_index
< fNPorts
; port_index
++) {
894 size_t len
= *((size_t*)(fNetBuffer
+ port_index
* fSubPeriodBytesSize
));
895 fCompressedSizesByte
[port_index
] = ntohs(len
);
896 memcpy(fCompressedBuffer
[port_index
] + sub_cycle
* fSubPeriodBytesSize
, fNetBuffer
+ CDO
+ port_index
* fSubPeriodBytesSize
, fSubPeriodBytesSize
- CDO
);
898 } else if (sub_cycle
== fNumPackets
- 1) {
899 for (int port_index
= 0; port_index
< fNPorts
; port_index
++) {
900 memcpy(fCompressedBuffer
[port_index
] + sub_cycle
* fSubPeriodBytesSize
- CDO
, fNetBuffer
+ port_index
* fLastSubPeriodBytesSize
, fLastSubPeriodBytesSize
);
903 for (int port_index
= 0; port_index
< fNPorts
; port_index
++) {
904 memcpy(fCompressedBuffer
[port_index
] + sub_cycle
* fSubPeriodBytesSize
- CDO
, fNetBuffer
+ port_index
* fSubPeriodBytesSize
, fSubPeriodBytesSize
);
909 return CheckPacket(cycle
, sub_cycle
);
912 int NetOpusAudioBuffer::RenderToNetwork(int sub_cycle
, uint32_t port_num
)
914 if (sub_cycle
== 0) {
915 for (int port_index
= 0; port_index
< fNPorts
; port_index
++) {
916 unsigned short len
= htons(fCompressedSizesByte
[port_index
]);
917 memcpy(fNetBuffer
+ port_index
* fSubPeriodBytesSize
, &len
, CDO
);
918 memcpy(fNetBuffer
+ port_index
* fSubPeriodBytesSize
+ CDO
, fCompressedBuffer
[port_index
], fSubPeriodBytesSize
- CDO
);
920 return fNPorts
* fSubPeriodBytesSize
;
921 } else if (sub_cycle
== fNumPackets
- 1) {
922 for (int port_index
= 0; port_index
< fNPorts
; port_index
++) {
923 memcpy(fNetBuffer
+ port_index
* fLastSubPeriodBytesSize
, fCompressedBuffer
[port_index
] + sub_cycle
* fSubPeriodBytesSize
- CDO
, fLastSubPeriodBytesSize
);
925 return fNPorts
* fLastSubPeriodBytesSize
;
927 for (int port_index
= 0; port_index
< fNPorts
; port_index
++) {
928 memcpy(fNetBuffer
+ port_index
* fSubPeriodBytesSize
, fCompressedBuffer
[port_index
] + sub_cycle
* fSubPeriodBytesSize
- CDO
, fSubPeriodBytesSize
);
930 return fNPorts
* fSubPeriodBytesSize
;
937 NetIntAudioBuffer::NetIntAudioBuffer(session_params_t
* params
, uint32_t nports
, char* net_buffer
)
938 : NetAudioBuffer(params
, nports
, net_buffer
)
940 fPeriodSize
= params
->fPeriodSize
;
942 fCompressedSizeByte
= (params
->fPeriodSize
* sizeof(short));
943 jack_log("NetIntAudioBuffer fCompressedSizeByte %d", fCompressedSizeByte
);
945 fIntBuffer
= new short* [fNPorts
];
946 for (int port_index
= 0; port_index
< fNPorts
; port_index
++) {
947 fIntBuffer
[port_index
] = new short[fPeriodSize
];
948 memset(fIntBuffer
[port_index
], 0, fPeriodSize
* sizeof(short));
951 int res1
= (fNPorts
* fCompressedSizeByte
) % PACKET_AVAILABLE_SIZE(params
);
952 int res2
= (fNPorts
* fCompressedSizeByte
) / PACKET_AVAILABLE_SIZE(params
);
954 jack_log("NetIntAudioBuffer res1 = %d res2 = %d", res1
, res2
);
956 fNumPackets
= (res1
) ? (res2
+ 1) : res2
;
958 fSubPeriodBytesSize
= fCompressedSizeByte
/ fNumPackets
;
959 fLastSubPeriodBytesSize
= fSubPeriodBytesSize
+ fCompressedSizeByte
% fNumPackets
;
961 fSubPeriodSize
= fSubPeriodBytesSize
/ sizeof(short);
963 jack_log("NetIntAudioBuffer fNumPackets = %d fSubPeriodBytesSize = %d, fLastSubPeriodBytesSize = %d", fNumPackets
, fSubPeriodBytesSize
, fLastSubPeriodBytesSize
);
965 fCycleDuration
= float(fSubPeriodBytesSize
/ sizeof(sample_t
)) / float(params
->fSampleRate
);
966 fCycleBytesSize
= params
->fMtu
* fNumPackets
;
972 NetIntAudioBuffer::~NetIntAudioBuffer()
974 for (int port_index
= 0; port_index
< fNPorts
; port_index
++) {
975 delete [] fIntBuffer
[port_index
];
978 delete [] fIntBuffer
;
981 size_t NetIntAudioBuffer::GetCycleSize()
983 return fCycleBytesSize
;
986 float NetIntAudioBuffer::GetCycleDuration()
988 return fCycleDuration
;
991 int NetIntAudioBuffer::GetNumPackets(int active_ports
)
996 int NetIntAudioBuffer::RenderFromJackPorts()
998 for (int port_index
= 0; port_index
< fNPorts
; port_index
++) {
999 if (fPortBuffer
[port_index
]) {
1000 for (uint frame
= 0; frame
< fPeriodSize
; frame
++) {
1001 fIntBuffer
[port_index
][frame
] = short(fPortBuffer
[port_index
][frame
] * 32768.f
);
1010 void NetIntAudioBuffer::RenderToJackPorts()
1012 float coef
= 1.f
/ 32768.f
;
1013 for (int port_index
= 0; port_index
< fNPorts
; port_index
++) {
1014 if (fPortBuffer
[port_index
]) {
1015 for (uint frame
= 0; frame
< fPeriodSize
; frame
++) {
1016 fPortBuffer
[port_index
][frame
] = float(fIntBuffer
[port_index
][frame
] * coef
);
1025 int NetIntAudioBuffer::RenderFromNetwork(int cycle
, int sub_cycle
, uint32_t port_num
)
1027 // Cleanup all JACK ports at the beginning of the cycle
1028 if (sub_cycle
== 0) {
1033 if (sub_cycle
== fNumPackets
- 1) {
1034 for (int port_index
= 0; port_index
< fNPorts
; port_index
++) {
1035 memcpy(fIntBuffer
[port_index
] + sub_cycle
* fSubPeriodSize
, fNetBuffer
+ port_index
* fLastSubPeriodBytesSize
, fLastSubPeriodBytesSize
);
1038 for (int port_index
= 0; port_index
< fNPorts
; port_index
++) {
1039 memcpy(fIntBuffer
[port_index
] + sub_cycle
* fSubPeriodSize
, fNetBuffer
+ port_index
* fSubPeriodBytesSize
, fSubPeriodBytesSize
);
1044 return CheckPacket(cycle
, sub_cycle
);
1047 int NetIntAudioBuffer::RenderToNetwork(int sub_cycle
, uint32_t port_num
)
1049 // Last packet of the cycle
1050 if (sub_cycle
== fNumPackets
- 1) {
1051 for (int port_index
= 0; port_index
< fNPorts
; port_index
++) {
1052 memcpy(fNetBuffer
+ port_index
* fLastSubPeriodBytesSize
, fIntBuffer
[port_index
] + sub_cycle
* fSubPeriodSize
, fLastSubPeriodBytesSize
);
1054 return fNPorts
* fLastSubPeriodBytesSize
;
1056 for (int port_index
= 0; port_index
< fNPorts
; port_index
++) {
1057 memcpy(fNetBuffer
+ port_index
* fSubPeriodBytesSize
, fIntBuffer
[port_index
] + sub_cycle
* fSubPeriodSize
, fSubPeriodBytesSize
);
1059 return fNPorts
* fSubPeriodBytesSize
;
1063 // SessionParams ************************************************************************************
1065 SERVER_EXPORT
void SessionParamsHToN(session_params_t
* src_params
, session_params_t
* dst_params
)
1067 memcpy(dst_params
, src_params
, sizeof(session_params_t
));
1068 dst_params
->fProtocolVersion
= htonl(src_params
->fProtocolVersion
);
1069 dst_params
->fPacketID
= htonl(src_params
->fPacketID
);
1070 dst_params
->fMtu
= htonl(src_params
->fMtu
);
1071 dst_params
->fID
= htonl(src_params
->fID
);
1072 dst_params
->fTransportSync
= htonl(src_params
->fTransportSync
);
1073 dst_params
->fSendAudioChannels
= htonl(src_params
->fSendAudioChannels
);
1074 dst_params
->fReturnAudioChannels
= htonl(src_params
->fReturnAudioChannels
);
1075 dst_params
->fSendMidiChannels
= htonl(src_params
->fSendMidiChannels
);
1076 dst_params
->fReturnMidiChannels
= htonl(src_params
->fReturnMidiChannels
);
1077 dst_params
->fSampleRate
= htonl(src_params
->fSampleRate
);
1078 dst_params
->fPeriodSize
= htonl(src_params
->fPeriodSize
);
1079 dst_params
->fSampleEncoder
= htonl(src_params
->fSampleEncoder
);
1080 dst_params
->fKBps
= htonl(src_params
->fKBps
);
1081 dst_params
->fSlaveSyncMode
= htonl(src_params
->fSlaveSyncMode
);
1082 dst_params
->fNetworkLatency
= htonl(src_params
->fNetworkLatency
);
1085 SERVER_EXPORT
void SessionParamsNToH(session_params_t
* src_params
, session_params_t
* dst_params
)
1087 memcpy(dst_params
, src_params
, sizeof(session_params_t
));
1088 dst_params
->fProtocolVersion
= ntohl(src_params
->fProtocolVersion
);
1089 dst_params
->fPacketID
= ntohl(src_params
->fPacketID
);
1090 dst_params
->fMtu
= ntohl(src_params
->fMtu
);
1091 dst_params
->fID
= ntohl(src_params
->fID
);
1092 dst_params
->fTransportSync
= ntohl(src_params
->fTransportSync
);
1093 dst_params
->fSendAudioChannels
= ntohl(src_params
->fSendAudioChannels
);
1094 dst_params
->fReturnAudioChannels
= ntohl(src_params
->fReturnAudioChannels
);
1095 dst_params
->fSendMidiChannels
= ntohl(src_params
->fSendMidiChannels
);
1096 dst_params
->fReturnMidiChannels
= ntohl(src_params
->fReturnMidiChannels
);
1097 dst_params
->fSampleRate
= ntohl(src_params
->fSampleRate
);
1098 dst_params
->fPeriodSize
= ntohl(src_params
->fPeriodSize
);
1099 dst_params
->fSampleEncoder
= ntohl(src_params
->fSampleEncoder
);
1100 dst_params
->fKBps
= ntohl(src_params
->fKBps
);
1101 dst_params
->fSlaveSyncMode
= ntohl(src_params
->fSlaveSyncMode
);
1102 dst_params
->fNetworkLatency
= ntohl(src_params
->fNetworkLatency
);
1105 SERVER_EXPORT
void SessionParamsDisplay(session_params_t
* params
)
1108 switch (params
->fSampleEncoder
)
1110 case JackFloatEncoder
:
1111 strcpy(encoder
, "float");
1113 case JackIntEncoder
:
1114 strcpy(encoder
, "integer");
1116 case JackCeltEncoder
:
1117 strcpy(encoder
, "CELT");
1119 case JackOpusEncoder
:
1120 strcpy(encoder
, "OPUS");
1124 jack_info("**************** Network parameters ****************");
1125 jack_info("Name : %s", params
->fName
);
1126 jack_info("Protocol revision : %d", params
->fProtocolVersion
);
1127 jack_info("MTU : %u", params
->fMtu
);
1128 jack_info("Master name : %s", params
->fMasterNetName
);
1129 jack_info("Slave name : %s", params
->fSlaveNetName
);
1130 jack_info("ID : %u", params
->fID
);
1131 jack_info("Transport Sync : %s", (params
->fTransportSync
) ? "yes" : "no");
1132 jack_info("Send channels (audio - midi) : %d - %d", params
->fSendAudioChannels
, params
->fSendMidiChannels
);
1133 jack_info("Return channels (audio - midi) : %d - %d", params
->fReturnAudioChannels
, params
->fReturnMidiChannels
);
1134 jack_info("Sample rate : %u frames per second", params
->fSampleRate
);
1135 jack_info("Period size : %u frames per period", params
->fPeriodSize
);
1136 jack_info("Network latency : %u cycles", params
->fNetworkLatency
);
1137 switch (params
->fSampleEncoder
) {
1138 case (JackFloatEncoder
):
1139 jack_info("SampleEncoder : %s", "Float");
1141 case (JackIntEncoder
):
1142 jack_info("SampleEncoder : %s", "16 bits integer");
1144 case (JackCeltEncoder
):
1145 jack_info("SampleEncoder : %s", "CELT");
1146 jack_info("kBits : %d", params
->fKBps
);
1148 case (JackOpusEncoder
):
1149 jack_info("SampleEncoder : %s", "OPUS");
1150 jack_info("kBits : %d", params
->fKBps
);
1153 jack_info("Slave mode : %s", (params
->fSlaveSyncMode
) ? "sync" : "async");
1154 jack_info("****************************************************");
1157 SERVER_EXPORT sync_packet_type_t
GetPacketType(session_params_t
* params
)
1159 switch (params
->fPacketID
)
1162 return SLAVE_AVAILABLE
;
1166 return START_MASTER
;
1175 SERVER_EXPORT
int SetPacketType(session_params_t
* params
, sync_packet_type_t packet_type
)
1177 switch (packet_type
)
1181 case SLAVE_AVAILABLE
:
1182 params
->fPacketID
= 0;
1185 params
->fPacketID
= 1;
1188 params
->fPacketID
= 2;
1191 params
->fPacketID
= 3;
1194 params
->fPacketID
= 4;
1199 // Packet header **********************************************************************************
1201 SERVER_EXPORT
void PacketHeaderHToN(packet_header_t
* src_header
, packet_header_t
* dst_header
)
1203 memcpy(dst_header
, src_header
, sizeof(packet_header_t
));
1204 dst_header
->fDataType
= htonl(src_header
->fDataType
);
1205 dst_header
->fDataStream
= htonl(src_header
->fDataStream
);
1206 dst_header
->fID
= htonl(src_header
->fID
);
1207 dst_header
->fNumPacket
= htonl(src_header
->fNumPacket
);
1208 dst_header
->fPacketSize
= htonl(src_header
->fPacketSize
);
1209 dst_header
->fActivePorts
= htonl(src_header
->fActivePorts
);
1210 dst_header
->fCycle
= htonl(src_header
->fCycle
);
1211 dst_header
->fSubCycle
= htonl(src_header
->fSubCycle
);
1212 dst_header
->fIsLastPckt
= htonl(src_header
->fIsLastPckt
);
1215 SERVER_EXPORT
void PacketHeaderNToH(packet_header_t
* src_header
, packet_header_t
* dst_header
)
1217 memcpy(dst_header
, src_header
, sizeof(packet_header_t
));
1218 dst_header
->fDataType
= ntohl(src_header
->fDataType
);
1219 dst_header
->fDataStream
= ntohl(src_header
->fDataStream
);
1220 dst_header
->fID
= ntohl(src_header
->fID
);
1221 dst_header
->fNumPacket
= ntohl(src_header
->fNumPacket
);
1222 dst_header
->fPacketSize
= ntohl(src_header
->fPacketSize
);
1223 dst_header
->fActivePorts
= ntohl(src_header
->fActivePorts
);
1224 dst_header
->fCycle
= ntohl(src_header
->fCycle
);
1225 dst_header
->fSubCycle
= ntohl(src_header
->fSubCycle
);
1226 dst_header
->fIsLastPckt
= ntohl(src_header
->fIsLastPckt
);
1229 SERVER_EXPORT
void PacketHeaderDisplay(packet_header_t
* header
)
1232 jack_info("********************Header********************");
1233 jack_info("Data type : %c", header
->fDataType
);
1234 jack_info("Data stream : %c", header
->fDataStream
);
1235 jack_info("ID : %u", header
->fID
);
1236 jack_info("Cycle : %u", header
->fCycle
);
1237 jack_info("SubCycle : %u", header
->fSubCycle
);
1238 jack_info("Active ports : %u", header
->fActivePorts
);
1239 jack_info("DATA packets : %u", header
->fNumPacket
);
1240 jack_info("DATA size : %u", header
->fPacketSize
);
1241 jack_info("Last packet : '%s'", (header
->fIsLastPckt
) ? "yes" : "no");
1242 jack_info("Bitdepth : %s", bitdepth
);
1243 jack_info("**********************************************");
1246 SERVER_EXPORT
void NetTransportDataDisplay(net_transport_data_t
* data
)
1248 jack_info("********************Network Transport********************");
1249 jack_info("Transport new state : %u", data
->fNewState
);
1250 jack_info("Transport timebase master : %u", data
->fTimebaseMaster
);
1251 jack_info("Transport cycle state : %u", data
->fState
);
1252 jack_info("**********************************************");
1255 SERVER_EXPORT
void MidiBufferHToN(JackMidiBuffer
* src_buffer
, JackMidiBuffer
* dst_buffer
)
1257 dst_buffer
->magic
= htonl(src_buffer
->magic
);
1258 dst_buffer
->buffer_size
= htonl(src_buffer
->buffer_size
);
1259 dst_buffer
->nframes
= htonl(src_buffer
->nframes
);
1260 dst_buffer
->write_pos
= htonl(src_buffer
->write_pos
);
1261 dst_buffer
->event_count
= htonl(src_buffer
->event_count
);
1262 dst_buffer
->lost_events
= htonl(src_buffer
->lost_events
);
1263 dst_buffer
->mix_index
= htonl(src_buffer
->mix_index
);
1266 SERVER_EXPORT
void MidiBufferNToH(JackMidiBuffer
* src_buffer
, JackMidiBuffer
* dst_buffer
)
1268 dst_buffer
->magic
= ntohl(src_buffer
->magic
);
1269 dst_buffer
->buffer_size
= ntohl(src_buffer
->buffer_size
);
1270 dst_buffer
->nframes
= ntohl(src_buffer
->nframes
);
1271 dst_buffer
->write_pos
= ntohl(src_buffer
->write_pos
);
1272 dst_buffer
->event_count
= ntohl(src_buffer
->event_count
);
1273 dst_buffer
->lost_events
= ntohl(src_buffer
->lost_events
);
1274 dst_buffer
->mix_index
= ntohl(src_buffer
->mix_index
);
1277 SERVER_EXPORT
void TransportDataHToN(net_transport_data_t
* src_params
, net_transport_data_t
* dst_params
)
1279 dst_params
->fNewState
= htonl(src_params
->fNewState
);
1280 dst_params
->fTimebaseMaster
= htonl(src_params
->fTimebaseMaster
);
1281 dst_params
->fState
= htonl(src_params
->fState
);
1282 dst_params
->fPosition
.unique_1
= htonll(src_params
->fPosition
.unique_1
);
1283 dst_params
->fPosition
.usecs
= htonl(src_params
->fPosition
.usecs
);
1284 dst_params
->fPosition
.frame_rate
= htonl(src_params
->fPosition
.frame_rate
);
1285 dst_params
->fPosition
.frame
= htonl(src_params
->fPosition
.frame
);
1286 dst_params
->fPosition
.valid
= (jack_position_bits_t
)htonl((uint32_t)src_params
->fPosition
.valid
);
1287 dst_params
->fPosition
.bar
= htonl(src_params
->fPosition
.bar
);
1288 dst_params
->fPosition
.beat
= htonl(src_params
->fPosition
.beat
);
1289 dst_params
->fPosition
.tick
= htonl(src_params
->fPosition
.tick
);
1290 dst_params
->fPosition
.bar_start_tick
= htonll((uint64_t)src_params
->fPosition
.bar_start_tick
);
1291 dst_params
->fPosition
.beats_per_bar
= htonl((uint32_t)src_params
->fPosition
.beats_per_bar
);
1292 dst_params
->fPosition
.beat_type
= htonl((uint32_t)src_params
->fPosition
.beat_type
);
1293 dst_params
->fPosition
.ticks_per_beat
= htonll((uint64_t)src_params
->fPosition
.ticks_per_beat
);
1294 dst_params
->fPosition
.beats_per_minute
= htonll((uint64_t)src_params
->fPosition
.beats_per_minute
);
1295 dst_params
->fPosition
.frame_time
= htonll((uint64_t)src_params
->fPosition
.frame_time
);
1296 dst_params
->fPosition
.next_time
= htonll((uint64_t)src_params
->fPosition
.next_time
);
1297 dst_params
->fPosition
.bbt_offset
= htonl(src_params
->fPosition
.bbt_offset
);
1298 dst_params
->fPosition
.audio_frames_per_video_frame
= htonl((uint32_t)src_params
->fPosition
.audio_frames_per_video_frame
);
1299 dst_params
->fPosition
.video_offset
= htonl(src_params
->fPosition
.video_offset
);
1300 dst_params
->fPosition
.unique_2
= htonll(src_params
->fPosition
.unique_2
);
1303 SERVER_EXPORT
void TransportDataNToH(net_transport_data_t
* src_params
, net_transport_data_t
* dst_params
)
1305 dst_params
->fNewState
= ntohl(src_params
->fNewState
);
1306 dst_params
->fTimebaseMaster
= ntohl(src_params
->fTimebaseMaster
);
1307 dst_params
->fState
= ntohl(src_params
->fState
);
1308 dst_params
->fPosition
.unique_1
= ntohll(src_params
->fPosition
.unique_1
);
1309 dst_params
->fPosition
.usecs
= ntohl(src_params
->fPosition
.usecs
);
1310 dst_params
->fPosition
.frame_rate
= ntohl(src_params
->fPosition
.frame_rate
);
1311 dst_params
->fPosition
.frame
= ntohl(src_params
->fPosition
.frame
);
1312 dst_params
->fPosition
.valid
= (jack_position_bits_t
)ntohl((uint32_t)src_params
->fPosition
.valid
);
1313 dst_params
->fPosition
.bar
= ntohl(src_params
->fPosition
.bar
);
1314 dst_params
->fPosition
.beat
= ntohl(src_params
->fPosition
.beat
);
1315 dst_params
->fPosition
.tick
= ntohl(src_params
->fPosition
.tick
);
1316 dst_params
->fPosition
.bar_start_tick
= ntohll((uint64_t)src_params
->fPosition
.bar_start_tick
);
1317 dst_params
->fPosition
.beats_per_bar
= ntohl((uint32_t)src_params
->fPosition
.beats_per_bar
);
1318 dst_params
->fPosition
.beat_type
= ntohl((uint32_t)src_params
->fPosition
.beat_type
);
1319 dst_params
->fPosition
.ticks_per_beat
= ntohll((uint64_t)src_params
->fPosition
.ticks_per_beat
);
1320 dst_params
->fPosition
.beats_per_minute
= ntohll((uint64_t)src_params
->fPosition
.beats_per_minute
);
1321 dst_params
->fPosition
.frame_time
= ntohll((uint64_t)src_params
->fPosition
.frame_time
);
1322 dst_params
->fPosition
.next_time
= ntohll((uint64_t)src_params
->fPosition
.next_time
);
1323 dst_params
->fPosition
.bbt_offset
= ntohl(src_params
->fPosition
.bbt_offset
);
1324 dst_params
->fPosition
.audio_frames_per_video_frame
= ntohl((uint32_t)src_params
->fPosition
.audio_frames_per_video_frame
);
1325 dst_params
->fPosition
.video_offset
= ntohl(src_params
->fPosition
.video_offset
);
1326 dst_params
->fPosition
.unique_2
= ntohll(src_params
->fPosition
.unique_2
);
1329 // Utility *******************************************************************************************************
1331 SERVER_EXPORT
int SocketAPIInit()
1334 WORD wVersionRequested
= MAKEWORD(2, 2);
1337 if (WSAStartup(wVersionRequested
, &wsaData
) != 0) {
1338 jack_error("WSAStartup error : %s", strerror(NET_ERROR_CODE
));
1342 if (LOBYTE(wsaData
.wVersion
) != 2 || HIBYTE(wsaData
.wVersion
) != 2) {
1343 jack_error("Could not find a useable version of Winsock.dll\n");
1351 SERVER_EXPORT
int SocketAPIEnd()
1354 return WSACleanup();
1359 SERVER_EXPORT
const char* GetTransportState(int transport_state
)
1361 switch (transport_state
)
1363 case JackTransportRolling
:
1365 case JackTransportStarting
:
1367 case JackTransportStopped
:
1369 case JackTransportNetStarting
:
1370 return "netstarting";