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.

1138 lines
42KB

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