00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "JackNetTool.h"
00021
00022 #ifdef __APPLE__
00023
00024 #include <mach/mach_time.h>
00025
00026 class HardwareClock
00027 {
00028 public:
00029
00030 HardwareClock();
00031
00032 void Reset();
00033 void Update();
00034
00035 float GetDeltaTime() const;
00036 double GetTime() const;
00037
00038 private:
00039
00040 double m_clockToSeconds;
00041
00042 uint64_t m_startAbsTime;
00043 uint64_t m_lastAbsTime;
00044
00045 double m_time;
00046 float m_deltaTime;
00047 };
00048
00049 HardwareClock::HardwareClock()
00050 {
00051 mach_timebase_info_data_t info;
00052 mach_timebase_info(&info);
00053 m_clockToSeconds = (double)info.numer/info.denom/1000000000.0;
00054 Reset();
00055 }
00056
00057 void HardwareClock::Reset()
00058 {
00059 m_startAbsTime = mach_absolute_time();
00060 m_lastAbsTime = m_startAbsTime;
00061 m_time = m_startAbsTime*m_clockToSeconds;
00062 m_deltaTime = 1.0f/60.0f;
00063 }
00064
00065 void HardwareClock::Update()
00066 {
00067 const uint64_t currentTime = mach_absolute_time();
00068 const uint64_t dt = currentTime - m_lastAbsTime;
00069
00070 m_time = currentTime*m_clockToSeconds;
00071 m_deltaTime = (double)dt*m_clockToSeconds;
00072 m_lastAbsTime = currentTime;
00073 }
00074
00075 float HardwareClock::GetDeltaTime() const
00076 {
00077 return m_deltaTime;
00078 }
00079
00080 double HardwareClock::GetTime() const
00081 {
00082 return m_time;
00083 }
00084
00085 #endif
00086
00087 using namespace std;
00088
00089 namespace Jack
00090 {
00091
00092
00093 NetMidiBuffer::NetMidiBuffer(session_params_t* params, uint32_t nports, char* net_buffer)
00094 {
00095 fNPorts = nports;
00096 fMaxBufsize = fNPorts * sizeof(sample_t) * params->fPeriodSize ;
00097 fMaxPcktSize = params->fMtu - sizeof(packet_header_t);
00098 fBuffer = new char[fMaxBufsize];
00099 fPortBuffer = new JackMidiBuffer* [fNPorts];
00100 for (int port_index = 0; port_index < fNPorts; port_index++) {
00101 fPortBuffer[port_index] = NULL;
00102 }
00103 fNetBuffer = net_buffer;
00104
00105 fCycleBytesSize = params->fMtu
00106 * (max(params->fSendMidiChannels, params->fReturnMidiChannels)
00107 * params->fPeriodSize * sizeof(sample_t) / (params->fMtu - sizeof(packet_header_t)));
00108 }
00109
00110 NetMidiBuffer::~NetMidiBuffer()
00111 {
00112 delete[] fBuffer;
00113 delete[] fPortBuffer;
00114 }
00115
00116 size_t NetMidiBuffer::GetCycleSize()
00117 {
00118 return fCycleBytesSize;
00119 }
00120
00121 int NetMidiBuffer::GetNumPackets(int data_size, int max_size)
00122 {
00123 int res1 = data_size % max_size;
00124 int res2 = data_size / max_size;
00125 return (res1) ? res2 + 1 : res2;
00126 }
00127
00128 void NetMidiBuffer::SetBuffer(int index, JackMidiBuffer* buffer)
00129 {
00130 fPortBuffer[index] = buffer;
00131 }
00132
00133 JackMidiBuffer* NetMidiBuffer::GetBuffer(int index)
00134 {
00135 return fPortBuffer[index];
00136 }
00137
00138 void NetMidiBuffer::DisplayEvents()
00139 {
00140 for (int port_index = 0; port_index < fNPorts; port_index++) {
00141 for (uint event = 0; event < fPortBuffer[port_index]->event_count; event++) {
00142 if (fPortBuffer[port_index]->IsValid()) {
00143 jack_info("port %d : midi event %u/%u -> time : %u, size : %u",
00144 port_index + 1, event + 1, fPortBuffer[port_index]->event_count,
00145 fPortBuffer[port_index]->events[event].time, fPortBuffer[port_index]->events[event].size);
00146 }
00147 }
00148 }
00149 }
00150
00151 int NetMidiBuffer::RenderFromJackPorts()
00152 {
00153 int pos = 0;
00154 size_t copy_size;
00155
00156 for (int port_index = 0; port_index < fNPorts; port_index++) {
00157 char* write_pos = fBuffer + pos;
00158 copy_size = sizeof(JackMidiBuffer) + fPortBuffer[port_index]->event_count * sizeof(JackMidiEvent);
00159 memcpy(fBuffer + pos, fPortBuffer[port_index], copy_size);
00160 pos += copy_size;
00161 memcpy(fBuffer + pos,
00162 fPortBuffer[port_index] + (fPortBuffer[port_index]->buffer_size - fPortBuffer[port_index]->write_pos),
00163 fPortBuffer[port_index]->write_pos);
00164 pos += fPortBuffer[port_index]->write_pos;
00165
00166 JackMidiBuffer* midi_buffer = reinterpret_cast<JackMidiBuffer*>(write_pos);
00167 MidiBufferHToN(midi_buffer, midi_buffer);
00168 }
00169 return pos;
00170 }
00171
00172 void NetMidiBuffer::RenderToJackPorts()
00173 {
00174 int pos = 0;
00175 size_t copy_size;
00176
00177 for (int port_index = 0; port_index < fNPorts; port_index++) {
00178 JackMidiBuffer* midi_buffer = reinterpret_cast<JackMidiBuffer*>(fBuffer + pos);
00179 MidiBufferNToH(midi_buffer, midi_buffer);
00180 copy_size = sizeof(JackMidiBuffer) + reinterpret_cast<JackMidiBuffer*>(fBuffer + pos)->event_count * sizeof(JackMidiEvent);
00181 memcpy(fPortBuffer[port_index], fBuffer + pos, copy_size);
00182 pos += copy_size;
00183 memcpy(fPortBuffer[port_index] + (fPortBuffer[port_index]->buffer_size - fPortBuffer[port_index]->write_pos),
00184 fBuffer + pos,
00185 fPortBuffer[port_index]->write_pos);
00186 pos += fPortBuffer[port_index]->write_pos;
00187 }
00188 }
00189
00190 void NetMidiBuffer::RenderFromNetwork(int sub_cycle, size_t copy_size)
00191 {
00192 memcpy(fBuffer + sub_cycle * fMaxPcktSize, fNetBuffer, copy_size);
00193 }
00194
00195 int NetMidiBuffer::RenderToNetwork(int sub_cycle, size_t total_size)
00196 {
00197 int size = total_size - sub_cycle * fMaxPcktSize;
00198 int copy_size = (size <= fMaxPcktSize) ? size : fMaxPcktSize;
00199 memcpy(fNetBuffer, fBuffer + sub_cycle * fMaxPcktSize, copy_size);
00200 return copy_size;
00201 }
00202
00203
00204
00205 NetAudioBuffer::NetAudioBuffer(session_params_t* params, uint32_t nports, char* net_buffer)
00206 {
00207 fNPorts = nports;
00208 fNetBuffer = net_buffer;
00209
00210 fPortBuffer = new sample_t* [fNPorts];
00211 fConnectedPorts = new bool[fNPorts];
00212 for (int port_index = 0; port_index < fNPorts; port_index++) {
00213 fPortBuffer[port_index] = NULL;
00214 fConnectedPorts[port_index] = true;
00215 }
00216 }
00217
00218 NetAudioBuffer::~NetAudioBuffer()
00219 {
00220 delete [] fConnectedPorts;
00221 delete [] fPortBuffer;
00222 }
00223
00224 void NetAudioBuffer::SetBuffer(int index, sample_t* buffer)
00225 {
00226 fPortBuffer[index] = buffer;
00227 }
00228
00229 sample_t* NetAudioBuffer::GetBuffer(int index)
00230 {
00231 return fPortBuffer[index];
00232 }
00233
00234 int NetAudioBuffer::CheckPacket(int cycle, int sub_cycle)
00235 {
00236 int res;
00237
00238 if (sub_cycle != fLastSubCycle + 1) {
00239 jack_error("Packet(s) missing from... %d %d", fLastSubCycle, sub_cycle);
00240 res = NET_PACKET_ERROR;
00241 } else {
00242 res = 0;
00243 }
00244
00245 fLastSubCycle = sub_cycle;
00246 return res;
00247 }
00248
00249 void NetAudioBuffer::NextCycle()
00250 {
00251
00252 fLastSubCycle = -1;
00253 }
00254
00255 void NetAudioBuffer::Cleanup()
00256 {
00257 for (int port_index = 0; port_index < fNPorts; port_index++) {
00258 if (fPortBuffer[port_index]) {
00259 memset(fPortBuffer[port_index], 0, fPeriodSize * sizeof(sample_t));
00260 }
00261 }
00262 }
00263
00264
00265
00266 int NetAudioBuffer::ActivePortsToNetwork(char* net_buffer)
00267 {
00268 int active_ports = 0;
00269 int* active_port_address = (int*)net_buffer;
00270
00271 for (int port_index = 0; port_index < fNPorts; port_index++) {
00272
00273 if (fPortBuffer[port_index]) {
00274 *active_port_address = htonl(port_index);
00275 active_port_address++;
00276 active_ports++;
00277 assert(active_ports < 256);
00278 }
00279 }
00280
00281 return active_ports;
00282 }
00283
00284 void NetAudioBuffer::ActivePortsFromNetwork(char* net_buffer, uint32_t port_num)
00285 {
00286 int* active_port_address = (int*)net_buffer;
00287
00288 for (int port_index = 0; port_index < fNPorts; port_index++) {
00289 fConnectedPorts[port_index] = false;
00290 }
00291
00292 for (uint port_index = 0; port_index < port_num; port_index++) {
00293
00294 int active_port = ntohl(*active_port_address);
00295 if (active_port >= 0 && active_port < fNPorts) {
00296 fConnectedPorts[active_port] = true;
00297 } else {
00298 jack_error("ActivePortsFromNetwork: incorrect port = %d", active_port);
00299 }
00300 active_port_address++;
00301 }
00302 }
00303
00304 int NetAudioBuffer::RenderFromJackPorts()
00305 {
00306
00307 int active_ports = 0;
00308 for (int port_index = 0; port_index < fNPorts; port_index++) {
00309
00310 if (fPortBuffer[port_index]) {
00311 active_ports++;
00312 }
00313 }
00314
00315 return active_ports;
00316 }
00317
00318 void NetAudioBuffer::RenderToJackPorts()
00319 {
00320
00321 NextCycle();
00322 }
00323
00324
00325
00326 NetFloatAudioBuffer::NetFloatAudioBuffer(session_params_t* params, uint32_t nports, char* net_buffer)
00327 : NetAudioBuffer(params, nports, net_buffer)
00328 {
00329 fPeriodSize = params->fPeriodSize;
00330 fPacketSize = PACKET_AVAILABLE_SIZE(params);
00331
00332 UpdateParams(max(params->fReturnAudioChannels, params->fSendAudioChannels));
00333
00334 fSubPeriodBytesSize = fSubPeriodSize * sizeof(sample_t);
00335
00336 fCycleDuration = float(fSubPeriodSize) / float(params->fSampleRate);
00337 fCycleBytesSize = params->fMtu * (fPeriodSize / fSubPeriodSize);
00338
00339 fLastSubCycle = -1;
00340 }
00341
00342 NetFloatAudioBuffer::~NetFloatAudioBuffer()
00343 {}
00344
00345
00346 size_t NetFloatAudioBuffer::GetCycleSize()
00347 {
00348 return fCycleBytesSize;
00349 }
00350
00351
00352 float NetFloatAudioBuffer::GetCycleDuration()
00353 {
00354 return fCycleDuration;
00355 }
00356
00357 void NetFloatAudioBuffer::UpdateParams(int active_ports)
00358 {
00359 if (active_ports == 0) {
00360 fSubPeriodSize = fPeriodSize;
00361 } else {
00362 jack_nframes_t period = (int) powf(2.f, (int)(log(float(fPacketSize) / (active_ports * sizeof(sample_t))) / log(2.)));
00363 fSubPeriodSize = (period > fPeriodSize) ? fPeriodSize : period;
00364 }
00365
00366 fSubPeriodBytesSize = fSubPeriodSize * sizeof(sample_t) + sizeof(int);
00367 }
00368
00369 int NetFloatAudioBuffer::GetNumPackets(int active_ports)
00370 {
00371 UpdateParams(active_ports);
00372
00373
00374
00375
00376
00377 return fPeriodSize / fSubPeriodSize;
00378 }
00379
00380
00381
00382 int NetFloatAudioBuffer::RenderFromNetwork(int cycle, int sub_cycle, uint32_t port_num)
00383 {
00384
00385 if (sub_cycle == 0) {
00386 Cleanup();
00387 }
00388
00389 if (port_num > 0) {
00390 UpdateParams(port_num);
00391 for (uint32_t port_index = 0; port_index < port_num; port_index++) {
00392
00393 int* active_port_address = (int*)(fNetBuffer + port_index * fSubPeriodBytesSize);
00394 int active_port = ntohl(*active_port_address);
00395 RenderFromNetwork((char*)(active_port_address + 1), active_port, sub_cycle);
00396 }
00397 }
00398
00399 return CheckPacket(cycle, sub_cycle);
00400 }
00401
00402
00403 int NetFloatAudioBuffer::RenderToNetwork(int sub_cycle, uint32_t port_num)
00404 {
00405 int active_ports = 0;
00406
00407 for (int port_index = 0; port_index < fNPorts; port_index++) {
00408
00409 if (fPortBuffer[port_index]) {
00410 int* active_port_address = (int*)(fNetBuffer + active_ports * fSubPeriodBytesSize);
00411 *active_port_address = htonl(port_index);
00412 RenderToNetwork((char*)(active_port_address + 1), port_index, sub_cycle);
00413 active_ports++;
00414 }
00415 }
00416
00417 return port_num * fSubPeriodBytesSize;
00418 }
00419
00420 #ifdef __BIG_ENDIAN__
00421
00422 static inline jack_default_audio_sample_t SwapFloat(jack_default_audio_sample_t f)
00423 {
00424 union
00425 {
00426 jack_default_audio_sample_t f;
00427 unsigned char b[4];
00428 } dat1, dat2;
00429
00430 dat1.f = f;
00431 dat2.b[0] = dat1.b[3];
00432 dat2.b[1] = dat1.b[2];
00433 dat2.b[2] = dat1.b[1];
00434 dat2.b[3] = dat1.b[0];
00435 return dat2.f;
00436 }
00437
00438 void NetFloatAudioBuffer::RenderFromNetwork(char* net_buffer, int active_port, int sub_cycle)
00439 {
00440 if (fPortBuffer[active_port]) {
00441 jack_default_audio_sample_t* src = (jack_default_audio_sample_t*)(net_buffer);
00442 jack_default_audio_sample_t* dst = (jack_default_audio_sample_t*)(fPortBuffer[active_port] + sub_cycle * fSubPeriodSize);
00443 for (unsigned int sample = 0; sample < (fSubPeriodBytesSize - sizeof(int)) / sizeof(jack_default_audio_sample_t); sample++) {
00444 dst[sample] = SwapFloat(src[sample]);
00445 }
00446 }
00447 }
00448
00449 void NetFloatAudioBuffer::RenderToNetwork(char* net_buffer, int active_port, int sub_cycle)
00450 {
00451 for (int port_index = 0; port_index < fNPorts; port_index++ ) {
00452 jack_default_audio_sample_t* src = (jack_default_audio_sample_t*)(fPortBuffer[active_port] + sub_cycle * fSubPeriodSize);
00453 jack_default_audio_sample_t* dst = (jack_default_audio_sample_t*)(net_buffer);
00454 for (unsigned int sample = 0; sample < (fSubPeriodBytesSize - sizeof(int)) / sizeof(jack_default_audio_sample_t); sample++) {
00455 dst[sample] = SwapFloat(src[sample]);
00456 }
00457 }
00458 }
00459
00460 #else
00461
00462 void NetFloatAudioBuffer::RenderFromNetwork(char* net_buffer, int active_port, int sub_cycle)
00463 {
00464 if (fPortBuffer[active_port]) {
00465 memcpy(fPortBuffer[active_port] + sub_cycle * fSubPeriodSize, net_buffer, fSubPeriodBytesSize - sizeof(int));
00466 }
00467 }
00468
00469 void NetFloatAudioBuffer::RenderToNetwork(char* net_buffer, int active_port, int sub_cycle)
00470 {
00471 memcpy(net_buffer, fPortBuffer[active_port] + sub_cycle * fSubPeriodSize, fSubPeriodBytesSize - sizeof(int));
00472 }
00473
00474 #endif
00475
00476
00477 #if HAVE_CELT
00478
00479 #define KPS 32
00480 #define KPS_DIV 8
00481
00482 NetCeltAudioBuffer::NetCeltAudioBuffer(session_params_t* params, uint32_t nports, char* net_buffer, int kbps)
00483 :NetAudioBuffer(params, nports, net_buffer)
00484 {
00485 fCeltMode = new CELTMode *[fNPorts];
00486 fCeltEncoder = new CELTEncoder *[fNPorts];
00487 fCeltDecoder = new CELTDecoder *[fNPorts];
00488
00489 memset(fCeltMode, 0, fNPorts * sizeof(CELTMode*));
00490 memset(fCeltEncoder, 0, fNPorts * sizeof(CELTEncoder*));
00491 memset(fCeltDecoder, 0, fNPorts * sizeof(CELTDecoder*));
00492
00493 int error = CELT_OK;
00494
00495 for (int i = 0; i < fNPorts; i++) {
00496 fCeltMode[i] = celt_mode_create(params->fSampleRate, params->fPeriodSize, &error);
00497 if (error != CELT_OK) {
00498 goto error;
00499 }
00500
00501 #if HAVE_CELT_API_0_11
00502
00503 fCeltEncoder[i] = celt_encoder_create_custom(fCeltMode[i], 1, &error);
00504 if (error != CELT_OK) {
00505 goto error;
00506 }
00507 celt_encoder_ctl(fCeltEncoder[i], CELT_SET_COMPLEXITY(1));
00508
00509 fCeltDecoder[i] = celt_decoder_create_custom(fCeltMode[i], 1, &error);
00510 if (error != CELT_OK) {
00511 goto error;
00512 }
00513 celt_decoder_ctl(fCeltDecoder[i], CELT_SET_COMPLEXITY(1));
00514
00515 #elif HAVE_CELT_API_0_7 || HAVE_CELT_API_0_8
00516
00517 fCeltEncoder[i] = celt_encoder_create(fCeltMode[i], 1, &error);
00518 if (error != CELT_OK) {
00519 goto error;
00520 }
00521 celt_encoder_ctl(fCeltEncoder[i], CELT_SET_COMPLEXITY(1));
00522
00523 fCeltDecoder[i] = celt_decoder_create(fCeltMode[i], 1, &error);
00524 if (error != CELT_OK) {
00525 goto error;
00526 }
00527 celt_decoder_ctl(fCeltDecoder[i], CELT_SET_COMPLEXITY(1));
00528
00529 #else
00530
00531 fCeltEncoder[i] = celt_encoder_create(fCeltMode[i]);
00532 if (error != CELT_OK) {
00533 goto error;
00534 }
00535 celt_encoder_ctl(fCeltEncoder[i], CELT_SET_COMPLEXITY(1));
00536
00537 fCeltDecoder[i] = celt_decoder_create(fCeltMode[i]);
00538 if (error != CELT_OK) {
00539 goto error;
00540 }
00541 celt_decoder_ctl(fCeltDecoder[i], CELT_SET_COMPLEXITY(1));
00542
00543 #endif
00544 }
00545
00546 {
00547 fPeriodSize = params->fPeriodSize;
00548
00549 fCompressedSizeByte = (kbps * params->fPeriodSize * 1024) / (params->fSampleRate * 8);
00550 jack_log("NetCeltAudioBuffer fCompressedSizeByte %d", fCompressedSizeByte);
00551
00552 fCompressedBuffer = new unsigned char* [fNPorts];
00553 for (int port_index = 0; port_index < fNPorts; port_index++) {
00554 fCompressedBuffer[port_index] = new unsigned char[fCompressedSizeByte];
00555 memset(fCompressedBuffer[port_index], 0, fCompressedSizeByte * sizeof(char));
00556 }
00557
00558 int res1 = (fNPorts * fCompressedSizeByte) % PACKET_AVAILABLE_SIZE(params);
00559 int res2 = (fNPorts * fCompressedSizeByte) / PACKET_AVAILABLE_SIZE(params);
00560
00561 fNumPackets = (res1) ? (res2 + 1) : res2;
00562
00563 jack_log("NetCeltAudioBuffer res1 = %d res2 = %d", res1, res2);
00564
00565 fSubPeriodBytesSize = fCompressedSizeByte / fNumPackets;
00566 fLastSubPeriodBytesSize = fSubPeriodBytesSize + fCompressedSizeByte % fNumPackets;
00567
00568 jack_log("NetCeltAudioBuffer fNumPackets = %d fSubPeriodBytesSize = %d, fLastSubPeriodBytesSize = %d", fNumPackets, fSubPeriodBytesSize, fLastSubPeriodBytesSize);
00569
00570 fCycleDuration = float(fSubPeriodBytesSize / sizeof(sample_t)) / float(params->fSampleRate);
00571 fCycleBytesSize = params->fMtu * fNumPackets;
00572
00573 fLastSubCycle = -1;
00574 return;
00575 }
00576
00577 error:
00578
00579 FreeCelt();
00580 throw std::bad_alloc();
00581 }
00582
00583 NetCeltAudioBuffer::~NetCeltAudioBuffer()
00584 {
00585 FreeCelt();
00586
00587 for (int port_index = 0; port_index < fNPorts; port_index++) {
00588 delete [] fCompressedBuffer[port_index];
00589 }
00590
00591 delete [] fCompressedBuffer;
00592 }
00593
00594 void NetCeltAudioBuffer::FreeCelt()
00595 {
00596 for (int i = 0; i < fNPorts; i++) {
00597 if (fCeltEncoder[i]) {
00598 celt_encoder_destroy(fCeltEncoder[i]);
00599 }
00600 if (fCeltDecoder[i]) {
00601 celt_decoder_destroy(fCeltDecoder[i]);
00602 }
00603 if (fCeltMode[i]) {
00604 celt_mode_destroy(fCeltMode[i]);
00605 }
00606 }
00607
00608 delete [] fCeltMode;
00609 delete [] fCeltEncoder;
00610 delete [] fCeltDecoder;
00611 }
00612
00613 size_t NetCeltAudioBuffer::GetCycleSize()
00614 {
00615 return fCycleBytesSize;
00616 }
00617
00618 float NetCeltAudioBuffer::GetCycleDuration()
00619 {
00620 return fCycleDuration;
00621 }
00622
00623 int NetCeltAudioBuffer::GetNumPackets(int active_ports)
00624 {
00625 return fNumPackets;
00626 }
00627
00628 int NetCeltAudioBuffer::RenderFromJackPorts()
00629 {
00630 float buffer[fPeriodSize];
00631
00632 for (int port_index = 0; port_index < fNPorts; port_index++) {
00633 if (fPortBuffer[port_index]) {
00634 memcpy(buffer, fPortBuffer[port_index], fPeriodSize * sizeof(sample_t));
00635 } else {
00636 memset(buffer, 0, fPeriodSize * sizeof(sample_t));
00637 }
00638 #if HAVE_CELT_API_0_8 || HAVE_CELT_API_0_11
00639 int res = celt_encode_float(fCeltEncoder[port_index], buffer, fPeriodSize, fCompressedBuffer[port_index], fCompressedSizeByte);
00640 #else
00641 int res = celt_encode_float(fCeltEncoder[port_index], buffer, NULL, fCompressedBuffer[port_index], fCompressedSizeByte);
00642 #endif
00643 if (res != fCompressedSizeByte) {
00644 jack_error("celt_encode_float error fCompressedSizeByte = %d res = %d", fCompressedSizeByte, res);
00645 }
00646 }
00647
00648
00649 return fNPorts;
00650 }
00651
00652 void NetCeltAudioBuffer::RenderToJackPorts()
00653 {
00654 for (int port_index = 0; port_index < fNPorts; port_index++) {
00655 if (fPortBuffer[port_index]) {
00656 #if HAVE_CELT_API_0_8 || HAVE_CELT_API_0_11
00657 int res = celt_decode_float(fCeltDecoder[port_index], fCompressedBuffer[port_index], fCompressedSizeByte, fPortBuffer[port_index], fPeriodSize);
00658 #else
00659 int res = celt_decode_float(fCeltDecoder[port_index], fCompressedBuffer[port_index], fCompressedSizeByte, fPortBuffer[port_index]);
00660 #endif
00661 if (res != CELT_OK) {
00662 jack_error("celt_decode_float error fCompressedSizeByte = %d res = %d", fCompressedSizeByte, res);
00663 }
00664 }
00665 }
00666
00667 NextCycle();
00668 }
00669
00670
00671 int NetCeltAudioBuffer::RenderFromNetwork(int cycle, int sub_cycle, uint32_t port_num)
00672 {
00673
00674 if (sub_cycle == 0) {
00675 Cleanup();
00676 }
00677
00678 if (port_num > 0) {
00679
00680 if (sub_cycle == fNumPackets - 1) {
00681 for (int port_index = 0; port_index < fNPorts; port_index++) {
00682 memcpy(fCompressedBuffer[port_index] + sub_cycle * fSubPeriodBytesSize, fNetBuffer + port_index * fLastSubPeriodBytesSize, fLastSubPeriodBytesSize);
00683 }
00684 } else {
00685 for (int port_index = 0; port_index < fNPorts; port_index++) {
00686 memcpy(fCompressedBuffer[port_index] + sub_cycle * fSubPeriodBytesSize, fNetBuffer + port_index * fSubPeriodBytesSize, fSubPeriodBytesSize);
00687 }
00688 }
00689 }
00690
00691 return CheckPacket(cycle, sub_cycle);
00692 }
00693
00694 int NetCeltAudioBuffer::RenderToNetwork(int sub_cycle, uint32_t port_num)
00695 {
00696
00697 if (sub_cycle == fNumPackets - 1) {
00698 for (int port_index = 0; port_index < fNPorts; port_index++) {
00699 memcpy(fNetBuffer + port_index * fLastSubPeriodBytesSize, fCompressedBuffer[port_index] + sub_cycle * fSubPeriodBytesSize, fLastSubPeriodBytesSize);
00700 }
00701 return fNPorts * fLastSubPeriodBytesSize;
00702 } else {
00703 for (int port_index = 0; port_index < fNPorts; port_index++) {
00704 memcpy(fNetBuffer + port_index * fSubPeriodBytesSize, fCompressedBuffer[port_index] + sub_cycle * fSubPeriodBytesSize, fSubPeriodBytesSize);
00705 }
00706 return fNPorts * fSubPeriodBytesSize;
00707 }
00708 }
00709
00710 #endif
00711
00712 NetIntAudioBuffer::NetIntAudioBuffer(session_params_t* params, uint32_t nports, char* net_buffer)
00713 : NetAudioBuffer(params, nports, net_buffer)
00714 {
00715 fPeriodSize = params->fPeriodSize;
00716
00717 fCompressedSizeByte = (params->fPeriodSize * sizeof(short));
00718 jack_log("NetIntAudioBuffer fCompressedSizeByte %d", fCompressedSizeByte);
00719
00720 fIntBuffer = new short* [fNPorts];
00721 for (int port_index = 0; port_index < fNPorts; port_index++) {
00722 fIntBuffer[port_index] = new short[fPeriodSize];
00723 memset(fIntBuffer[port_index], 0, fPeriodSize * sizeof(short));
00724 }
00725
00726 int res1 = (fNPorts * fCompressedSizeByte) % PACKET_AVAILABLE_SIZE(params);
00727 int res2 = (fNPorts * fCompressedSizeByte) / PACKET_AVAILABLE_SIZE(params);
00728
00729 jack_log("NetIntAudioBuffer res1 = %d res2 = %d", res1, res2);
00730
00731 fNumPackets = (res1) ? (res2 + 1) : res2;
00732
00733 fSubPeriodBytesSize = fCompressedSizeByte / fNumPackets;
00734 fLastSubPeriodBytesSize = fSubPeriodBytesSize + fCompressedSizeByte % fNumPackets;
00735
00736 fSubPeriodSize = fSubPeriodBytesSize / sizeof(short);
00737
00738 jack_log("NetIntAudioBuffer fNumPackets = %d fSubPeriodBytesSize = %d, fLastSubPeriodBytesSize = %d", fNumPackets, fSubPeriodBytesSize, fLastSubPeriodBytesSize);
00739
00740 fCycleDuration = float(fSubPeriodBytesSize / sizeof(sample_t)) / float(params->fSampleRate);
00741 fCycleBytesSize = params->fMtu * fNumPackets;
00742
00743 fLastSubCycle = -1;
00744 return;
00745 }
00746
00747 NetIntAudioBuffer::~NetIntAudioBuffer()
00748 {
00749 for (int port_index = 0; port_index < fNPorts; port_index++) {
00750 delete [] fIntBuffer[port_index];
00751 }
00752
00753 delete [] fIntBuffer;
00754 }
00755
00756 size_t NetIntAudioBuffer::GetCycleSize()
00757 {
00758 return fCycleBytesSize;
00759 }
00760
00761 float NetIntAudioBuffer::GetCycleDuration()
00762 {
00763 return fCycleDuration;
00764 }
00765
00766 int NetIntAudioBuffer::GetNumPackets(int active_ports)
00767 {
00768 return fNumPackets;
00769 }
00770
00771 int NetIntAudioBuffer::RenderFromJackPorts()
00772 {
00773 for (int port_index = 0; port_index < fNPorts; port_index++) {
00774 if (fPortBuffer[port_index]) {
00775 for (uint frame = 0; frame < fPeriodSize; frame++) {
00776 fIntBuffer[port_index][frame] = short(fPortBuffer[port_index][frame] * 32768.f);
00777 }
00778 }
00779 }
00780
00781
00782 return fNPorts;
00783 }
00784
00785 void NetIntAudioBuffer::RenderToJackPorts()
00786 {
00787 float coef = 1.f / 32768.f;
00788 for (int port_index = 0; port_index < fNPorts; port_index++) {
00789 if (fPortBuffer[port_index]) {
00790 for (uint frame = 0; frame < fPeriodSize; frame++) {
00791 fPortBuffer[port_index][frame] = float(fIntBuffer[port_index][frame] * coef);
00792 }
00793 }
00794 }
00795
00796 NextCycle();
00797 }
00798
00799
00800 int NetIntAudioBuffer::RenderFromNetwork(int cycle, int sub_cycle, uint32_t port_num)
00801 {
00802
00803 if (sub_cycle == 0) {
00804 Cleanup();
00805 }
00806
00807 if (port_num > 0) {
00808 if (sub_cycle == fNumPackets - 1) {
00809 for (int port_index = 0; port_index < fNPorts; port_index++) {
00810 memcpy(fIntBuffer[port_index] + sub_cycle * fSubPeriodSize, fNetBuffer + port_index * fLastSubPeriodBytesSize, fLastSubPeriodBytesSize);
00811 }
00812 } else {
00813 for (int port_index = 0; port_index < fNPorts; port_index++) {
00814 memcpy(fIntBuffer[port_index] + sub_cycle * fSubPeriodSize, fNetBuffer + port_index * fSubPeriodBytesSize, fSubPeriodBytesSize);
00815 }
00816 }
00817 }
00818
00819 return CheckPacket(cycle, sub_cycle);
00820 }
00821
00822 int NetIntAudioBuffer::RenderToNetwork(int sub_cycle, uint32_t port_num)
00823 {
00824
00825 if (sub_cycle == fNumPackets - 1) {
00826 for (int port_index = 0; port_index < fNPorts; port_index++) {
00827 memcpy(fNetBuffer + port_index * fLastSubPeriodBytesSize, fIntBuffer[port_index] + sub_cycle * fSubPeriodSize, fLastSubPeriodBytesSize);
00828 }
00829 return fNPorts * fLastSubPeriodBytesSize;
00830 } else {
00831 for (int port_index = 0; port_index < fNPorts; port_index++) {
00832 memcpy(fNetBuffer + port_index * fSubPeriodBytesSize, fIntBuffer[port_index] + sub_cycle * fSubPeriodSize, fSubPeriodBytesSize);
00833 }
00834 return fNPorts * fSubPeriodBytesSize;
00835 }
00836 }
00837
00838
00839
00840 SERVER_EXPORT void SessionParamsHToN(session_params_t* src_params, session_params_t* dst_params)
00841 {
00842 memcpy(dst_params, src_params, sizeof(session_params_t));
00843 dst_params->fPacketID = htonl(src_params->fPacketID);
00844 dst_params->fMtu = htonl(src_params->fMtu);
00845 dst_params->fID = htonl(src_params->fID);
00846 dst_params->fTransportSync = htonl(src_params->fTransportSync);
00847 dst_params->fSendAudioChannels = htonl(src_params->fSendAudioChannels);
00848 dst_params->fReturnAudioChannels = htonl(src_params->fReturnAudioChannels);
00849 dst_params->fSendMidiChannels = htonl(src_params->fSendMidiChannels);
00850 dst_params->fReturnMidiChannels = htonl(src_params->fReturnMidiChannels);
00851 dst_params->fSampleRate = htonl(src_params->fSampleRate);
00852 dst_params->fPeriodSize = htonl(src_params->fPeriodSize);
00853 dst_params->fSampleEncoder = htonl(src_params->fSampleEncoder);
00854 dst_params->fKBps = htonl(src_params->fKBps);
00855 dst_params->fSlaveSyncMode = htonl(src_params->fSlaveSyncMode);
00856 dst_params->fNetworkLatency = htonl(src_params->fNetworkLatency);
00857 }
00858
00859 SERVER_EXPORT void SessionParamsNToH(session_params_t* src_params, session_params_t* dst_params)
00860 {
00861 memcpy(dst_params, src_params, sizeof(session_params_t));
00862 dst_params->fPacketID = ntohl(src_params->fPacketID);
00863 dst_params->fMtu = ntohl(src_params->fMtu);
00864 dst_params->fID = ntohl(src_params->fID);
00865 dst_params->fTransportSync = ntohl(src_params->fTransportSync);
00866 dst_params->fSendAudioChannels = ntohl(src_params->fSendAudioChannels);
00867 dst_params->fReturnAudioChannels = ntohl(src_params->fReturnAudioChannels);
00868 dst_params->fSendMidiChannels = ntohl(src_params->fSendMidiChannels);
00869 dst_params->fReturnMidiChannels = ntohl(src_params->fReturnMidiChannels);
00870 dst_params->fSampleRate = ntohl(src_params->fSampleRate);
00871 dst_params->fPeriodSize = ntohl(src_params->fPeriodSize);
00872 dst_params->fSampleEncoder = ntohl(src_params->fSampleEncoder);
00873 dst_params->fKBps = ntohl(src_params->fKBps);
00874 dst_params->fSlaveSyncMode = ntohl(src_params->fSlaveSyncMode);
00875 dst_params->fNetworkLatency = ntohl(src_params->fNetworkLatency);
00876 }
00877
00878 SERVER_EXPORT void SessionParamsDisplay(session_params_t* params)
00879 {
00880 char encoder[16];
00881 switch (params->fSampleEncoder)
00882 {
00883 case JackFloatEncoder:
00884 strcpy(encoder, "float");
00885 break;
00886 case JackIntEncoder:
00887 strcpy(encoder, "integer");
00888 break;
00889 case JackCeltEncoder:
00890 strcpy(encoder, "CELT");
00891 break;
00892 }
00893
00894 jack_info("**************** Network parameters ****************");
00895 jack_info("Name : %s", params->fName);
00896 jack_info("Protocol revision : %d", params->fProtocolVersion);
00897 jack_info("MTU : %u", params->fMtu);
00898 jack_info("Master name : %s", params->fMasterNetName);
00899 jack_info("Slave name : %s", params->fSlaveNetName);
00900 jack_info("ID : %u", params->fID);
00901 jack_info("Transport Sync : %s", (params->fTransportSync) ? "yes" : "no");
00902 jack_info("Send channels (audio - midi) : %d - %d", params->fSendAudioChannels, params->fSendMidiChannels);
00903 jack_info("Return channels (audio - midi) : %d - %d", params->fReturnAudioChannels, params->fReturnMidiChannels);
00904 jack_info("Sample rate : %u frames per second", params->fSampleRate);
00905 jack_info("Period size : %u frames per period", params->fPeriodSize);
00906 jack_info("Network latency : %u cycles", params->fNetworkLatency);
00907 switch (params->fSampleEncoder) {
00908 case (JackFloatEncoder):
00909 jack_info("SampleEncoder : %s", "Float");
00910 break;
00911 case (JackIntEncoder):
00912 jack_info("SampleEncoder : %s", "16 bits integer");
00913 break;
00914 case (JackCeltEncoder):
00915 jack_info("SampleEncoder : %s", "CELT");
00916 jack_info("kBits : %d", params->fKBps);
00917 break;
00918 };
00919 jack_info("Slave mode : %s", (params->fSlaveSyncMode) ? "sync" : "async");
00920 jack_info("****************************************************");
00921 }
00922
00923 SERVER_EXPORT sync_packet_type_t GetPacketType(session_params_t* params)
00924 {
00925 switch (params->fPacketID)
00926 {
00927 case 0:
00928 return SLAVE_AVAILABLE;
00929 case 1:
00930 return SLAVE_SETUP;
00931 case 2:
00932 return START_MASTER;
00933 case 3:
00934 return START_SLAVE;
00935 case 4:
00936 return KILL_MASTER;
00937 }
00938 return INVALID;
00939 }
00940
00941 SERVER_EXPORT int SetPacketType(session_params_t* params, sync_packet_type_t packet_type)
00942 {
00943 switch (packet_type)
00944 {
00945 case INVALID:
00946 return -1;
00947 case SLAVE_AVAILABLE:
00948 params->fPacketID = 0;
00949 break;
00950 case SLAVE_SETUP:
00951 params->fPacketID = 1;
00952 break;
00953 case START_MASTER:
00954 params->fPacketID = 2;
00955 break;
00956 case START_SLAVE:
00957 params->fPacketID = 3;
00958 break;
00959 case KILL_MASTER:
00960 params->fPacketID = 4;
00961 }
00962 return 0;
00963 }
00964
00965
00966
00967 SERVER_EXPORT void PacketHeaderHToN(packet_header_t* src_header, packet_header_t* dst_header)
00968 {
00969 memcpy(dst_header, src_header, sizeof(packet_header_t));
00970 dst_header->fID = htonl(src_header->fID);
00971 dst_header->fNumPacket = htonl(src_header->fNumPacket);
00972 dst_header->fPacketSize = htonl(src_header->fPacketSize);
00973 dst_header->fActivePorts = htonl(src_header->fActivePorts);
00974 dst_header->fCycle = htonl(src_header->fCycle);
00975 dst_header->fSubCycle = htonl(src_header->fSubCycle);
00976 dst_header->fIsLastPckt = htonl(src_header->fIsLastPckt);
00977 }
00978
00979 SERVER_EXPORT void PacketHeaderNToH(packet_header_t* src_header, packet_header_t* dst_header)
00980 {
00981 memcpy(dst_header, src_header, sizeof(packet_header_t));
00982 dst_header->fID = ntohl(src_header->fID);
00983 dst_header->fNumPacket = ntohl(src_header->fNumPacket);
00984 dst_header->fPacketSize = ntohl(src_header->fPacketSize);
00985 dst_header->fActivePorts = ntohl(src_header->fActivePorts);
00986 dst_header->fCycle = ntohl(src_header->fCycle);
00987 dst_header->fSubCycle = ntohl(src_header->fSubCycle);
00988 dst_header->fIsLastPckt = ntohl(src_header->fIsLastPckt);
00989 }
00990
00991 SERVER_EXPORT void PacketHeaderDisplay(packet_header_t* header)
00992 {
00993 char bitdepth[16];
00994 jack_info("********************Header********************");
00995 jack_info("Data type : %c", header->fDataType);
00996 jack_info("Data stream : %c", header->fDataStream);
00997 jack_info("ID : %u", header->fID);
00998 jack_info("Cycle : %u", header->fCycle);
00999 jack_info("SubCycle : %u", header->fSubCycle);
01000 jack_info("Active ports : %u", header->fActivePorts);
01001 jack_info("DATA packets : %u", header->fNumPacket);
01002 jack_info("DATA size : %u", header->fPacketSize);
01003 jack_info("Last packet : '%s'", (header->fIsLastPckt) ? "yes" : "no");
01004 jack_info("Bitdepth : %s", bitdepth);
01005 jack_info("**********************************************");
01006 }
01007
01008 SERVER_EXPORT void NetTransportDataDisplay(net_transport_data_t* data)
01009 {
01010 jack_info("********************Network Transport********************");
01011 jack_info("Transport new state : %u", data->fNewState);
01012 jack_info("Transport timebase master : %u", data->fTimebaseMaster);
01013 jack_info("Transport cycle state : %u", data->fState);
01014 jack_info("**********************************************");
01015 }
01016
01017 SERVER_EXPORT void MidiBufferHToN(JackMidiBuffer* src_buffer, JackMidiBuffer* dst_buffer)
01018 {
01019 dst_buffer->magic = htonl(src_buffer->magic);
01020 dst_buffer->buffer_size = htonl(src_buffer->buffer_size);
01021 dst_buffer->nframes = htonl(src_buffer->nframes);
01022 dst_buffer->write_pos = htonl(src_buffer->write_pos);
01023 dst_buffer->event_count = htonl(src_buffer->event_count);
01024 dst_buffer->lost_events = htonl(src_buffer->lost_events);
01025 dst_buffer->mix_index = htonl(src_buffer->mix_index);
01026 }
01027
01028 SERVER_EXPORT void MidiBufferNToH(JackMidiBuffer* src_buffer, JackMidiBuffer* dst_buffer)
01029 {
01030 dst_buffer->magic = ntohl(src_buffer->magic);
01031 dst_buffer->buffer_size = ntohl(src_buffer->buffer_size);
01032 dst_buffer->nframes = ntohl(src_buffer->nframes);
01033 dst_buffer->write_pos = ntohl(src_buffer->write_pos);
01034 dst_buffer->event_count = ntohl(src_buffer->event_count);
01035 dst_buffer->lost_events = ntohl(src_buffer->lost_events);
01036 dst_buffer->mix_index = ntohl(src_buffer->mix_index);
01037 }
01038
01039 SERVER_EXPORT void TransportDataHToN(net_transport_data_t* src_params, net_transport_data_t* dst_params)
01040 {
01041 dst_params->fNewState = htonl(src_params->fNewState);
01042 dst_params->fTimebaseMaster = htonl(src_params->fTimebaseMaster);
01043 dst_params->fState = htonl(src_params->fState);
01044 dst_params->fPosition.unique_1 = htonll(src_params->fPosition.unique_1);
01045 dst_params->fPosition.usecs = htonl(src_params->fPosition.usecs);
01046 dst_params->fPosition.frame_rate = htonl(src_params->fPosition.frame_rate);
01047 dst_params->fPosition.frame = htonl(src_params->fPosition.frame);
01048 dst_params->fPosition.valid = (jack_position_bits_t)htonl((uint32_t)src_params->fPosition.valid);
01049 dst_params->fPosition.bar = htonl(src_params->fPosition.bar);
01050 dst_params->fPosition.beat = htonl(src_params->fPosition.beat);
01051 dst_params->fPosition.tick = htonl(src_params->fPosition.tick);
01052 dst_params->fPosition.bar_start_tick = htonll((uint64_t)src_params->fPosition.bar_start_tick);
01053 dst_params->fPosition.beats_per_bar = htonl((uint32_t)src_params->fPosition.beats_per_bar);
01054 dst_params->fPosition.beat_type = htonl((uint32_t)src_params->fPosition.beat_type);
01055 dst_params->fPosition.ticks_per_beat = htonll((uint64_t)src_params->fPosition.ticks_per_beat);
01056 dst_params->fPosition.beats_per_minute = htonll((uint64_t)src_params->fPosition.beats_per_minute);
01057 dst_params->fPosition.frame_time = htonll((uint64_t)src_params->fPosition.frame_time);
01058 dst_params->fPosition.next_time = htonll((uint64_t)src_params->fPosition.next_time);
01059 dst_params->fPosition.bbt_offset = htonl(src_params->fPosition.bbt_offset);
01060 dst_params->fPosition.audio_frames_per_video_frame = htonl((uint32_t)src_params->fPosition.audio_frames_per_video_frame);
01061 dst_params->fPosition.video_offset = htonl(src_params->fPosition.video_offset);
01062 dst_params->fPosition.unique_2 = htonll(src_params->fPosition.unique_2);
01063 }
01064
01065 SERVER_EXPORT void TransportDataNToH(net_transport_data_t* src_params, net_transport_data_t* dst_params)
01066 {
01067 dst_params->fNewState = ntohl(src_params->fNewState);
01068 dst_params->fTimebaseMaster = ntohl(src_params->fTimebaseMaster);
01069 dst_params->fState = ntohl(src_params->fState);
01070 dst_params->fPosition.unique_1 = ntohll(src_params->fPosition.unique_1);
01071 dst_params->fPosition.usecs = ntohl(src_params->fPosition.usecs);
01072 dst_params->fPosition.frame_rate = ntohl(src_params->fPosition.frame_rate);
01073 dst_params->fPosition.frame = ntohl(src_params->fPosition.frame);
01074 dst_params->fPosition.valid = (jack_position_bits_t)ntohl((uint32_t)src_params->fPosition.valid);
01075 dst_params->fPosition.bar = ntohl(src_params->fPosition.bar);
01076 dst_params->fPosition.beat = ntohl(src_params->fPosition.beat);
01077 dst_params->fPosition.tick = ntohl(src_params->fPosition.tick);
01078 dst_params->fPosition.bar_start_tick = ntohll((uint64_t)src_params->fPosition.bar_start_tick);
01079 dst_params->fPosition.beats_per_bar = ntohl((uint32_t)src_params->fPosition.beats_per_bar);
01080 dst_params->fPosition.beat_type = ntohl((uint32_t)src_params->fPosition.beat_type);
01081 dst_params->fPosition.ticks_per_beat = ntohll((uint64_t)src_params->fPosition.ticks_per_beat);
01082 dst_params->fPosition.beats_per_minute = ntohll((uint64_t)src_params->fPosition.beats_per_minute);
01083 dst_params->fPosition.frame_time = ntohll((uint64_t)src_params->fPosition.frame_time);
01084 dst_params->fPosition.next_time = ntohll((uint64_t)src_params->fPosition.next_time);
01085 dst_params->fPosition.bbt_offset = ntohl(src_params->fPosition.bbt_offset);
01086 dst_params->fPosition.audio_frames_per_video_frame = ntohl((uint32_t)src_params->fPosition.audio_frames_per_video_frame);
01087 dst_params->fPosition.video_offset = ntohl(src_params->fPosition.video_offset);
01088 dst_params->fPosition.unique_2 = ntohll(src_params->fPosition.unique_2);
01089 }
01090
01091
01092
01093 SERVER_EXPORT int SocketAPIInit()
01094 {
01095 #ifdef WIN32
01096 WORD wVersionRequested = MAKEWORD(2, 2);
01097 WSADATA wsaData;
01098
01099 if (WSAStartup(wVersionRequested, &wsaData) != 0) {
01100 jack_error("WSAStartup error : %s", strerror(NET_ERROR_CODE));
01101 return -1;
01102 }
01103
01104 if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2) {
01105 jack_error("Could not find a useable version of Winsock.dll\n");
01106 WSACleanup();
01107 return -1;
01108 }
01109 #endif
01110 return 0;
01111 }
01112
01113 SERVER_EXPORT int SocketAPIEnd()
01114 {
01115 #ifdef WIN32
01116 return WSACleanup();
01117 #endif
01118 return 0;
01119 }
01120
01121 SERVER_EXPORT const char* GetTransportState(int transport_state)
01122 {
01123 switch (transport_state)
01124 {
01125 case JackTransportRolling:
01126 return "rolling";
01127 case JackTransportStarting:
01128 return "starting";
01129 case JackTransportStopped:
01130 return "stopped";
01131 case JackTransportNetStarting:
01132 return "netstarting";
01133 }
01134 return NULL;
01135 }
01136 }