jack2 codebase
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1111 lines
41KB

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