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.

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