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.

1137 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. #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 = htonl(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 = ntohl(*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. int NetFloatAudioBuffer::RenderFromNetwork(int cycle, int sub_cycle, uint32_t port_num)
  308. {
  309. // Cleanup all JACK ports at the beginning of the cycle
  310. if (sub_cycle == 0) {
  311. Cleanup();
  312. }
  313. if (port_num > 0) {
  314. UpdateParams(port_num);
  315. for (uint32_t port_index = 0; port_index < port_num; port_index++) {
  316. // Only copy to active ports : read the active port number then audio data
  317. int* active_port_address = (int*)(fNetBuffer + port_index * fSubPeriodBytesSize);
  318. int active_port = ntohl(*active_port_address);
  319. RenderFromNetwork((char*)(active_port_address + 1), active_port, sub_cycle);
  320. }
  321. }
  322. return CheckPacket(cycle, sub_cycle);
  323. }
  324. int NetFloatAudioBuffer::RenderToNetwork(int sub_cycle, uint32_t port_num)
  325. {
  326. int active_ports = 0;
  327. for (int port_index = 0; port_index < fNPorts; port_index++) {
  328. // Only copy from active ports : write the active port number then audio data
  329. if (fPortBuffer[port_index]) {
  330. int* active_port_address = (int*)(fNetBuffer + active_ports * fSubPeriodBytesSize);
  331. *active_port_address = htonl(port_index);
  332. RenderToNetwork((char*)(active_port_address + 1), port_index, sub_cycle);
  333. active_ports++;
  334. }
  335. }
  336. return port_num * fSubPeriodBytesSize;
  337. }
  338. #ifdef __BIG_ENDIAN__
  339. static inline jack_default_audio_sample_t SwapFloat(jack_default_audio_sample_t f)
  340. {
  341. union
  342. {
  343. jack_default_audio_sample_t f;
  344. unsigned char b[4];
  345. } dat1, dat2;
  346. dat1.f = f;
  347. dat2.b[0] = dat1.b[3];
  348. dat2.b[1] = dat1.b[2];
  349. dat2.b[2] = dat1.b[1];
  350. dat2.b[3] = dat1.b[0];
  351. return dat2.f;
  352. }
  353. void NetFloatAudioBuffer::RenderFromNetwork(char* net_buffer, int active_port, int sub_cycle)
  354. {
  355. if (fPortBuffer[active_port]) {
  356. jack_default_audio_sample_t* src = (jack_default_audio_sample_t*)(net_buffer);
  357. jack_default_audio_sample_t* dst = (jack_default_audio_sample_t*)(fPortBuffer[active_port] + sub_cycle * fSubPeriodSize);
  358. for (unsigned int sample = 0; sample < (fSubPeriodBytesSize - sizeof(int)) / sizeof(jack_default_audio_sample_t); sample++) {
  359. dst[sample] = SwapFloat(src[sample]);
  360. }
  361. }
  362. }
  363. void NetFloatAudioBuffer::RenderToNetwork(char* net_buffer, int active_port, int sub_cycle)
  364. {
  365. for (int port_index = 0; port_index < fNPorts; port_index++ ) {
  366. jack_default_audio_sample_t* src = (jack_default_audio_sample_t*)(fPortBuffer[active_port] + sub_cycle * fSubPeriodSize);
  367. jack_default_audio_sample_t* dst = (jack_default_audio_sample_t*)(net_buffer);
  368. for (unsigned int sample = 0; sample < (fSubPeriodBytesSize - sizeof(int)) / sizeof(jack_default_audio_sample_t); sample++) {
  369. dst[sample] = SwapFloat(src[sample]);
  370. }
  371. }
  372. }
  373. #else
  374. void NetFloatAudioBuffer::RenderFromNetwork(char* net_buffer, int active_port, int sub_cycle)
  375. {
  376. if (fPortBuffer[active_port]) {
  377. memcpy(fPortBuffer[active_port] + sub_cycle * fSubPeriodSize, net_buffer, fSubPeriodBytesSize - sizeof(int));
  378. }
  379. }
  380. void NetFloatAudioBuffer::RenderToNetwork(char* net_buffer, int active_port, int sub_cycle)
  381. {
  382. memcpy(net_buffer, fPortBuffer[active_port] + sub_cycle * fSubPeriodSize, fSubPeriodBytesSize - sizeof(int));
  383. }
  384. #endif
  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. memset(fCompressedBuffer[port_index], 0, fCompressedSizeByte * sizeof(char));
  447. }
  448. int res1 = (fNPorts * fCompressedSizeByte) % PACKET_AVAILABLE_SIZE(params);
  449. int res2 = (fNPorts * fCompressedSizeByte) / PACKET_AVAILABLE_SIZE(params);
  450. fNumPackets = (res1) ? (res2 + 1) : res2;
  451. jack_log("NetCeltAudioBuffer res1 = %d res2 = %d", res1, res2);
  452. fSubPeriodBytesSize = fCompressedSizeByte / fNumPackets;
  453. fLastSubPeriodBytesSize = fSubPeriodBytesSize + fCompressedSizeByte % fNumPackets;
  454. jack_log("NetCeltAudioBuffer fNumPackets = %d fSubPeriodBytesSize = %d, fLastSubPeriodBytesSize = %d", fNumPackets, fSubPeriodBytesSize, fLastSubPeriodBytesSize);
  455. fCycleDuration = float(fSubPeriodBytesSize / sizeof(sample_t)) / float(params->fSampleRate);
  456. fCycleBytesSize = params->fMtu * fNumPackets;
  457. fLastSubCycle = -1;
  458. return;
  459. }
  460. error:
  461. FreeCelt();
  462. throw std::bad_alloc();
  463. }
  464. NetCeltAudioBuffer::~NetCeltAudioBuffer()
  465. {
  466. FreeCelt();
  467. for (int port_index = 0; port_index < fNPorts; port_index++) {
  468. delete [] fCompressedBuffer[port_index];
  469. }
  470. delete [] fCompressedBuffer;
  471. }
  472. void NetCeltAudioBuffer::FreeCelt()
  473. {
  474. for (int i = 0; i < fNPorts; i++) {
  475. if (fCeltEncoder[i]) {
  476. celt_encoder_destroy(fCeltEncoder[i]);
  477. }
  478. if (fCeltDecoder[i]) {
  479. celt_decoder_destroy(fCeltDecoder[i]);
  480. }
  481. if (fCeltMode[i]) {
  482. celt_mode_destroy(fCeltMode[i]);
  483. }
  484. }
  485. delete [] fCeltMode;
  486. delete [] fCeltEncoder;
  487. delete [] fCeltDecoder;
  488. }
  489. size_t NetCeltAudioBuffer::GetCycleSize()
  490. {
  491. return fCycleBytesSize;
  492. }
  493. float NetCeltAudioBuffer::GetCycleDuration()
  494. {
  495. return fCycleDuration;
  496. }
  497. int NetCeltAudioBuffer::GetNumPackets(int active_ports)
  498. {
  499. return fNumPackets;
  500. }
  501. int NetCeltAudioBuffer::RenderFromJackPorts()
  502. {
  503. float buffer[fPeriodSize];
  504. for (int port_index = 0; port_index < fNPorts; port_index++) {
  505. if (fPortBuffer[port_index]) {
  506. memcpy(buffer, fPortBuffer[port_index], fPeriodSize * sizeof(sample_t));
  507. } else {
  508. memset(buffer, 0, fPeriodSize * sizeof(sample_t));
  509. }
  510. #if HAVE_CELT_API_0_8 || HAVE_CELT_API_0_11
  511. int res = celt_encode_float(fCeltEncoder[port_index], buffer, fPeriodSize, fCompressedBuffer[port_index], fCompressedSizeByte);
  512. #else
  513. int res = celt_encode_float(fCeltEncoder[port_index], buffer, NULL, fCompressedBuffer[port_index], fCompressedSizeByte);
  514. #endif
  515. if (res != fCompressedSizeByte) {
  516. jack_error("celt_encode_float error fCompressedSizeByte = %d res = %d", fCompressedSizeByte, res);
  517. }
  518. }
  519. // All ports active
  520. return fNPorts;
  521. }
  522. void NetCeltAudioBuffer::RenderToJackPorts()
  523. {
  524. for (int port_index = 0; port_index < fNPorts; port_index++) {
  525. if (fPortBuffer[port_index]) {
  526. #if HAVE_CELT_API_0_8 || HAVE_CELT_API_0_11
  527. int res = celt_decode_float(fCeltDecoder[port_index], fCompressedBuffer[port_index], fCompressedSizeByte, fPortBuffer[port_index], fPeriodSize);
  528. #else
  529. int res = celt_decode_float(fCeltDecoder[port_index], fCompressedBuffer[port_index], fCompressedSizeByte, fPortBuffer[port_index]);
  530. #endif
  531. if (res != CELT_OK) {
  532. jack_error("celt_decode_float error fCompressedSizeByte = %d res = %d", fCompressedSizeByte, res);
  533. }
  534. }
  535. }
  536. NextCycle();
  537. }
  538. //network<->buffer
  539. int NetCeltAudioBuffer::RenderFromNetwork(int cycle, int sub_cycle, uint32_t port_num)
  540. {
  541. // Cleanup all JACK ports at the beginning of the cycle
  542. if (sub_cycle == 0) {
  543. Cleanup();
  544. }
  545. if (port_num > 0) {
  546. // Last packet of the cycle
  547. if (sub_cycle == fNumPackets - 1) {
  548. for (int port_index = 0; port_index < fNPorts; port_index++) {
  549. memcpy(fCompressedBuffer[port_index] + sub_cycle * fSubPeriodBytesSize, fNetBuffer + port_index * fLastSubPeriodBytesSize, fLastSubPeriodBytesSize);
  550. }
  551. } else {
  552. for (int port_index = 0; port_index < fNPorts; port_index++) {
  553. memcpy(fCompressedBuffer[port_index] + sub_cycle * fSubPeriodBytesSize, fNetBuffer + port_index * fSubPeriodBytesSize, fSubPeriodBytesSize);
  554. }
  555. }
  556. }
  557. return CheckPacket(cycle, sub_cycle);
  558. }
  559. int NetCeltAudioBuffer::RenderToNetwork(int sub_cycle, uint32_t port_num)
  560. {
  561. // Last packet of the cycle
  562. if (sub_cycle == fNumPackets - 1) {
  563. for (int port_index = 0; port_index < fNPorts; port_index++) {
  564. memcpy(fNetBuffer + port_index * fLastSubPeriodBytesSize, fCompressedBuffer[port_index] + sub_cycle * fSubPeriodBytesSize, fLastSubPeriodBytesSize);
  565. }
  566. return fNPorts * fLastSubPeriodBytesSize;
  567. } else {
  568. for (int port_index = 0; port_index < fNPorts; port_index++) {
  569. memcpy(fNetBuffer + port_index * fSubPeriodBytesSize, fCompressedBuffer[port_index] + sub_cycle * fSubPeriodBytesSize, fSubPeriodBytesSize);
  570. }
  571. return fNPorts * fSubPeriodBytesSize;
  572. }
  573. }
  574. #endif
  575. NetIntAudioBuffer::NetIntAudioBuffer(session_params_t* params, uint32_t nports, char* net_buffer)
  576. : NetAudioBuffer(params, nports, net_buffer)
  577. {
  578. fPeriodSize = params->fPeriodSize;
  579. fCompressedSizeByte = (params->fPeriodSize * sizeof(short));
  580. jack_log("NetIntAudioBuffer fCompressedSizeByte %d", fCompressedSizeByte);
  581. fIntBuffer = new short* [fNPorts];
  582. for (int port_index = 0; port_index < fNPorts; port_index++) {
  583. fIntBuffer[port_index] = new short[fPeriodSize];
  584. memset(fIntBuffer[port_index], 0, fPeriodSize * sizeof(short));
  585. }
  586. int res1 = (fNPorts * fCompressedSizeByte) % PACKET_AVAILABLE_SIZE(params);
  587. int res2 = (fNPorts * fCompressedSizeByte) / PACKET_AVAILABLE_SIZE(params);
  588. jack_log("NetIntAudioBuffer res1 = %d res2 = %d", res1, res2);
  589. fNumPackets = (res1) ? (res2 + 1) : res2;
  590. fSubPeriodBytesSize = fCompressedSizeByte / fNumPackets;
  591. fLastSubPeriodBytesSize = fSubPeriodBytesSize + fCompressedSizeByte % fNumPackets;
  592. fSubPeriodSize = fSubPeriodBytesSize / sizeof(short);
  593. jack_log("NetIntAudioBuffer fNumPackets = %d fSubPeriodBytesSize = %d, fLastSubPeriodBytesSize = %d", fNumPackets, fSubPeriodBytesSize, fLastSubPeriodBytesSize);
  594. fCycleDuration = float(fSubPeriodBytesSize / sizeof(sample_t)) / float(params->fSampleRate);
  595. fCycleBytesSize = params->fMtu * fNumPackets;
  596. fLastSubCycle = -1;
  597. return;
  598. }
  599. NetIntAudioBuffer::~NetIntAudioBuffer()
  600. {
  601. for (int port_index = 0; port_index < fNPorts; port_index++) {
  602. delete [] fIntBuffer[port_index];
  603. }
  604. delete [] fIntBuffer;
  605. }
  606. size_t NetIntAudioBuffer::GetCycleSize()
  607. {
  608. return fCycleBytesSize;
  609. }
  610. float NetIntAudioBuffer::GetCycleDuration()
  611. {
  612. return fCycleDuration;
  613. }
  614. int NetIntAudioBuffer::GetNumPackets(int active_ports)
  615. {
  616. return fNumPackets;
  617. }
  618. int NetIntAudioBuffer::RenderFromJackPorts()
  619. {
  620. for (int port_index = 0; port_index < fNPorts; port_index++) {
  621. if (fPortBuffer[port_index]) {
  622. for (uint frame = 0; frame < fPeriodSize; frame++) {
  623. fIntBuffer[port_index][frame] = short(fPortBuffer[port_index][frame] * 32768.f);
  624. }
  625. }
  626. }
  627. // All ports active
  628. return fNPorts;
  629. }
  630. void NetIntAudioBuffer::RenderToJackPorts()
  631. {
  632. float coef = 1.f / 32768.f;
  633. for (int port_index = 0; port_index < fNPorts; port_index++) {
  634. if (fPortBuffer[port_index]) {
  635. for (uint frame = 0; frame < fPeriodSize; frame++) {
  636. fPortBuffer[port_index][frame] = float(fIntBuffer[port_index][frame] * coef);
  637. }
  638. }
  639. }
  640. NextCycle();
  641. }
  642. //network<->buffer
  643. int NetIntAudioBuffer::RenderFromNetwork(int cycle, int sub_cycle, uint32_t port_num)
  644. {
  645. // Cleanup all JACK ports at the beginning of the cycle
  646. if (sub_cycle == 0) {
  647. Cleanup();
  648. }
  649. if (port_num > 0) {
  650. if (sub_cycle == fNumPackets - 1) {
  651. for (int port_index = 0; port_index < fNPorts; port_index++) {
  652. memcpy(fIntBuffer[port_index] + sub_cycle * fSubPeriodSize, fNetBuffer + port_index * fLastSubPeriodBytesSize, fLastSubPeriodBytesSize);
  653. }
  654. } else {
  655. for (int port_index = 0; port_index < fNPorts; port_index++) {
  656. memcpy(fIntBuffer[port_index] + sub_cycle * fSubPeriodSize, fNetBuffer + port_index * fSubPeriodBytesSize, fSubPeriodBytesSize);
  657. }
  658. }
  659. }
  660. return CheckPacket(cycle, sub_cycle);
  661. }
  662. int NetIntAudioBuffer::RenderToNetwork(int sub_cycle, uint32_t port_num)
  663. {
  664. // Last packet of the cycle
  665. if (sub_cycle == fNumPackets - 1) {
  666. for (int port_index = 0; port_index < fNPorts; port_index++) {
  667. memcpy(fNetBuffer + port_index * fLastSubPeriodBytesSize, fIntBuffer[port_index] + sub_cycle * fSubPeriodSize, fLastSubPeriodBytesSize);
  668. }
  669. return fNPorts * fLastSubPeriodBytesSize;
  670. } else {
  671. for (int port_index = 0; port_index < fNPorts; port_index++) {
  672. memcpy(fNetBuffer + port_index * fSubPeriodBytesSize, fIntBuffer[port_index] + sub_cycle * fSubPeriodSize, fSubPeriodBytesSize);
  673. }
  674. return fNPorts * fSubPeriodBytesSize;
  675. }
  676. }
  677. // SessionParams ************************************************************************************
  678. SERVER_EXPORT void SessionParamsHToN(session_params_t* src_params, session_params_t* dst_params)
  679. {
  680. memcpy(dst_params, src_params, sizeof(session_params_t));
  681. dst_params->fPacketID = htonl(src_params->fPacketID);
  682. dst_params->fMtu = htonl(src_params->fMtu);
  683. dst_params->fID = htonl(src_params->fID);
  684. dst_params->fTransportSync = htonl(src_params->fTransportSync);
  685. dst_params->fSendAudioChannels = htonl(src_params->fSendAudioChannels);
  686. dst_params->fReturnAudioChannels = htonl(src_params->fReturnAudioChannels);
  687. dst_params->fSendMidiChannels = htonl(src_params->fSendMidiChannels);
  688. dst_params->fReturnMidiChannels = htonl(src_params->fReturnMidiChannels);
  689. dst_params->fSampleRate = htonl(src_params->fSampleRate);
  690. dst_params->fPeriodSize = htonl(src_params->fPeriodSize);
  691. dst_params->fSampleEncoder = htonl(src_params->fSampleEncoder);
  692. dst_params->fKBps = htonl(src_params->fKBps);
  693. dst_params->fSlaveSyncMode = htonl(src_params->fSlaveSyncMode);
  694. dst_params->fNetworkLatency = htonl(src_params->fNetworkLatency);
  695. }
  696. SERVER_EXPORT void SessionParamsNToH(session_params_t* src_params, session_params_t* dst_params)
  697. {
  698. memcpy(dst_params, src_params, sizeof(session_params_t));
  699. dst_params->fPacketID = ntohl(src_params->fPacketID);
  700. dst_params->fMtu = ntohl(src_params->fMtu);
  701. dst_params->fID = ntohl(src_params->fID);
  702. dst_params->fTransportSync = ntohl(src_params->fTransportSync);
  703. dst_params->fSendAudioChannels = ntohl(src_params->fSendAudioChannels);
  704. dst_params->fReturnAudioChannels = ntohl(src_params->fReturnAudioChannels);
  705. dst_params->fSendMidiChannels = ntohl(src_params->fSendMidiChannels);
  706. dst_params->fReturnMidiChannels = ntohl(src_params->fReturnMidiChannels);
  707. dst_params->fSampleRate = ntohl(src_params->fSampleRate);
  708. dst_params->fPeriodSize = ntohl(src_params->fPeriodSize);
  709. dst_params->fSampleEncoder = ntohl(src_params->fSampleEncoder);
  710. dst_params->fKBps = ntohl(src_params->fKBps);
  711. dst_params->fSlaveSyncMode = ntohl(src_params->fSlaveSyncMode);
  712. dst_params->fNetworkLatency = ntohl(src_params->fNetworkLatency);
  713. }
  714. SERVER_EXPORT void SessionParamsDisplay(session_params_t* params)
  715. {
  716. char encoder[16];
  717. switch (params->fSampleEncoder)
  718. {
  719. case JackFloatEncoder:
  720. strcpy(encoder, "float");
  721. break;
  722. case JackIntEncoder:
  723. strcpy(encoder, "integer");
  724. break;
  725. case JackCeltEncoder:
  726. strcpy(encoder, "CELT");
  727. break;
  728. }
  729. jack_info("**************** Network parameters ****************");
  730. jack_info("Name : %s", params->fName);
  731. jack_info("Protocol revision : %d", params->fProtocolVersion);
  732. jack_info("MTU : %u", params->fMtu);
  733. jack_info("Master name : %s", params->fMasterNetName);
  734. jack_info("Slave name : %s", params->fSlaveNetName);
  735. jack_info("ID : %u", params->fID);
  736. jack_info("Transport Sync : %s", (params->fTransportSync) ? "yes" : "no");
  737. jack_info("Send channels (audio - midi) : %d - %d", params->fSendAudioChannels, params->fSendMidiChannels);
  738. jack_info("Return channels (audio - midi) : %d - %d", params->fReturnAudioChannels, params->fReturnMidiChannels);
  739. jack_info("Sample rate : %u frames per second", params->fSampleRate);
  740. jack_info("Period size : %u frames per period", params->fPeriodSize);
  741. jack_info("Network latency : %u cycles", params->fNetworkLatency);
  742. switch (params->fSampleEncoder) {
  743. case (JackFloatEncoder):
  744. jack_info("SampleEncoder : %s", "Float");
  745. break;
  746. case (JackIntEncoder):
  747. jack_info("SampleEncoder : %s", "16 bits integer");
  748. break;
  749. case (JackCeltEncoder):
  750. jack_info("SampleEncoder : %s", "CELT");
  751. jack_info("kBits : %d", params->fKBps);
  752. break;
  753. };
  754. jack_info("Slave mode : %s", (params->fSlaveSyncMode) ? "sync" : "async");
  755. jack_info("****************************************************");
  756. }
  757. SERVER_EXPORT sync_packet_type_t GetPacketType(session_params_t* params)
  758. {
  759. switch (params->fPacketID)
  760. {
  761. case 0:
  762. return SLAVE_AVAILABLE;
  763. case 1:
  764. return SLAVE_SETUP;
  765. case 2:
  766. return START_MASTER;
  767. case 3:
  768. return START_SLAVE;
  769. case 4:
  770. return KILL_MASTER;
  771. }
  772. return INVALID;
  773. }
  774. SERVER_EXPORT int SetPacketType(session_params_t* params, sync_packet_type_t packet_type)
  775. {
  776. switch (packet_type)
  777. {
  778. case INVALID:
  779. return -1;
  780. case SLAVE_AVAILABLE:
  781. params->fPacketID = 0;
  782. break;
  783. case SLAVE_SETUP:
  784. params->fPacketID = 1;
  785. break;
  786. case START_MASTER:
  787. params->fPacketID = 2;
  788. break;
  789. case START_SLAVE:
  790. params->fPacketID = 3;
  791. break;
  792. case KILL_MASTER:
  793. params->fPacketID = 4;
  794. }
  795. return 0;
  796. }
  797. // Packet header **********************************************************************************
  798. SERVER_EXPORT void PacketHeaderHToN(packet_header_t* src_header, packet_header_t* dst_header)
  799. {
  800. memcpy(dst_header, src_header, sizeof(packet_header_t));
  801. dst_header->fID = htonl(src_header->fID);
  802. dst_header->fNumPacket = htonl(src_header->fNumPacket);
  803. dst_header->fPacketSize = htonl(src_header->fPacketSize);
  804. dst_header->fActivePorts = htonl(src_header->fActivePorts);
  805. dst_header->fCycle = htonl(src_header->fCycle);
  806. dst_header->fSubCycle = htonl(src_header->fSubCycle);
  807. dst_header->fIsLastPckt = htonl(src_header->fIsLastPckt);
  808. }
  809. SERVER_EXPORT void PacketHeaderNToH(packet_header_t* src_header, packet_header_t* dst_header)
  810. {
  811. memcpy(dst_header, src_header, sizeof(packet_header_t));
  812. dst_header->fID = ntohl(src_header->fID);
  813. dst_header->fNumPacket = ntohl(src_header->fNumPacket);
  814. dst_header->fPacketSize = ntohl(src_header->fPacketSize);
  815. dst_header->fActivePorts = ntohl(src_header->fActivePorts);
  816. dst_header->fCycle = ntohl(src_header->fCycle);
  817. dst_header->fSubCycle = ntohl(src_header->fSubCycle);
  818. dst_header->fIsLastPckt = ntohl(src_header->fIsLastPckt);
  819. }
  820. SERVER_EXPORT void PacketHeaderDisplay(packet_header_t* header)
  821. {
  822. char bitdepth[16];
  823. jack_info("********************Header********************");
  824. jack_info("Data type : %c", header->fDataType);
  825. jack_info("Data stream : %c", header->fDataStream);
  826. jack_info("ID : %u", header->fID);
  827. jack_info("Cycle : %u", header->fCycle);
  828. jack_info("SubCycle : %u", header->fSubCycle);
  829. jack_info("Active ports : %u", header->fActivePorts);
  830. jack_info("DATA packets : %u", header->fNumPacket);
  831. jack_info("DATA size : %u", header->fPacketSize);
  832. jack_info("Last packet : '%s'", (header->fIsLastPckt) ? "yes" : "no");
  833. jack_info("Bitdepth : %s", bitdepth);
  834. jack_info("**********************************************");
  835. }
  836. SERVER_EXPORT void NetTransportDataDisplay(net_transport_data_t* data)
  837. {
  838. jack_info("********************Network Transport********************");
  839. jack_info("Transport new state : %u", data->fNewState);
  840. jack_info("Transport timebase master : %u", data->fTimebaseMaster);
  841. jack_info("Transport cycle state : %u", data->fState);
  842. jack_info("**********************************************");
  843. }
  844. SERVER_EXPORT void MidiBufferHToN(JackMidiBuffer* src_buffer, JackMidiBuffer* dst_buffer)
  845. {
  846. dst_buffer->magic = htonl(src_buffer->magic);
  847. dst_buffer->buffer_size = htonl(src_buffer->buffer_size);
  848. dst_buffer->nframes = htonl(src_buffer->nframes);
  849. dst_buffer->write_pos = htonl(src_buffer->write_pos);
  850. dst_buffer->event_count = htonl(src_buffer->event_count);
  851. dst_buffer->lost_events = htonl(src_buffer->lost_events);
  852. dst_buffer->mix_index = htonl(src_buffer->mix_index);
  853. }
  854. SERVER_EXPORT void MidiBufferNToH(JackMidiBuffer* src_buffer, JackMidiBuffer* dst_buffer)
  855. {
  856. dst_buffer->magic = ntohl(src_buffer->magic);
  857. dst_buffer->buffer_size = ntohl(src_buffer->buffer_size);
  858. dst_buffer->nframes = ntohl(src_buffer->nframes);
  859. dst_buffer->write_pos = ntohl(src_buffer->write_pos);
  860. dst_buffer->event_count = ntohl(src_buffer->event_count);
  861. dst_buffer->lost_events = ntohl(src_buffer->lost_events);
  862. dst_buffer->mix_index = ntohl(src_buffer->mix_index);
  863. }
  864. SERVER_EXPORT void TransportDataHToN(net_transport_data_t* src_params, net_transport_data_t* dst_params)
  865. {
  866. dst_params->fNewState = htonl(src_params->fNewState);
  867. dst_params->fTimebaseMaster = htonl(src_params->fTimebaseMaster);
  868. dst_params->fState = htonl(src_params->fState);
  869. dst_params->fPosition.unique_1 = htonll(src_params->fPosition.unique_1);
  870. dst_params->fPosition.usecs = htonl(src_params->fPosition.usecs);
  871. dst_params->fPosition.frame_rate = htonl(src_params->fPosition.frame_rate);
  872. dst_params->fPosition.frame = htonl(src_params->fPosition.frame);
  873. dst_params->fPosition.valid = (jack_position_bits_t)htonl((uint32_t)src_params->fPosition.valid);
  874. dst_params->fPosition.bar = htonl(src_params->fPosition.bar);
  875. dst_params->fPosition.beat = htonl(src_params->fPosition.beat);
  876. dst_params->fPosition.tick = htonl(src_params->fPosition.tick);
  877. dst_params->fPosition.bar_start_tick = htonll((uint64_t)src_params->fPosition.bar_start_tick);
  878. dst_params->fPosition.beats_per_bar = htonl((uint32_t)src_params->fPosition.beats_per_bar);
  879. dst_params->fPosition.beat_type = htonl((uint32_t)src_params->fPosition.beat_type);
  880. dst_params->fPosition.ticks_per_beat = htonll((uint64_t)src_params->fPosition.ticks_per_beat);
  881. dst_params->fPosition.beats_per_minute = htonll((uint64_t)src_params->fPosition.beats_per_minute);
  882. dst_params->fPosition.frame_time = htonll((uint64_t)src_params->fPosition.frame_time);
  883. dst_params->fPosition.next_time = htonll((uint64_t)src_params->fPosition.next_time);
  884. dst_params->fPosition.bbt_offset = htonl(src_params->fPosition.bbt_offset);
  885. dst_params->fPosition.audio_frames_per_video_frame = htonl((uint32_t)src_params->fPosition.audio_frames_per_video_frame);
  886. dst_params->fPosition.video_offset = htonl(src_params->fPosition.video_offset);
  887. dst_params->fPosition.unique_2 = htonll(src_params->fPosition.unique_2);
  888. }
  889. SERVER_EXPORT void TransportDataNToH(net_transport_data_t* src_params, net_transport_data_t* dst_params)
  890. {
  891. dst_params->fNewState = ntohl(src_params->fNewState);
  892. dst_params->fTimebaseMaster = ntohl(src_params->fTimebaseMaster);
  893. dst_params->fState = ntohl(src_params->fState);
  894. dst_params->fPosition.unique_1 = ntohll(src_params->fPosition.unique_1);
  895. dst_params->fPosition.usecs = ntohl(src_params->fPosition.usecs);
  896. dst_params->fPosition.frame_rate = ntohl(src_params->fPosition.frame_rate);
  897. dst_params->fPosition.frame = ntohl(src_params->fPosition.frame);
  898. dst_params->fPosition.valid = (jack_position_bits_t)ntohl((uint32_t)src_params->fPosition.valid);
  899. dst_params->fPosition.bar = ntohl(src_params->fPosition.bar);
  900. dst_params->fPosition.beat = ntohl(src_params->fPosition.beat);
  901. dst_params->fPosition.tick = ntohl(src_params->fPosition.tick);
  902. dst_params->fPosition.bar_start_tick = ntohll((uint64_t)src_params->fPosition.bar_start_tick);
  903. dst_params->fPosition.beats_per_bar = ntohl((uint32_t)src_params->fPosition.beats_per_bar);
  904. dst_params->fPosition.beat_type = ntohl((uint32_t)src_params->fPosition.beat_type);
  905. dst_params->fPosition.ticks_per_beat = ntohll((uint64_t)src_params->fPosition.ticks_per_beat);
  906. dst_params->fPosition.beats_per_minute = ntohll((uint64_t)src_params->fPosition.beats_per_minute);
  907. dst_params->fPosition.frame_time = ntohll((uint64_t)src_params->fPosition.frame_time);
  908. dst_params->fPosition.next_time = ntohll((uint64_t)src_params->fPosition.next_time);
  909. dst_params->fPosition.bbt_offset = ntohl(src_params->fPosition.bbt_offset);
  910. dst_params->fPosition.audio_frames_per_video_frame = ntohl((uint32_t)src_params->fPosition.audio_frames_per_video_frame);
  911. dst_params->fPosition.video_offset = ntohl(src_params->fPosition.video_offset);
  912. dst_params->fPosition.unique_2 = ntohll(src_params->fPosition.unique_2);
  913. }
  914. // Utility *******************************************************************************************************
  915. SERVER_EXPORT int SocketAPIInit()
  916. {
  917. #ifdef WIN32
  918. WORD wVersionRequested = MAKEWORD(2, 2);
  919. WSADATA wsaData;
  920. if (WSAStartup(wVersionRequested, &wsaData) != 0) {
  921. jack_error("WSAStartup error : %s", strerror(NET_ERROR_CODE));
  922. return -1;
  923. }
  924. if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2) {
  925. jack_error("Could not find a useable version of Winsock.dll\n");
  926. WSACleanup();
  927. return -1;
  928. }
  929. #endif
  930. return 0;
  931. }
  932. SERVER_EXPORT int SocketAPIEnd()
  933. {
  934. #ifdef WIN32
  935. return WSACleanup();
  936. #endif
  937. return 0;
  938. }
  939. SERVER_EXPORT const char* GetTransportState(int transport_state)
  940. {
  941. switch (transport_state)
  942. {
  943. case JackTransportRolling:
  944. return "rolling";
  945. case JackTransportStarting:
  946. return "starting";
  947. case JackTransportStopped:
  948. return "stopped";
  949. case JackTransportNetStarting:
  950. return "netstarting";
  951. }
  952. return NULL;
  953. }
  954. }