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
;
105 fCycleBytesSize
= params
->fMtu
106 * (max(params
->fSendMidiChannels
, params
->fReturnMidiChannels
)
107 * params
->fPeriodSize
* sizeof(sample_t
) / (params
->fMtu
- sizeof(packet_header_t
)));
110 NetMidiBuffer::~NetMidiBuffer()
113 delete[] fPortBuffer
;
116 size_t NetMidiBuffer::GetCycleSize()
118 return fCycleBytesSize
;
121 int NetMidiBuffer::GetNumPackets(int data_size
, int max_size
)
123 int res1
= data_size
% max_size
;
124 int res2
= data_size
/ max_size
;
125 return (res1
) ? res2
+ 1 : res2
;
128 void NetMidiBuffer::SetBuffer(int index
, JackMidiBuffer
* buffer
)
130 fPortBuffer
[index
] = buffer
;
133 JackMidiBuffer
* NetMidiBuffer::GetBuffer(int index
)
135 return fPortBuffer
[index
];
138 void NetMidiBuffer::DisplayEvents()
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
);
151 int NetMidiBuffer::RenderFromJackPorts()
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
);
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 JackMidiBuffer
* midi_buffer
= reinterpret_cast<JackMidiBuffer
*>(write_pos
);
166 MidiBufferHToN(midi_buffer
, midi_buffer
);
171 void NetMidiBuffer::RenderToJackPorts()
176 for (int port_index
= 0; port_index
< fNPorts
; port_index
++) {
177 JackMidiBuffer
* midi_buffer
= reinterpret_cast<JackMidiBuffer
*>(fBuffer
+ pos
);
178 MidiBufferNToH(midi_buffer
, midi_buffer
);
179 copy_size
= sizeof(JackMidiBuffer
) + reinterpret_cast<JackMidiBuffer
*>(fBuffer
+ pos
)->event_count
* sizeof(JackMidiEvent
);
180 memcpy(fPortBuffer
[port_index
], fBuffer
+ pos
, copy_size
);
182 memcpy(fPortBuffer
[port_index
] + (fPortBuffer
[port_index
]->buffer_size
- fPortBuffer
[port_index
]->write_pos
),
184 fPortBuffer
[port_index
]->write_pos
);
185 pos
+= fPortBuffer
[port_index
]->write_pos
;
189 void NetMidiBuffer::RenderFromNetwork(int sub_cycle
, size_t copy_size
)
191 memcpy(fBuffer
+ sub_cycle
* fMaxPcktSize
, fNetBuffer
, copy_size
);
194 int NetMidiBuffer::RenderToNetwork(int sub_cycle
, size_t total_size
)
196 int size
= total_size
- sub_cycle
* fMaxPcktSize
;
197 int copy_size
= (size
<= fMaxPcktSize
) ? size
: fMaxPcktSize
;
198 memcpy(fNetBuffer
, fBuffer
+ sub_cycle
* fMaxPcktSize
, copy_size
);
202 // net audio buffer *********************************************************************************
204 NetAudioBuffer::NetAudioBuffer(session_params_t
* params
, uint32_t nports
, char* net_buffer
)
207 fNetBuffer
= net_buffer
;
210 fPortBuffer
= new sample_t
*[fNPorts
];
211 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;
221 fSubPeriodBytesSize
= 0;
222 fCycleDuration
= 0.f
;
226 NetAudioBuffer::~NetAudioBuffer()
228 delete [] fConnectedPorts
;
229 delete [] fPortBuffer
;
232 void NetAudioBuffer::SetBuffer(int index
, sample_t
* buffer
)
234 fPortBuffer
[index
] = buffer
;
237 sample_t
* NetAudioBuffer::GetBuffer(int index
)
239 return fPortBuffer
[index
];
242 int NetAudioBuffer::CheckPacket(int cycle
, int sub_cycle
)
246 if (sub_cycle
!= fLastSubCycle
+ 1) {
247 jack_error("Packet(s) missing from... %d %d", fLastSubCycle
, sub_cycle
);
248 res
= DATA_PACKET_ERROR
;
253 fLastSubCycle
= sub_cycle
;
257 void NetAudioBuffer::NextCycle()
259 // reset for next cycle
263 void NetAudioBuffer::Cleanup()
265 for (int port_index
= 0; port_index
< fNPorts
; port_index
++) {
266 if (fPortBuffer
[port_index
]) {
267 memset(fPortBuffer
[port_index
], 0, fPeriodSize
* sizeof(sample_t
));
274 int NetAudioBuffer::ActivePortsToNetwork(char* net_buffer
)
276 int active_ports
= 0;
277 int* active_port_address
= (int*)net_buffer
;
279 for (int port_index
= 0; port_index
< fNPorts
; port_index
++) {
280 // Write the active port number
281 if (fPortBuffer
[port_index
]) {
282 *active_port_address
= htonl(port_index
);
283 active_port_address
++;
285 assert(active_ports
< 256);
292 void NetAudioBuffer::ActivePortsFromNetwork(char* net_buffer
, uint32_t port_num
)
294 int* active_port_address
= (int*)net_buffer
;
296 for (int port_index
= 0; port_index
< fNPorts
; port_index
++) {
297 fConnectedPorts
[port_index
] = false;
300 for (uint port_index
= 0; port_index
< port_num
; port_index
++) {
301 int active_port
= ntohl(*active_port_address
);
302 assert(active_port
< fNPorts
);
303 fConnectedPorts
[active_port
] = true;
304 active_port_address
++;
308 int NetAudioBuffer::RenderFromJackPorts(int unused_frames
)
310 // Count active ports
311 int active_ports
= 0;
312 for (int port_index
= 0; port_index
< fNPorts
; port_index
++) {
313 if (fPortBuffer
[port_index
]) {
321 void NetAudioBuffer::RenderToJackPorts(int unused_frames
)
329 NetFloatAudioBuffer::NetFloatAudioBuffer(session_params_t
* params
, uint32_t nports
, char* net_buffer
)
330 : NetAudioBuffer(params
, nports
, net_buffer
)
332 fPeriodSize
= params
->fPeriodSize
;
333 fPacketSize
= PACKET_AVAILABLE_SIZE(params
);
335 UpdateParams(max(params
->fReturnAudioChannels
, params
->fSendAudioChannels
));
337 fCycleDuration
= float(fSubPeriodSize
) / float(params
->fSampleRate
);
338 fCycleBytesSize
= params
->fMtu
* (fPeriodSize
/ fSubPeriodSize
);
343 NetFloatAudioBuffer::~NetFloatAudioBuffer()
346 // needed size in bytes for an entire cycle
347 size_t NetFloatAudioBuffer::GetCycleSize()
349 return fCycleBytesSize
;
352 // cycle duration in sec
353 float NetFloatAudioBuffer::GetCycleDuration()
355 return fCycleDuration
;
358 void NetFloatAudioBuffer::UpdateParams(int active_ports
)
360 if (active_ports
== 0) {
361 fSubPeriodSize
= fPeriodSize
;
363 jack_nframes_t period
= int(powf(2.f
, int(log(float(fPacketSize
) / (active_ports
* sizeof(sample_t
))) / log(2.))));
364 fSubPeriodSize
= (period
> fPeriodSize
) ? fPeriodSize
: period
;
367 fSubPeriodBytesSize
= fSubPeriodSize
* sizeof(sample_t
) + sizeof(int); // The port number in coded on 4 bytes
368 fNumPackets
= fPeriodSize
/ fSubPeriodSize
; // At least one packet
371 int NetFloatAudioBuffer::GetNumPackets(int active_ports
)
373 UpdateParams(active_ports
);
376 jack_log("GetNumPackets packet = %d fPeriodSize = %d fSubPeriodSize = %d fSubPeriodBytesSize = %d",
377 fPeriodSize / fSubPeriodSize, fPeriodSize, fSubPeriodSize, fSubPeriodBytesSize);
384 int NetFloatAudioBuffer::RenderFromNetwork(int cycle
, int sub_cycle
, uint32_t port_num
)
386 // Cleanup all JACK ports at the beginning of the cycle
387 if (sub_cycle
== 0) {
392 UpdateParams(port_num
);
393 for (uint32_t port_index
= 0; port_index
< port_num
; port_index
++) {
394 // Only copy to active ports : read the active port number then audio data
395 int* active_port_address
= (int*)(fNetBuffer
+ port_index
* fSubPeriodBytesSize
);
396 int active_port
= ntohl(*active_port_address
);
397 RenderFromNetwork((char*)(active_port_address
+ 1), active_port
, sub_cycle
);
401 return CheckPacket(cycle
, sub_cycle
);
404 int NetFloatAudioBuffer::RenderToNetwork(int sub_cycle
, uint32_t port_num
)
406 int active_ports
= 0;
408 for (int port_index
= 0; port_index
< fNPorts
; port_index
++) {
409 // Only copy from active ports : write the active port number then audio data
410 if (fPortBuffer
[port_index
]) {
411 int* active_port_address
= (int*)(fNetBuffer
+ active_ports
* fSubPeriodBytesSize
);
412 *active_port_address
= htonl(port_index
);
413 RenderToNetwork((char*)(active_port_address
+ 1), port_index
, sub_cycle
);
418 return port_num
* fSubPeriodBytesSize
;
421 #ifdef __BIG_ENDIAN__
423 static inline jack_default_audio_sample_t
SwapFloat(jack_default_audio_sample_t f
)
427 jack_default_audio_sample_t f
;
432 dat2
.b
[0] = dat1
.b
[3];
433 dat2
.b
[1] = dat1
.b
[2];
434 dat2
.b
[2] = dat1
.b
[1];
435 dat2
.b
[3] = dat1
.b
[0];
439 void NetFloatAudioBuffer::RenderFromNetwork(char* net_buffer
, int active_port
, int sub_cycle
)
441 if (fPortBuffer
[active_port
]) {
442 jack_default_audio_sample_t
* src
= (jack_default_audio_sample_t
*)(net_buffer
);
443 jack_default_audio_sample_t
* dst
= (jack_default_audio_sample_t
*)(fPortBuffer
[active_port
] + sub_cycle
* fSubPeriodSize
);
444 for (unsigned int sample
= 0; sample
< (fSubPeriodBytesSize
- sizeof(int)) / sizeof(jack_default_audio_sample_t
); sample
++) {
445 dst
[sample
] = SwapFloat(src
[sample
]);
450 void NetFloatAudioBuffer::RenderToNetwork(char* net_buffer
, int active_port
, int sub_cycle
)
452 for (int port_index
= 0; port_index
< fNPorts
; port_index
++ ) {
453 jack_default_audio_sample_t
* src
= (jack_default_audio_sample_t
*)(fPortBuffer
[active_port
] + sub_cycle
* fSubPeriodSize
);
454 jack_default_audio_sample_t
* dst
= (jack_default_audio_sample_t
*)(net_buffer
);
455 for (unsigned int sample
= 0; sample
< (fSubPeriodBytesSize
- sizeof(int)) / sizeof(jack_default_audio_sample_t
); sample
++) {
456 dst
[sample
] = SwapFloat(src
[sample
]);
463 void NetFloatAudioBuffer::RenderFromNetwork(char* net_buffer
, int active_port
, int sub_cycle
)
465 if (fPortBuffer
[active_port
]) {
466 memcpy(fPortBuffer
[active_port
] + sub_cycle
* fSubPeriodSize
, net_buffer
, fSubPeriodBytesSize
- sizeof(int));
470 void NetFloatAudioBuffer::RenderToNetwork(char* net_buffer
, int active_port
, int sub_cycle
)
472 memcpy(net_buffer
, fPortBuffer
[active_port
] + sub_cycle
* fSubPeriodSize
, fSubPeriodBytesSize
- sizeof(int));
476 // Celt audio buffer *********************************************************************************
483 NetCeltAudioBuffer::NetCeltAudioBuffer(session_params_t
* params
, uint32_t nports
, char* net_buffer
, int kbps
)
484 :NetAudioBuffer(params
, nports
, net_buffer
)
486 fCeltMode
= new CELTMode
*[fNPorts
];
487 fCeltEncoder
= new CELTEncoder
*[fNPorts
];
488 fCeltDecoder
= new CELTDecoder
*[fNPorts
];
490 memset(fCeltMode
, 0, fNPorts
* sizeof(CELTMode
*));
491 memset(fCeltEncoder
, 0, fNPorts
* sizeof(CELTEncoder
*));
492 memset(fCeltDecoder
, 0, fNPorts
* sizeof(CELTDecoder
*));
496 for (int i
= 0; i
< fNPorts
; i
++) {
497 fCeltMode
[i
] = celt_mode_create(params
->fSampleRate
, params
->fPeriodSize
, &error
);
498 if (error
!= CELT_OK
) {
499 jack_log("NetCeltAudioBuffer celt_mode_create err = %d", error
);
503 #if HAVE_CELT_API_0_11
505 fCeltEncoder
[i
] = celt_encoder_create_custom(fCeltMode
[i
], 1, &error
);
506 if (error
!= CELT_OK
) {
507 jack_log("NetCeltAudioBuffer celt_encoder_create_custom err = %d", error
);
510 celt_encoder_ctl(fCeltEncoder
[i
], CELT_SET_COMPLEXITY(1));
512 fCeltDecoder
[i
] = celt_decoder_create_custom(fCeltMode
[i
], 1, &error
);
513 if (error
!= CELT_OK
) {
514 jack_log("NetCeltAudioBuffer celt_decoder_create_custom err = %d", error
);
517 celt_decoder_ctl(fCeltDecoder
[i
], CELT_SET_COMPLEXITY(1));
519 #elif HAVE_CELT_API_0_7 || HAVE_CELT_API_0_8
521 fCeltEncoder
[i
] = celt_encoder_create(fCeltMode
[i
], 1, &error
);
522 if (error
!= CELT_OK
) {
523 jack_log("NetCeltAudioBuffer celt_mode_create err = %d", error
);
526 celt_encoder_ctl(fCeltEncoder
[i
], CELT_SET_COMPLEXITY(1));
528 fCeltDecoder
[i
] = celt_decoder_create(fCeltMode
[i
], 1, &error
);
529 if (error
!= CELT_OK
) {
530 jack_log("NetCeltAudioBuffer celt_decoder_create err = %d", error
);
533 celt_decoder_ctl(fCeltDecoder
[i
], CELT_SET_COMPLEXITY(1));
537 fCeltEncoder
[i
] = celt_encoder_create(fCeltMode
[i
]);
538 if (error
!= CELT_OK
) {
539 jack_log("NetCeltAudioBuffer celt_encoder_create err = %d", error
);
542 celt_encoder_ctl(fCeltEncoder
[i
], CELT_SET_COMPLEXITY(1));
544 fCeltDecoder
[i
] = celt_decoder_create(fCeltMode
[i
]);
545 if (error
!= CELT_OK
) {
546 jack_log("NetCeltAudioBuffer celt_decoder_create err = %d", error
);
549 celt_decoder_ctl(fCeltDecoder
[i
], CELT_SET_COMPLEXITY(1));
555 fPeriodSize
= params
->fPeriodSize
;
557 fCompressedSizeByte
= (kbps
* params
->fPeriodSize
* 1024) / (params
->fSampleRate
* 8);
558 jack_log("NetCeltAudioBuffer fCompressedSizeByte %d", fCompressedSizeByte
);
560 fCompressedBuffer
= new unsigned char* [fNPorts
];
561 for (int port_index
= 0; port_index
< fNPorts
; port_index
++) {
562 fCompressedBuffer
[port_index
] = new unsigned char[fCompressedSizeByte
];
563 memset(fCompressedBuffer
[port_index
], 0, fCompressedSizeByte
* sizeof(char));
566 int res1
= (fNPorts
* fCompressedSizeByte
) % PACKET_AVAILABLE_SIZE(params
);
567 int res2
= (fNPorts
* fCompressedSizeByte
) / PACKET_AVAILABLE_SIZE(params
);
569 fNumPackets
= (res1
) ? (res2
+ 1) : res2
;
571 jack_log("NetCeltAudioBuffer res1 = %d res2 = %d", res1
, res2
);
573 fSubPeriodBytesSize
= fCompressedSizeByte
/ fNumPackets
;
574 fLastSubPeriodBytesSize
= fSubPeriodBytesSize
+ fCompressedSizeByte
% fNumPackets
;
576 jack_log("NetCeltAudioBuffer fNumPackets = %d fSubPeriodBytesSize = %d, fLastSubPeriodBytesSize = %d", fNumPackets
, fSubPeriodBytesSize
, fLastSubPeriodBytesSize
);
578 fCycleDuration
= float(fSubPeriodBytesSize
/ sizeof(sample_t
)) / float(params
->fSampleRate
);
579 fCycleBytesSize
= params
->fMtu
* fNumPackets
;
588 throw std::bad_alloc();
591 NetCeltAudioBuffer::~NetCeltAudioBuffer()
595 for (int port_index
= 0; port_index
< fNPorts
; port_index
++) {
596 delete [] fCompressedBuffer
[port_index
];
599 delete [] fCompressedBuffer
;
602 void NetCeltAudioBuffer::FreeCelt()
604 for (int i
= 0; i
< fNPorts
; i
++) {
605 if (fCeltEncoder
[i
]) {
606 celt_encoder_destroy(fCeltEncoder
[i
]);
608 if (fCeltDecoder
[i
]) {
609 celt_decoder_destroy(fCeltDecoder
[i
]);
612 celt_mode_destroy(fCeltMode
[i
]);
617 delete [] fCeltEncoder
;
618 delete [] fCeltDecoder
;
621 size_t NetCeltAudioBuffer::GetCycleSize()
623 return fCycleBytesSize
;
626 float NetCeltAudioBuffer::GetCycleDuration()
628 return fCycleDuration
;
631 int NetCeltAudioBuffer::GetNumPackets(int active_ports
)
636 int NetCeltAudioBuffer::RenderFromJackPorts(int nframes
)
638 float buffer
[BUFFER_SIZE_MAX
];
640 for (int port_index
= 0; port_index
< fNPorts
; port_index
++) {
641 if (fPortBuffer
[port_index
]) {
642 memcpy(buffer
, fPortBuffer
[port_index
], fPeriodSize
* sizeof(sample_t
));
644 memset(buffer
, 0, fPeriodSize
* sizeof(sample_t
));
646 #if HAVE_CELT_API_0_8 || HAVE_CELT_API_0_11
647 //int res = celt_encode_float(fCeltEncoder[port_index], buffer, fPeriodSize, fCompressedBuffer[port_index], fCompressedSizeByte);
648 int res
= celt_encode_float(fCeltEncoder
[port_index
], buffer
, nframes
, fCompressedBuffer
[port_index
], fCompressedSizeByte
);
650 int res
= celt_encode_float(fCeltEncoder
[port_index
], buffer
, NULL
, fCompressedBuffer
[port_index
], fCompressedSizeByte
);
652 if (res
!= fCompressedSizeByte
) {
653 jack_error("celt_encode_float error fCompressedSizeByte = %d res = %d", fCompressedSizeByte
, res
);
661 void NetCeltAudioBuffer::RenderToJackPorts(int nframes
)
663 for (int port_index
= 0; port_index
< fNPorts
; port_index
++) {
664 if (fPortBuffer
[port_index
]) {
665 #if HAVE_CELT_API_0_8 || HAVE_CELT_API_0_11
666 //int res = celt_decode_float(fCeltDecoder[port_index], fCompressedBuffer[port_index], fCompressedSizeByte, fPortBuffer[port_index], fPeriodSize);
667 int res
= celt_decode_float(fCeltDecoder
[port_index
], fCompressedBuffer
[port_index
], fCompressedSizeByte
, fPortBuffer
[port_index
], nframes
);
669 int res
= celt_decode_float(fCeltDecoder
[port_index
], fCompressedBuffer
[port_index
], fCompressedSizeByte
, fPortBuffer
[port_index
]);
671 if (res
!= CELT_OK
) {
672 jack_error("celt_decode_float error fCompressedSizeByte = %d res = %d", fCompressedSizeByte
, res
);
681 int NetCeltAudioBuffer::RenderFromNetwork(int cycle
, int sub_cycle
, uint32_t port_num
)
683 // Cleanup all JACK ports at the beginning of the cycle
684 if (sub_cycle
== 0) {
690 int sub_period_bytes_size
;
692 // Last packet of the cycle
693 if (sub_cycle
== fNumPackets
- 1) {
694 sub_period_bytes_size
= fLastSubPeriodBytesSize
;
696 sub_period_bytes_size
= fSubPeriodBytesSize
;
699 for (int port_index
= 0; port_index
< fNPorts
; port_index
++) {
700 memcpy(fCompressedBuffer
[port_index
] + sub_cycle
* fSubPeriodBytesSize
, fNetBuffer
+ port_index
* sub_period_bytes_size
, sub_period_bytes_size
);
704 return CheckPacket(cycle
, sub_cycle
);
707 int NetCeltAudioBuffer::RenderToNetwork(int sub_cycle
, uint32_t port_num
)
709 int sub_period_bytes_size
;
711 // Last packet of the cycle
712 if (sub_cycle
== fNumPackets
- 1) {
713 sub_period_bytes_size
= fLastSubPeriodBytesSize
;
715 sub_period_bytes_size
= fSubPeriodBytesSize
;
718 for (int port_index
= 0; port_index
< fNPorts
; port_index
++) {
719 memcpy(fNetBuffer
+ port_index
* sub_period_bytes_size
, fCompressedBuffer
[port_index
] + sub_cycle
* fSubPeriodBytesSize
, sub_period_bytes_size
);
721 return fNPorts
* sub_period_bytes_size
;
728 #define CDO (sizeof(short)) ///< compressed data offset (first 2 bytes are length)
729 NetOpusAudioBuffer::NetOpusAudioBuffer(session_params_t
* params
, uint32_t nports
, char* net_buffer
, int kbps
)
730 :NetAudioBuffer(params
, nports
, net_buffer
)
732 fOpusMode
= new OpusCustomMode
*[fNPorts
];
733 fOpusEncoder
= new OpusCustomEncoder
*[fNPorts
];
734 fOpusDecoder
= new OpusCustomDecoder
*[fNPorts
];
735 fCompressedSizesByte
= new unsigned short[fNPorts
];
737 memset(fOpusMode
, 0, fNPorts
* sizeof(OpusCustomMode
*));
738 memset(fOpusEncoder
, 0, fNPorts
* sizeof(OpusCustomEncoder
*));
739 memset(fOpusDecoder
, 0, fNPorts
* sizeof(OpusCustomDecoder
*));
740 memset(fCompressedSizesByte
, 0, fNPorts
* sizeof(short));
744 for (int i
= 0; i
< fNPorts
; i
++) {
745 /* Allocate en/decoders */
746 fOpusMode
[i
] = opus_custom_mode_create(params
->fSampleRate
, params
->fPeriodSize
, &error
);
747 if (error
!= OPUS_OK
) {
748 jack_log("NetOpusAudioBuffer opus_custom_mode_create err = %d", error
);
752 fOpusEncoder
[i
] = opus_custom_encoder_create(fOpusMode
[i
], 1, &error
);
753 if (error
!= OPUS_OK
) {
754 jack_log("NetOpusAudioBuffer opus_custom_encoder_create err = %d", error
);
758 fOpusDecoder
[i
] = opus_custom_decoder_create(fOpusMode
[i
], 1, &error
);
759 if (error
!= OPUS_OK
) {
760 jack_log("NetOpusAudioBuffer opus_custom_decoder_create err = %d", error
);
764 opus_custom_encoder_ctl(fOpusEncoder
[i
], OPUS_SET_BITRATE(kbps
*1024)); // bits per second
765 opus_custom_encoder_ctl(fOpusEncoder
[i
], OPUS_SET_COMPLEXITY(10));
766 opus_custom_encoder_ctl(fOpusEncoder
[i
], OPUS_SET_SIGNAL(OPUS_SIGNAL_MUSIC
));
767 opus_custom_encoder_ctl(fOpusEncoder
[i
], OPUS_SET_SIGNAL(OPUS_APPLICATION_RESTRICTED_LOWDELAY
));
771 fCompressedMaxSizeByte
= (kbps
* params
->fPeriodSize
* 1024) / (params
->fSampleRate
* 8);
772 fPeriodSize
= params
->fPeriodSize
;
773 jack_log("NetOpusAudioBuffer fCompressedMaxSizeByte %d", fCompressedMaxSizeByte
);
775 fCompressedBuffer
= new unsigned char* [fNPorts
];
776 for (int port_index
= 0; port_index
< fNPorts
; port_index
++) {
777 fCompressedBuffer
[port_index
] = new unsigned char[fCompressedMaxSizeByte
];
778 memset(fCompressedBuffer
[port_index
], 0, fCompressedMaxSizeByte
* sizeof(char));
781 int res1
= (fNPorts
* fCompressedMaxSizeByte
+ CDO
) % PACKET_AVAILABLE_SIZE(params
);
782 int res2
= (fNPorts
* fCompressedMaxSizeByte
+ CDO
) / PACKET_AVAILABLE_SIZE(params
);
784 fNumPackets
= (res1
) ? (res2
+ 1) : res2
;
786 jack_log("NetOpusAudioBuffer res1 = %d res2 = %d", res1
, res2
);
788 fSubPeriodBytesSize
= (fCompressedMaxSizeByte
+ CDO
) / fNumPackets
;
789 fLastSubPeriodBytesSize
= fSubPeriodBytesSize
+ (fCompressedMaxSizeByte
+ CDO
) % fNumPackets
;
791 if (fNumPackets
== 1) {
792 fSubPeriodBytesSize
= fLastSubPeriodBytesSize
;
795 jack_log("NetOpusAudioBuffer fNumPackets = %d fSubPeriodBytesSize = %d, fLastSubPeriodBytesSize = %d", fNumPackets
, fSubPeriodBytesSize
, fLastSubPeriodBytesSize
);
797 fCycleDuration
= float(fSubPeriodBytesSize
/ sizeof(sample_t
)) / float(params
->fSampleRate
);
798 fCycleBytesSize
= params
->fMtu
* fNumPackets
;
807 throw std::bad_alloc();
810 NetOpusAudioBuffer::~NetOpusAudioBuffer()
814 for (int port_index
= 0; port_index
< fNPorts
; port_index
++) {
815 delete [] fCompressedBuffer
[port_index
];
818 delete [] fCompressedBuffer
;
819 delete [] fCompressedSizesByte
;
822 void NetOpusAudioBuffer::FreeOpus()
824 for (int i
= 0; i
< fNPorts
; i
++) {
825 if (fOpusEncoder
[i
]) {
826 opus_custom_encoder_destroy(fOpusEncoder
[i
]);
829 if (fOpusDecoder
[i
]) {
830 opus_custom_decoder_destroy(fOpusDecoder
[i
]);
834 opus_custom_mode_destroy(fOpusMode
[i
]);
839 delete [] fOpusEncoder
;
840 delete [] fOpusDecoder
;
844 size_t NetOpusAudioBuffer::GetCycleSize()
846 return fCycleBytesSize
;
849 float NetOpusAudioBuffer::GetCycleDuration()
851 return fCycleDuration
;
854 int NetOpusAudioBuffer::GetNumPackets(int active_ports
)
859 int NetOpusAudioBuffer::RenderFromJackPorts(int nframes
)
861 float buffer
[BUFFER_SIZE_MAX
];
863 for (int port_index
= 0; port_index
< fNPorts
; port_index
++) {
864 if (fPortBuffer
[port_index
]) {
865 memcpy(buffer
, fPortBuffer
[port_index
], fPeriodSize
* sizeof(sample_t
));
867 memset(buffer
, 0, fPeriodSize
* sizeof(sample_t
));
869 int res
= opus_custom_encode_float(fOpusEncoder
[port_index
], buffer
, ((nframes
== -1) ? fPeriodSize
: nframes
), fCompressedBuffer
[port_index
], fCompressedMaxSizeByte
);
870 if (res
< 0 || res
>= 65535) {
871 jack_error("opus_custom_encode_float error res = %d", res
);
872 fCompressedSizesByte
[port_index
] = 0;
874 fCompressedSizesByte
[port_index
] = res
;
882 void NetOpusAudioBuffer::RenderToJackPorts(int nframes
)
884 for (int port_index
= 0; port_index
< fNPorts
; port_index
++) {
885 if (fPortBuffer
[port_index
]) {
886 int res
= opus_custom_decode_float(fOpusDecoder
[port_index
], fCompressedBuffer
[port_index
], fCompressedSizesByte
[port_index
], fPortBuffer
[port_index
], ((nframes
== -1) ? fPeriodSize
: nframes
));
887 if (res
< 0 || res
!= ((nframes
== -1) ? fPeriodSize
: nframes
)) {
888 jack_error("opus_custom_decode_float error fCompressedSizeByte = %d res = %d", fCompressedSizesByte
[port_index
], res
);
897 int NetOpusAudioBuffer::RenderFromNetwork(int cycle
, int sub_cycle
, uint32_t port_num
)
899 // Cleanup all JACK ports at the beginning of the cycle
900 if (sub_cycle
== 0) {
905 if (sub_cycle
== 0) {
906 for (int port_index
= 0; port_index
< fNPorts
; port_index
++) {
907 size_t len
= *((size_t*)(fNetBuffer
+ port_index
* fSubPeriodBytesSize
));
908 fCompressedSizesByte
[port_index
] = ntohs(len
);
909 memcpy(fCompressedBuffer
[port_index
] + sub_cycle
* fSubPeriodBytesSize
, fNetBuffer
+ CDO
+ port_index
* fSubPeriodBytesSize
, fSubPeriodBytesSize
- CDO
);
911 } else if (sub_cycle
== fNumPackets
- 1) {
912 for (int port_index
= 0; port_index
< fNPorts
; port_index
++) {
913 memcpy(fCompressedBuffer
[port_index
] + sub_cycle
* fSubPeriodBytesSize
- CDO
, fNetBuffer
+ port_index
* fLastSubPeriodBytesSize
, fLastSubPeriodBytesSize
);
916 for (int port_index
= 0; port_index
< fNPorts
; port_index
++) {
917 memcpy(fCompressedBuffer
[port_index
] + sub_cycle
* fSubPeriodBytesSize
- CDO
, fNetBuffer
+ port_index
* fSubPeriodBytesSize
, fSubPeriodBytesSize
);
922 return CheckPacket(cycle
, sub_cycle
);
925 int NetOpusAudioBuffer::RenderToNetwork(int sub_cycle
, uint32_t port_num
)
927 if (sub_cycle
== 0) {
928 for (int port_index
= 0; port_index
< fNPorts
; port_index
++) {
929 unsigned short len
= htons(fCompressedSizesByte
[port_index
]);
930 memcpy(fNetBuffer
+ port_index
* fSubPeriodBytesSize
, &len
, CDO
);
931 memcpy(fNetBuffer
+ port_index
* fSubPeriodBytesSize
+ CDO
, fCompressedBuffer
[port_index
], fSubPeriodBytesSize
- CDO
);
933 return fNPorts
* fSubPeriodBytesSize
;
934 } else if (sub_cycle
== fNumPackets
- 1) {
935 for (int port_index
= 0; port_index
< fNPorts
; port_index
++) {
936 memcpy(fNetBuffer
+ port_index
* fLastSubPeriodBytesSize
, fCompressedBuffer
[port_index
] + sub_cycle
* fSubPeriodBytesSize
- CDO
, fLastSubPeriodBytesSize
);
938 return fNPorts
* fLastSubPeriodBytesSize
;
940 for (int port_index
= 0; port_index
< fNPorts
; port_index
++) {
941 memcpy(fNetBuffer
+ port_index
* fSubPeriodBytesSize
, fCompressedBuffer
[port_index
] + sub_cycle
* fSubPeriodBytesSize
- CDO
, fSubPeriodBytesSize
);
943 return fNPorts
* fSubPeriodBytesSize
;
949 NetIntAudioBuffer::NetIntAudioBuffer(session_params_t
* params
, uint32_t nports
, char* net_buffer
)
950 : NetAudioBuffer(params
, nports
, net_buffer
)
952 fPeriodSize
= params
->fPeriodSize
;
954 fCompressedSizeByte
= (params
->fPeriodSize
* sizeof(short));
955 jack_log("NetIntAudioBuffer fCompressedSizeByte %d", fCompressedSizeByte
);
957 fIntBuffer
= new short* [fNPorts
];
958 for (int port_index
= 0; port_index
< fNPorts
; port_index
++) {
959 fIntBuffer
[port_index
] = new short[fPeriodSize
];
960 memset(fIntBuffer
[port_index
], 0, fPeriodSize
* sizeof(short));
963 int res1
= (fNPorts
* fCompressedSizeByte
) % PACKET_AVAILABLE_SIZE(params
);
964 int res2
= (fNPorts
* fCompressedSizeByte
) / PACKET_AVAILABLE_SIZE(params
);
966 jack_log("NetIntAudioBuffer res1 = %d res2 = %d", res1
, res2
);
968 fNumPackets
= (res1
) ? (res2
+ 1) : res2
;
970 fSubPeriodBytesSize
= fCompressedSizeByte
/ fNumPackets
;
971 fLastSubPeriodBytesSize
= fSubPeriodBytesSize
+ fCompressedSizeByte
% fNumPackets
;
973 fSubPeriodSize
= fSubPeriodBytesSize
/ sizeof(short);
975 jack_log("NetIntAudioBuffer fNumPackets = %d fSubPeriodBytesSize = %d, fLastSubPeriodBytesSize = %d", fNumPackets
, fSubPeriodBytesSize
, fLastSubPeriodBytesSize
);
977 fCycleDuration
= float(fSubPeriodBytesSize
/ sizeof(sample_t
)) / float(params
->fSampleRate
);
978 fCycleBytesSize
= params
->fMtu
* fNumPackets
;
983 NetIntAudioBuffer::~NetIntAudioBuffer()
985 for (int port_index
= 0; port_index
< fNPorts
; port_index
++) {
986 delete [] fIntBuffer
[port_index
];
989 delete [] fIntBuffer
;
992 size_t NetIntAudioBuffer::GetCycleSize()
994 return fCycleBytesSize
;
997 float NetIntAudioBuffer::GetCycleDuration()
999 return fCycleDuration
;
1002 int NetIntAudioBuffer::GetNumPackets(int active_ports
)
1007 int NetIntAudioBuffer::RenderFromJackPorts(int nframes
)
1009 for (int port_index
= 0; port_index
< fNPorts
; port_index
++) {
1010 if (fPortBuffer
[port_index
]) {
1011 for (int frame
= 0; frame
< nframes
; frame
++) {
1012 fIntBuffer
[port_index
][frame
] = short(fPortBuffer
[port_index
][frame
] * 32767.f
);
1015 memset(fIntBuffer
[port_index
], 0, fPeriodSize
* sizeof(short));
1023 void NetIntAudioBuffer::RenderToJackPorts(int nframes
)
1025 float coef
= 1.f
/ 32767.f
;
1026 for (int port_index
= 0; port_index
< fNPorts
; port_index
++) {
1027 if (fPortBuffer
[port_index
]) {
1028 for (int frame
= 0; frame
< nframes
; frame
++) {
1029 fPortBuffer
[port_index
][frame
] = float(fIntBuffer
[port_index
][frame
] * coef
);
1038 int NetIntAudioBuffer::RenderFromNetwork(int cycle
, int sub_cycle
, uint32_t port_num
)
1040 // Cleanup all JACK ports at the beginning of the cycle
1041 if (sub_cycle
== 0) {
1046 int sub_period_bytes_size
;
1049 if (sub_cycle
== fNumPackets
- 1) {
1050 sub_period_bytes_size
= fLastSubPeriodBytesSize
;
1052 sub_period_bytes_size
= fSubPeriodBytesSize
;
1055 for (int port_index
= 0; port_index
< fNPorts
; port_index
++) {
1056 memcpy(fIntBuffer
[port_index
] + sub_cycle
* fSubPeriodSize
, fNetBuffer
+ port_index
* sub_period_bytes_size
, sub_period_bytes_size
);
1060 return CheckPacket(cycle
, sub_cycle
);
1063 int NetIntAudioBuffer::RenderToNetwork(int sub_cycle
, uint32_t port_num
)
1065 int sub_period_bytes_size
;
1068 if (sub_cycle
== fNumPackets
- 1) {
1069 sub_period_bytes_size
= fLastSubPeriodBytesSize
;
1071 sub_period_bytes_size
= fSubPeriodBytesSize
;
1074 for (int port_index
= 0; port_index
< fNPorts
; port_index
++) {
1075 memcpy(fNetBuffer
+ port_index
* sub_period_bytes_size
, fIntBuffer
[port_index
] + sub_cycle
* fSubPeriodSize
, sub_period_bytes_size
);
1077 return fNPorts
* sub_period_bytes_size
;
1080 // SessionParams ************************************************************************************
1082 SERVER_EXPORT
void SessionParamsHToN(session_params_t
* src_params
, session_params_t
* dst_params
)
1084 memcpy(dst_params
, src_params
, sizeof(session_params_t
));
1085 dst_params
->fProtocolVersion
= htonl(src_params
->fProtocolVersion
);
1086 dst_params
->fPacketID
= htonl(src_params
->fPacketID
);
1087 dst_params
->fMtu
= htonl(src_params
->fMtu
);
1088 dst_params
->fID
= htonl(src_params
->fID
);
1089 dst_params
->fTransportSync
= htonl(src_params
->fTransportSync
);
1090 dst_params
->fSendAudioChannels
= htonl(src_params
->fSendAudioChannels
);
1091 dst_params
->fReturnAudioChannels
= htonl(src_params
->fReturnAudioChannels
);
1092 dst_params
->fSendMidiChannels
= htonl(src_params
->fSendMidiChannels
);
1093 dst_params
->fReturnMidiChannels
= htonl(src_params
->fReturnMidiChannels
);
1094 dst_params
->fSampleRate
= htonl(src_params
->fSampleRate
);
1095 dst_params
->fPeriodSize
= htonl(src_params
->fPeriodSize
);
1096 dst_params
->fSampleEncoder
= htonl(src_params
->fSampleEncoder
);
1097 dst_params
->fKBps
= htonl(src_params
->fKBps
);
1098 dst_params
->fSlaveSyncMode
= htonl(src_params
->fSlaveSyncMode
);
1099 dst_params
->fNetworkLatency
= htonl(src_params
->fNetworkLatency
);
1102 SERVER_EXPORT
void SessionParamsNToH(session_params_t
* src_params
, session_params_t
* dst_params
)
1104 memcpy(dst_params
, src_params
, sizeof(session_params_t
));
1105 dst_params
->fProtocolVersion
= ntohl(src_params
->fProtocolVersion
);
1106 dst_params
->fPacketID
= ntohl(src_params
->fPacketID
);
1107 dst_params
->fMtu
= ntohl(src_params
->fMtu
);
1108 dst_params
->fID
= ntohl(src_params
->fID
);
1109 dst_params
->fTransportSync
= ntohl(src_params
->fTransportSync
);
1110 dst_params
->fSendAudioChannels
= ntohl(src_params
->fSendAudioChannels
);
1111 dst_params
->fReturnAudioChannels
= ntohl(src_params
->fReturnAudioChannels
);
1112 dst_params
->fSendMidiChannels
= ntohl(src_params
->fSendMidiChannels
);
1113 dst_params
->fReturnMidiChannels
= ntohl(src_params
->fReturnMidiChannels
);
1114 dst_params
->fSampleRate
= ntohl(src_params
->fSampleRate
);
1115 dst_params
->fPeriodSize
= ntohl(src_params
->fPeriodSize
);
1116 dst_params
->fSampleEncoder
= ntohl(src_params
->fSampleEncoder
);
1117 dst_params
->fKBps
= ntohl(src_params
->fKBps
);
1118 dst_params
->fSlaveSyncMode
= ntohl(src_params
->fSlaveSyncMode
);
1119 dst_params
->fNetworkLatency
= ntohl(src_params
->fNetworkLatency
);
1122 SERVER_EXPORT
void SessionParamsDisplay(session_params_t
* params
)
1125 switch (params
->fSampleEncoder
)
1127 case JackFloatEncoder
:
1128 strcpy(encoder
, "float");
1130 case JackIntEncoder
:
1131 strcpy(encoder
, "integer");
1133 case JackCeltEncoder
:
1134 strcpy(encoder
, "CELT");
1136 case JackOpusEncoder
:
1137 strcpy(encoder
, "OPUS");
1141 jack_info("**************** Network parameters ****************");
1142 jack_info("Name : %s", params
->fName
);
1143 jack_info("Protocol revision : %d", params
->fProtocolVersion
);
1144 jack_info("MTU : %u", params
->fMtu
);
1145 jack_info("Master name : %s", params
->fMasterNetName
);
1146 jack_info("Slave name : %s", params
->fSlaveNetName
);
1147 jack_info("ID : %u", params
->fID
);
1148 jack_info("Transport Sync : %s", (params
->fTransportSync
) ? "yes" : "no");
1149 jack_info("Send channels (audio - midi) : %d - %d", params
->fSendAudioChannels
, params
->fSendMidiChannels
);
1150 jack_info("Return channels (audio - midi) : %d - %d", params
->fReturnAudioChannels
, params
->fReturnMidiChannels
);
1151 jack_info("Sample rate : %u frames per second", params
->fSampleRate
);
1152 jack_info("Period size : %u frames per period", params
->fPeriodSize
);
1153 jack_info("Network latency : %u cycles", params
->fNetworkLatency
);
1154 switch (params
->fSampleEncoder
) {
1155 case (JackFloatEncoder
):
1156 jack_info("SampleEncoder : %s", "Float");
1158 case (JackIntEncoder
):
1159 jack_info("SampleEncoder : %s", "16 bits integer");
1161 case (JackCeltEncoder
):
1162 jack_info("SampleEncoder : %s", "CELT");
1163 jack_info("kBits : %d", params
->fKBps
);
1165 case (JackOpusEncoder
):
1166 jack_info("SampleEncoder : %s", "OPUS");
1167 jack_info("kBits : %d", params
->fKBps
);
1170 jack_info("Slave mode : %s", (params
->fSlaveSyncMode
) ? "sync" : "async");
1171 jack_info("****************************************************");
1174 SERVER_EXPORT sync_packet_type_t
GetPacketType(session_params_t
* params
)
1176 switch (params
->fPacketID
)
1179 return SLAVE_AVAILABLE
;
1183 return START_MASTER
;
1192 SERVER_EXPORT
int SetPacketType(session_params_t
* params
, sync_packet_type_t packet_type
)
1194 switch (packet_type
)
1198 case SLAVE_AVAILABLE
:
1199 params
->fPacketID
= 0;
1202 params
->fPacketID
= 1;
1205 params
->fPacketID
= 2;
1208 params
->fPacketID
= 3;
1211 params
->fPacketID
= 4;
1216 // Packet header **********************************************************************************
1218 SERVER_EXPORT
void PacketHeaderHToN(packet_header_t
* src_header
, packet_header_t
* dst_header
)
1220 memcpy(dst_header
, src_header
, sizeof(packet_header_t
));
1221 dst_header
->fDataType
= htonl(src_header
->fDataType
);
1222 dst_header
->fDataStream
= htonl(src_header
->fDataStream
);
1223 dst_header
->fID
= htonl(src_header
->fID
);
1224 dst_header
->fNumPacket
= htonl(src_header
->fNumPacket
);
1225 dst_header
->fPacketSize
= htonl(src_header
->fPacketSize
);
1226 dst_header
->fActivePorts
= htonl(src_header
->fActivePorts
);
1227 dst_header
->fCycle
= htonl(src_header
->fCycle
);
1228 dst_header
->fSubCycle
= htonl(src_header
->fSubCycle
);
1229 dst_header
->fFrames
= htonl(src_header
->fFrames
);
1230 dst_header
->fIsLastPckt
= htonl(src_header
->fIsLastPckt
);
1233 SERVER_EXPORT
void PacketHeaderNToH(packet_header_t
* src_header
, packet_header_t
* dst_header
)
1235 memcpy(dst_header
, src_header
, sizeof(packet_header_t
));
1236 dst_header
->fDataType
= ntohl(src_header
->fDataType
);
1237 dst_header
->fDataStream
= ntohl(src_header
->fDataStream
);
1238 dst_header
->fID
= ntohl(src_header
->fID
);
1239 dst_header
->fNumPacket
= ntohl(src_header
->fNumPacket
);
1240 dst_header
->fPacketSize
= ntohl(src_header
->fPacketSize
);
1241 dst_header
->fActivePorts
= ntohl(src_header
->fActivePorts
);
1242 dst_header
->fCycle
= ntohl(src_header
->fCycle
);
1243 dst_header
->fSubCycle
= ntohl(src_header
->fSubCycle
);
1244 dst_header
->fFrames
= ntohl(src_header
->fFrames
);
1245 dst_header
->fIsLastPckt
= ntohl(src_header
->fIsLastPckt
);
1248 SERVER_EXPORT
void PacketHeaderDisplay(packet_header_t
* header
)
1250 jack_info("********************Header********************");
1251 jack_info("Data type : %c", header
->fDataType
);
1252 jack_info("Data stream : %c", header
->fDataStream
);
1253 jack_info("ID : %u", header
->fID
);
1254 jack_info("Cycle : %u", header
->fCycle
);
1255 jack_info("SubCycle : %u", header
->fSubCycle
);
1256 jack_info("Active ports : %u", header
->fActivePorts
);
1257 jack_info("DATA packets : %u", header
->fNumPacket
);
1258 jack_info("DATA size : %u", header
->fPacketSize
);
1259 jack_info("DATA frames : %d", header
->fFrames
);
1260 jack_info("Last packet : '%s'", (header
->fIsLastPckt
) ? "yes" : "no");
1261 jack_info("**********************************************");
1264 SERVER_EXPORT
void NetTransportDataDisplay(net_transport_data_t
* data
)
1266 jack_info("********************Network Transport********************");
1267 jack_info("Transport new state : %u", data
->fNewState
);
1268 jack_info("Transport timebase master : %u", data
->fTimebaseMaster
);
1269 jack_info("Transport cycle state : %u", data
->fState
);
1270 jack_info("**********************************************");
1273 SERVER_EXPORT
void MidiBufferHToN(JackMidiBuffer
* src_buffer
, JackMidiBuffer
* dst_buffer
)
1275 dst_buffer
->magic
= htonl(src_buffer
->magic
);
1276 dst_buffer
->buffer_size
= htonl(src_buffer
->buffer_size
);
1277 dst_buffer
->nframes
= htonl(src_buffer
->nframes
);
1278 dst_buffer
->write_pos
= htonl(src_buffer
->write_pos
);
1279 dst_buffer
->event_count
= htonl(src_buffer
->event_count
);
1280 dst_buffer
->lost_events
= htonl(src_buffer
->lost_events
);
1283 SERVER_EXPORT
void MidiBufferNToH(JackMidiBuffer
* src_buffer
, JackMidiBuffer
* dst_buffer
)
1285 dst_buffer
->magic
= ntohl(src_buffer
->magic
);
1286 dst_buffer
->buffer_size
= ntohl(src_buffer
->buffer_size
);
1287 dst_buffer
->nframes
= ntohl(src_buffer
->nframes
);
1288 dst_buffer
->write_pos
= ntohl(src_buffer
->write_pos
);
1289 dst_buffer
->event_count
= ntohl(src_buffer
->event_count
);
1290 dst_buffer
->lost_events
= ntohl(src_buffer
->lost_events
);
1293 SERVER_EXPORT
void TransportDataHToN(net_transport_data_t
* src_params
, net_transport_data_t
* dst_params
)
1295 dst_params
->fNewState
= htonl(src_params
->fNewState
);
1296 dst_params
->fTimebaseMaster
= htonl(src_params
->fTimebaseMaster
);
1297 dst_params
->fState
= htonl(src_params
->fState
);
1298 dst_params
->fPosition
.unique_1
= htonll(src_params
->fPosition
.unique_1
);
1299 dst_params
->fPosition
.usecs
= htonl(src_params
->fPosition
.usecs
);
1300 dst_params
->fPosition
.frame_rate
= htonl(src_params
->fPosition
.frame_rate
);
1301 dst_params
->fPosition
.frame
= htonl(src_params
->fPosition
.frame
);
1302 dst_params
->fPosition
.valid
= (jack_position_bits_t
)htonl((uint32_t)src_params
->fPosition
.valid
);
1303 dst_params
->fPosition
.bar
= htonl(src_params
->fPosition
.bar
);
1304 dst_params
->fPosition
.beat
= htonl(src_params
->fPosition
.beat
);
1305 dst_params
->fPosition
.tick
= htonl(src_params
->fPosition
.tick
);
1306 dst_params
->fPosition
.bar_start_tick
= htonll((uint64_t)src_params
->fPosition
.bar_start_tick
);
1307 dst_params
->fPosition
.beats_per_bar
= htonl((uint32_t)src_params
->fPosition
.beats_per_bar
);
1308 dst_params
->fPosition
.beat_type
= htonl((uint32_t)src_params
->fPosition
.beat_type
);
1309 dst_params
->fPosition
.ticks_per_beat
= htonll((uint64_t)src_params
->fPosition
.ticks_per_beat
);
1310 dst_params
->fPosition
.beats_per_minute
= htonll((uint64_t)src_params
->fPosition
.beats_per_minute
);
1311 dst_params
->fPosition
.frame_time
= htonll((uint64_t)src_params
->fPosition
.frame_time
);
1312 dst_params
->fPosition
.next_time
= htonll((uint64_t)src_params
->fPosition
.next_time
);
1313 dst_params
->fPosition
.bbt_offset
= htonl(src_params
->fPosition
.bbt_offset
);
1314 dst_params
->fPosition
.audio_frames_per_video_frame
= htonl((uint32_t)src_params
->fPosition
.audio_frames_per_video_frame
);
1315 dst_params
->fPosition
.video_offset
= htonl(src_params
->fPosition
.video_offset
);
1316 dst_params
->fPosition
.unique_2
= htonll(src_params
->fPosition
.unique_2
);
1319 SERVER_EXPORT
void TransportDataNToH(net_transport_data_t
* src_params
, net_transport_data_t
* dst_params
)
1321 dst_params
->fNewState
= ntohl(src_params
->fNewState
);
1322 dst_params
->fTimebaseMaster
= ntohl(src_params
->fTimebaseMaster
);
1323 dst_params
->fState
= ntohl(src_params
->fState
);
1324 dst_params
->fPosition
.unique_1
= ntohll(src_params
->fPosition
.unique_1
);
1325 dst_params
->fPosition
.usecs
= ntohl(src_params
->fPosition
.usecs
);
1326 dst_params
->fPosition
.frame_rate
= ntohl(src_params
->fPosition
.frame_rate
);
1327 dst_params
->fPosition
.frame
= ntohl(src_params
->fPosition
.frame
);
1328 dst_params
->fPosition
.valid
= (jack_position_bits_t
)ntohl((uint32_t)src_params
->fPosition
.valid
);
1329 dst_params
->fPosition
.bar
= ntohl(src_params
->fPosition
.bar
);
1330 dst_params
->fPosition
.beat
= ntohl(src_params
->fPosition
.beat
);
1331 dst_params
->fPosition
.tick
= ntohl(src_params
->fPosition
.tick
);
1332 dst_params
->fPosition
.bar_start_tick
= ntohll((uint64_t)src_params
->fPosition
.bar_start_tick
);
1333 dst_params
->fPosition
.beats_per_bar
= ntohl((uint32_t)src_params
->fPosition
.beats_per_bar
);
1334 dst_params
->fPosition
.beat_type
= ntohl((uint32_t)src_params
->fPosition
.beat_type
);
1335 dst_params
->fPosition
.ticks_per_beat
= ntohll((uint64_t)src_params
->fPosition
.ticks_per_beat
);
1336 dst_params
->fPosition
.beats_per_minute
= ntohll((uint64_t)src_params
->fPosition
.beats_per_minute
);
1337 dst_params
->fPosition
.frame_time
= ntohll((uint64_t)src_params
->fPosition
.frame_time
);
1338 dst_params
->fPosition
.next_time
= ntohll((uint64_t)src_params
->fPosition
.next_time
);
1339 dst_params
->fPosition
.bbt_offset
= ntohl(src_params
->fPosition
.bbt_offset
);
1340 dst_params
->fPosition
.audio_frames_per_video_frame
= ntohl((uint32_t)src_params
->fPosition
.audio_frames_per_video_frame
);
1341 dst_params
->fPosition
.video_offset
= ntohl(src_params
->fPosition
.video_offset
);
1342 dst_params
->fPosition
.unique_2
= ntohll(src_params
->fPosition
.unique_2
);
1345 // Utility *******************************************************************************************************
1347 SERVER_EXPORT
int SocketAPIInit()
1350 WORD wVersionRequested
= MAKEWORD(2, 2);
1353 if (WSAStartup(wVersionRequested
, &wsaData
) != 0) {
1354 jack_error("WSAStartup error : %s", strerror(NET_ERROR_CODE
));
1358 if (LOBYTE(wsaData
.wVersion
) != 2 || HIBYTE(wsaData
.wVersion
) != 2) {
1359 jack_error("Could not find a useable version of Winsock.dll\n");
1367 SERVER_EXPORT
int SocketAPIEnd()
1370 return WSACleanup();
1375 SERVER_EXPORT
const char* GetTransportState(int transport_state
)
1377 switch (transport_state
)
1379 case JackTransportRolling
:
1381 case JackTransportStarting
:
1383 case JackTransportStopped
:
1385 case JackTransportNetStarting
:
1386 return "netstarting";