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
;
712 NetIntAudioBuffer::NetIntAudioBuffer(session_params_t
* params
, uint32_t nports
, char* net_buffer
)
713 : NetAudioBuffer(params
, nports
, net_buffer
)
715 fPeriodSize
= params
->fPeriodSize
;
717 fCompressedSizeByte
= (params
->fPeriodSize
* sizeof(short));
718 jack_log("NetIntAudioBuffer fCompressedSizeByte %d", fCompressedSizeByte
);
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));
726 int res1
= (fNPorts
* fCompressedSizeByte
) % PACKET_AVAILABLE_SIZE(params
);
727 int res2
= (fNPorts
* fCompressedSizeByte
) / PACKET_AVAILABLE_SIZE(params
);
729 jack_log("NetIntAudioBuffer res1 = %d res2 = %d", res1
, res2
);
731 fNumPackets
= (res1
) ? (res2
+ 1) : res2
;
733 fSubPeriodBytesSize
= fCompressedSizeByte
/ fNumPackets
;
734 fLastSubPeriodBytesSize
= fSubPeriodBytesSize
+ fCompressedSizeByte
% fNumPackets
;
736 fSubPeriodSize
= fSubPeriodBytesSize
/ sizeof(short);
738 jack_log("NetIntAudioBuffer fNumPackets = %d fSubPeriodBytesSize = %d, fLastSubPeriodBytesSize = %d", fNumPackets
, fSubPeriodBytesSize
, fLastSubPeriodBytesSize
);
740 fCycleDuration
= float(fSubPeriodBytesSize
/ sizeof(sample_t
)) / float(params
->fSampleRate
);
741 fCycleBytesSize
= params
->fMtu
* fNumPackets
;
747 NetIntAudioBuffer::~NetIntAudioBuffer()
749 for (int port_index
= 0; port_index
< fNPorts
; port_index
++) {
750 delete [] fIntBuffer
[port_index
];
753 delete [] fIntBuffer
;
756 size_t NetIntAudioBuffer::GetCycleSize()
758 return fCycleBytesSize
;
761 float NetIntAudioBuffer::GetCycleDuration()
763 return fCycleDuration
;
766 int NetIntAudioBuffer::GetNumPackets(int active_ports
)
771 int NetIntAudioBuffer::RenderFromJackPorts()
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
);
785 void NetIntAudioBuffer::RenderToJackPorts()
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
);
800 int NetIntAudioBuffer::RenderFromNetwork(int cycle
, int sub_cycle
, uint32_t port_num
)
802 // Cleanup all JACK ports at the beginning of the cycle
803 if (sub_cycle
== 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
);
813 for (int port_index
= 0; port_index
< fNPorts
; port_index
++) {
814 memcpy(fIntBuffer
[port_index
] + sub_cycle
* fSubPeriodSize
, fNetBuffer
+ port_index
* fSubPeriodBytesSize
, fSubPeriodBytesSize
);
819 return CheckPacket(cycle
, sub_cycle
);
822 int NetIntAudioBuffer::RenderToNetwork(int sub_cycle
, uint32_t port_num
)
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
);
829 return fNPorts
* fLastSubPeriodBytesSize
;
831 for (int port_index
= 0; port_index
< fNPorts
; port_index
++) {
832 memcpy(fNetBuffer
+ port_index
* fSubPeriodBytesSize
, fIntBuffer
[port_index
] + sub_cycle
* fSubPeriodSize
, fSubPeriodBytesSize
);
834 return fNPorts
* fSubPeriodBytesSize
;
838 // SessionParams ************************************************************************************
840 SERVER_EXPORT
void SessionParamsHToN(session_params_t
* src_params
, session_params_t
* dst_params
)
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
);
859 SERVER_EXPORT
void SessionParamsNToH(session_params_t
* src_params
, session_params_t
* dst_params
)
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
);
878 SERVER_EXPORT
void SessionParamsDisplay(session_params_t
* params
)
881 switch (params
->fSampleEncoder
)
883 case JackFloatEncoder
:
884 strcpy(encoder
, "float");
887 strcpy(encoder
, "integer");
889 case JackCeltEncoder
:
890 strcpy(encoder
, "CELT");
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");
911 case (JackIntEncoder
):
912 jack_info("SampleEncoder : %s", "16 bits integer");
914 case (JackCeltEncoder
):
915 jack_info("SampleEncoder : %s", "CELT");
916 jack_info("kBits : %d", params
->fKBps
);
919 jack_info("Slave mode : %s", (params
->fSlaveSyncMode
) ? "sync" : "async");
920 jack_info("****************************************************");
923 SERVER_EXPORT sync_packet_type_t
GetPacketType(session_params_t
* params
)
925 switch (params
->fPacketID
)
928 return SLAVE_AVAILABLE
;
941 SERVER_EXPORT
int SetPacketType(session_params_t
* params
, sync_packet_type_t packet_type
)
947 case SLAVE_AVAILABLE
:
948 params
->fPacketID
= 0;
951 params
->fPacketID
= 1;
954 params
->fPacketID
= 2;
957 params
->fPacketID
= 3;
960 params
->fPacketID
= 4;
965 // Packet header **********************************************************************************
967 SERVER_EXPORT
void PacketHeaderHToN(packet_header_t
* src_header
, packet_header_t
* dst_header
)
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
);
979 SERVER_EXPORT
void PacketHeaderNToH(packet_header_t
* src_header
, packet_header_t
* dst_header
)
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
);
991 SERVER_EXPORT
void PacketHeaderDisplay(packet_header_t
* header
)
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("**********************************************");
1008 SERVER_EXPORT
void NetTransportDataDisplay(net_transport_data_t
* data
)
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("**********************************************");
1017 SERVER_EXPORT
void MidiBufferHToN(JackMidiBuffer
* src_buffer
, JackMidiBuffer
* dst_buffer
)
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
);
1028 SERVER_EXPORT
void MidiBufferNToH(JackMidiBuffer
* src_buffer
, JackMidiBuffer
* dst_buffer
)
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
);
1039 SERVER_EXPORT
void TransportDataHToN(net_transport_data_t
* src_params
, net_transport_data_t
* dst_params
)
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
);
1065 SERVER_EXPORT
void TransportDataNToH(net_transport_data_t
* src_params
, net_transport_data_t
* dst_params
)
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
);
1091 // Utility *******************************************************************************************************
1093 SERVER_EXPORT
int SocketAPIInit()
1096 WORD wVersionRequested
= MAKEWORD(2, 2);
1099 if (WSAStartup(wVersionRequested
, &wsaData
) != 0) {
1100 jack_error("WSAStartup error : %s", strerror(NET_ERROR_CODE
));
1104 if (LOBYTE(wsaData
.wVersion
) != 2 || HIBYTE(wsaData
.wVersion
) != 2) {
1105 jack_error("Could not find a useable version of Winsock.dll\n");
1113 SERVER_EXPORT
int SocketAPIEnd()
1116 return WSACleanup();
1121 SERVER_EXPORT
const char* GetTransportState(int transport_state
)
1123 switch (transport_state
)
1125 case JackTransportRolling
:
1127 case JackTransportStarting
:
1129 case JackTransportStopped
:
1131 case JackTransportNetStarting
:
1132 return "netstarting";