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.

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