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.

1113 lines
41KB

  1. /*
  2. Copyright (C) 2008-2011 Romain Moret at Grame
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  14. */
  15. #include "JackNetTool.h"
  16. #ifdef __APPLE__
  17. #include <mach/mach_time.h>
  18. class HardwareClock
  19. {
  20. public:
  21. HardwareClock();
  22. void Reset();
  23. void Update();
  24. float GetDeltaTime() const;
  25. double GetTime() const;
  26. private:
  27. double m_clockToSeconds;
  28. uint64_t m_startAbsTime;
  29. uint64_t m_lastAbsTime;
  30. double m_time;
  31. float m_deltaTime;
  32. };
  33. HardwareClock::HardwareClock()
  34. {
  35. mach_timebase_info_data_t info;
  36. mach_timebase_info(&info);
  37. m_clockToSeconds = (double)info.numer/info.denom/1000000000.0;
  38. Reset();
  39. }
  40. void HardwareClock::Reset()
  41. {
  42. m_startAbsTime = mach_absolute_time();
  43. m_lastAbsTime = m_startAbsTime;
  44. m_time = m_startAbsTime*m_clockToSeconds;
  45. m_deltaTime = 1.0f/60.0f;
  46. }
  47. void HardwareClock::Update()
  48. {
  49. const uint64_t currentTime = mach_absolute_time();
  50. const uint64_t dt = currentTime - m_lastAbsTime;
  51. m_time = currentTime*m_clockToSeconds;
  52. m_deltaTime = (double)dt*m_clockToSeconds;
  53. m_lastAbsTime = currentTime;
  54. }
  55. float HardwareClock::GetDeltaTime() const
  56. {
  57. return m_deltaTime;
  58. }
  59. double HardwareClock::GetTime() const
  60. {
  61. return m_time;
  62. }
  63. #endif
  64. using namespace std;
  65. namespace Jack
  66. {
  67. // NetMidiBuffer**********************************************************************************
  68. NetMidiBuffer::NetMidiBuffer(session_params_t* params, uint32_t nports, char* net_buffer)
  69. {
  70. fNPorts = nports;
  71. fMaxBufsize = fNPorts * sizeof(sample_t) * params->fPeriodSize ;
  72. fMaxPcktSize = params->fMtu - sizeof(packet_header_t);
  73. fBuffer = new char[fMaxBufsize];
  74. fPortBuffer = new JackMidiBuffer* [fNPorts];
  75. for (int port_index = 0; port_index < fNPorts; port_index++) {
  76. fPortBuffer[port_index] = NULL;
  77. }
  78. fNetBuffer = net_buffer;
  79. fCycleBytesSize = params->fMtu
  80. * (max(params->fSendMidiChannels, params->fReturnMidiChannels)
  81. * params->fPeriodSize * sizeof(sample_t) / (params->fMtu - sizeof(packet_header_t)));
  82. }
  83. NetMidiBuffer::~NetMidiBuffer()
  84. {
  85. delete[] fBuffer;
  86. delete[] fPortBuffer;
  87. }
  88. size_t NetMidiBuffer::GetCycleSize()
  89. {
  90. return fCycleBytesSize;
  91. }
  92. int NetMidiBuffer::GetNumPackets(int data_size, int max_size)
  93. {
  94. int res1 = data_size % max_size;
  95. int res2 = data_size / max_size;
  96. return (res1) ? res2 + 1 : res2;
  97. }
  98. void NetMidiBuffer::SetBuffer(int index, JackMidiBuffer* buffer)
  99. {
  100. fPortBuffer[index] = buffer;
  101. }
  102. JackMidiBuffer* NetMidiBuffer::GetBuffer(int index)
  103. {
  104. return fPortBuffer[index];
  105. }
  106. void NetMidiBuffer::DisplayEvents()
  107. {
  108. for (int port_index = 0; port_index < fNPorts; port_index++) {
  109. for (uint event = 0; event < fPortBuffer[port_index]->event_count; event++) {
  110. if (fPortBuffer[port_index]->IsValid()) {
  111. jack_info("port %d : midi event %u/%u -> time : %u, size : %u",
  112. port_index + 1, event + 1, fPortBuffer[port_index]->event_count,
  113. fPortBuffer[port_index]->events[event].time, fPortBuffer[port_index]->events[event].size);
  114. }
  115. }
  116. }
  117. }
  118. int NetMidiBuffer::RenderFromJackPorts()
  119. {
  120. int pos = 0;
  121. size_t copy_size;
  122. for (int port_index = 0; port_index < fNPorts; port_index++) {
  123. char* write_pos = fBuffer + pos;
  124. copy_size = sizeof(JackMidiBuffer) + fPortBuffer[port_index]->event_count * sizeof(JackMidiEvent);
  125. memcpy(fBuffer + pos, fPortBuffer[port_index], copy_size);
  126. pos += copy_size;
  127. memcpy(fBuffer + pos,
  128. fPortBuffer[port_index] + (fPortBuffer[port_index]->buffer_size - fPortBuffer[port_index]->write_pos),
  129. fPortBuffer[port_index]->write_pos);
  130. pos += fPortBuffer[port_index]->write_pos;
  131. JackMidiBuffer* midi_buffer = reinterpret_cast<JackMidiBuffer*>(write_pos);
  132. MidiBufferHToN(midi_buffer, midi_buffer);
  133. }
  134. return pos;
  135. }
  136. void NetMidiBuffer::RenderToJackPorts()
  137. {
  138. int pos = 0;
  139. size_t copy_size;
  140. for (int port_index = 0; port_index < fNPorts; port_index++) {
  141. JackMidiBuffer* midi_buffer = reinterpret_cast<JackMidiBuffer*>(fBuffer + pos);
  142. MidiBufferNToH(midi_buffer, midi_buffer);
  143. copy_size = sizeof(JackMidiBuffer) + reinterpret_cast<JackMidiBuffer*>(fBuffer + pos)->event_count * sizeof(JackMidiEvent);
  144. memcpy(fPortBuffer[port_index], fBuffer + pos, copy_size);
  145. pos += copy_size;
  146. memcpy(fPortBuffer[port_index] + (fPortBuffer[port_index]->buffer_size - fPortBuffer[port_index]->write_pos),
  147. fBuffer + pos,
  148. fPortBuffer[port_index]->write_pos);
  149. pos += fPortBuffer[port_index]->write_pos;
  150. }
  151. }
  152. void NetMidiBuffer::RenderFromNetwork(int sub_cycle, size_t copy_size)
  153. {
  154. memcpy(fBuffer + sub_cycle * fMaxPcktSize, fNetBuffer, copy_size);
  155. }
  156. int NetMidiBuffer::RenderToNetwork(int sub_cycle, size_t total_size)
  157. {
  158. int size = total_size - sub_cycle * fMaxPcktSize;
  159. int copy_size = (size <= fMaxPcktSize) ? size : fMaxPcktSize;
  160. memcpy(fNetBuffer, fBuffer + sub_cycle * fMaxPcktSize, copy_size);
  161. return copy_size;
  162. }
  163. // net audio buffer *********************************************************************************
  164. NetAudioBuffer::NetAudioBuffer(session_params_t* params, uint32_t nports, char* net_buffer)
  165. {
  166. fNPorts = nports;
  167. fNetBuffer = net_buffer;
  168. fPortBuffer = new sample_t* [fNPorts];
  169. fConnectedPorts = new bool[fNPorts];
  170. for (int port_index = 0; port_index < fNPorts; port_index++) {
  171. fPortBuffer[port_index] = NULL;
  172. fConnectedPorts[port_index] = true;
  173. }
  174. }
  175. NetAudioBuffer::~NetAudioBuffer()
  176. {
  177. delete [] fConnectedPorts;
  178. delete [] fPortBuffer;
  179. }
  180. void NetAudioBuffer::SetBuffer(int index, sample_t* buffer)
  181. {
  182. fPortBuffer[index] = buffer;
  183. }
  184. sample_t* NetAudioBuffer::GetBuffer(int index)
  185. {
  186. return fPortBuffer[index];
  187. }
  188. int NetAudioBuffer::CheckPacket(int cycle, int sub_cycle)
  189. {
  190. int res;
  191. if (sub_cycle != fLastSubCycle + 1) {
  192. jack_error("Packet(s) missing from... %d %d", fLastSubCycle, sub_cycle);
  193. res = NET_PACKET_ERROR;
  194. } else {
  195. res = 0;
  196. }
  197. fLastSubCycle = sub_cycle;
  198. return res;
  199. }
  200. void NetAudioBuffer::NextCycle()
  201. {
  202. // reset for next cycle
  203. fLastSubCycle = -1;
  204. }
  205. void NetAudioBuffer::Cleanup()
  206. {
  207. for (int port_index = 0; port_index < fNPorts; port_index++) {
  208. if (fPortBuffer[port_index]) {
  209. memset(fPortBuffer[port_index], 0, fPeriodSize * sizeof(sample_t));
  210. }
  211. }
  212. }
  213. //network<->buffer
  214. int NetAudioBuffer::ActivePortsToNetwork(char* net_buffer)
  215. {
  216. int active_ports = 0;
  217. int* active_port_address = (int*)net_buffer;
  218. for (int port_index = 0; port_index < fNPorts; port_index++) {
  219. // Write the active port number
  220. if (fPortBuffer[port_index]) {
  221. *active_port_address = 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. }
  447. int res1 = (fNPorts * fCompressedSizeByte) % PACKET_AVAILABLE_SIZE(params);
  448. int res2 = (fNPorts * fCompressedSizeByte) / PACKET_AVAILABLE_SIZE(params);
  449. fNumPackets = (res1) ? (res2 + 1) : res2;
  450. jack_log("NetCeltAudioBuffer res1 = %d res2 = %d", res1, res2);
  451. fSubPeriodBytesSize = fCompressedSizeByte / fNumPackets;
  452. fLastSubPeriodBytesSize = fSubPeriodBytesSize + fCompressedSizeByte % fNumPackets;
  453. jack_log("NetCeltAudioBuffer fNumPackets = %d fSubPeriodBytesSize = %d, fLastSubPeriodBytesSize = %d", fNumPackets, fSubPeriodBytesSize, fLastSubPeriodBytesSize);
  454. fCycleDuration = float(fSubPeriodBytesSize / sizeof(sample_t)) / float(params->fSampleRate);
  455. fCycleBytesSize = params->fMtu * fNumPackets;
  456. fLastSubCycle = -1;
  457. return;
  458. }
  459. error:
  460. FreeCelt();
  461. throw std::bad_alloc();
  462. }
  463. NetCeltAudioBuffer::~NetCeltAudioBuffer()
  464. {
  465. FreeCelt();
  466. for (int port_index = 0; port_index < fNPorts; port_index++) {
  467. delete [] fCompressedBuffer[port_index];
  468. }
  469. delete [] fCompressedBuffer;
  470. }
  471. void NetCeltAudioBuffer::FreeCelt()
  472. {
  473. for (int i = 0; i < fNPorts; i++) {
  474. if (fCeltEncoder[i]) {
  475. celt_encoder_destroy(fCeltEncoder[i]);
  476. }
  477. if (fCeltDecoder[i]) {
  478. celt_decoder_destroy(fCeltDecoder[i]);
  479. }
  480. if (fCeltMode[i]) {
  481. celt_mode_destroy(fCeltMode[i]);
  482. }
  483. }
  484. delete [] fCeltMode;
  485. delete [] fCeltEncoder;
  486. delete [] fCeltDecoder;
  487. }
  488. size_t NetCeltAudioBuffer::GetCycleSize()
  489. {
  490. return fCycleBytesSize;
  491. }
  492. float NetCeltAudioBuffer::GetCycleDuration()
  493. {
  494. return fCycleDuration;
  495. }
  496. int NetCeltAudioBuffer::GetNumPackets(int active_ports)
  497. {
  498. return fNumPackets;
  499. }
  500. int NetCeltAudioBuffer::RenderFromJackPorts()
  501. {
  502. float buffer[fPeriodSize];
  503. for (int port_index = 0; port_index < fNPorts; port_index++) {
  504. memcpy(buffer, fPortBuffer[port_index], fPeriodSize * sizeof(sample_t));
  505. #if HAVE_CELT_API_0_8 || HAVE_CELT_API_0_11
  506. int res = celt_encode_float(fCeltEncoder[port_index], buffer, fPeriodSize, fCompressedBuffer[port_index], fCompressedSizeByte);
  507. #else
  508. int res = celt_encode_float(fCeltEncoder[port_index], buffer, NULL, fCompressedBuffer[port_index], fCompressedSizeByte);
  509. #endif
  510. if (res != fCompressedSizeByte) {
  511. jack_error("celt_encode_float error fCompressedSizeByte = %d res = %d", fCompressedSizeByte, res);
  512. }
  513. }
  514. // All ports active
  515. return fNPorts;
  516. }
  517. void NetCeltAudioBuffer::RenderToJackPorts()
  518. {
  519. for (int port_index = 0; port_index < fNPorts; port_index++) {
  520. #if HAVE_CELT_API_0_8 || HAVE_CELT_API_0_11
  521. int res = celt_decode_float(fCeltDecoder[port_index], fCompressedBuffer[port_index], fCompressedSizeByte, fPortBuffer[port_index], fPeriodSize);
  522. #else
  523. int res = celt_decode_float(fCeltDecoder[port_index], fCompressedBuffer[port_index], fCompressedSizeByte, fPortBuffer[port_index]);
  524. #endif
  525. if (res != CELT_OK) {
  526. jack_error("celt_decode_float error fCompressedSizeByte = %d res = %d", fCompressedSizeByte, res);
  527. }
  528. }
  529. NextCycle();
  530. }
  531. //network<->buffer
  532. int NetCeltAudioBuffer::RenderFromNetwork(int cycle, int sub_cycle, uint32_t port_num)
  533. {
  534. // Last packet of the cycle
  535. if (sub_cycle == fNumPackets - 1) {
  536. for (int port_index = 0; port_index < fNPorts; port_index++) {
  537. memcpy(fCompressedBuffer[port_index] + sub_cycle * fSubPeriodBytesSize, fNetBuffer + port_index * fLastSubPeriodBytesSize, fLastSubPeriodBytesSize);
  538. }
  539. } else {
  540. for (int port_index = 0; port_index < fNPorts; port_index++) {
  541. memcpy(fCompressedBuffer[port_index] + sub_cycle * fSubPeriodBytesSize, fNetBuffer + port_index * fSubPeriodBytesSize, fSubPeriodBytesSize);
  542. }
  543. }
  544. return CheckPacket(cycle, sub_cycle);
  545. }
  546. int NetCeltAudioBuffer::RenderToNetwork(int sub_cycle, uint32_t port_num)
  547. {
  548. // Last packet of the cycle
  549. if (sub_cycle == fNumPackets - 1) {
  550. for (int port_index = 0; port_index < fNPorts; port_index++) {
  551. memcpy(fNetBuffer + port_index * fLastSubPeriodBytesSize, fCompressedBuffer[port_index] + sub_cycle * fSubPeriodBytesSize, fLastSubPeriodBytesSize);
  552. }
  553. return fNPorts * fLastSubPeriodBytesSize;
  554. } else {
  555. for (int port_index = 0; port_index < fNPorts; port_index++) {
  556. memcpy(fNetBuffer + port_index * fSubPeriodBytesSize, fCompressedBuffer[port_index] + sub_cycle * fSubPeriodBytesSize, fSubPeriodBytesSize);
  557. }
  558. return fNPorts * fSubPeriodBytesSize;
  559. }
  560. }
  561. #endif
  562. NetIntAudioBuffer::NetIntAudioBuffer(session_params_t* params, uint32_t nports, char* net_buffer)
  563. : NetAudioBuffer(params, nports, net_buffer)
  564. {
  565. fPeriodSize = params->fPeriodSize;
  566. fCompressedSizeByte = (params->fPeriodSize * sizeof(short));
  567. jack_log("NetIntAudioBuffer fCompressedSizeByte %d", fCompressedSizeByte);
  568. fIntBuffer = new short* [fNPorts];
  569. for (int port_index = 0; port_index < fNPorts; port_index++) {
  570. fIntBuffer[port_index] = new short[fPeriodSize];
  571. }
  572. int res1 = (fNPorts * fCompressedSizeByte) % PACKET_AVAILABLE_SIZE(params);
  573. int res2 = (fNPorts * fCompressedSizeByte) / PACKET_AVAILABLE_SIZE(params);
  574. jack_log("NetIntAudioBuffer res1 = %d res2 = %d", res1, res2);
  575. fNumPackets = (res1) ? (res2 + 1) : res2;
  576. fSubPeriodBytesSize = fCompressedSizeByte / fNumPackets;
  577. fLastSubPeriodBytesSize = fSubPeriodBytesSize + fCompressedSizeByte % fNumPackets;
  578. fSubPeriodSize = fSubPeriodBytesSize / sizeof(short);
  579. jack_log("NetIntAudioBuffer fNumPackets = %d fSubPeriodBytesSize = %d, fLastSubPeriodBytesSize = %d", fNumPackets, fSubPeriodBytesSize, fLastSubPeriodBytesSize);
  580. fCycleDuration = float(fSubPeriodBytesSize / sizeof(sample_t)) / float(params->fSampleRate);
  581. fCycleBytesSize = params->fMtu * fNumPackets;
  582. fLastSubCycle = -1;
  583. return;
  584. }
  585. NetIntAudioBuffer::~NetIntAudioBuffer()
  586. {
  587. for (int port_index = 0; port_index < fNPorts; port_index++) {
  588. delete [] fIntBuffer[port_index];
  589. }
  590. delete [] fIntBuffer;
  591. }
  592. size_t NetIntAudioBuffer::GetCycleSize()
  593. {
  594. return fCycleBytesSize;
  595. }
  596. float NetIntAudioBuffer::GetCycleDuration()
  597. {
  598. return fCycleDuration;
  599. }
  600. int NetIntAudioBuffer::GetNumPackets(int active_ports)
  601. {
  602. return fNumPackets;
  603. }
  604. int NetIntAudioBuffer::RenderFromJackPorts()
  605. {
  606. for (int port_index = 0; port_index < fNPorts; port_index++) {
  607. for (uint frame = 0; frame < fPeriodSize; frame++) {
  608. fIntBuffer[port_index][frame] = short(fPortBuffer[port_index][frame] * 32768.f);
  609. }
  610. }
  611. // All ports active
  612. return fNPorts;
  613. }
  614. void NetIntAudioBuffer::RenderToJackPorts()
  615. {
  616. float coef = 1.f / 32768.f;
  617. for (int port_index = 0; port_index < fNPorts; port_index++) {
  618. for (uint frame = 0; frame < fPeriodSize; frame++) {
  619. fPortBuffer[port_index][frame] = float(fIntBuffer[port_index][frame] * coef);
  620. }
  621. }
  622. NextCycle();
  623. }
  624. //network<->buffer
  625. int NetIntAudioBuffer::RenderFromNetwork(int cycle, int sub_cycle, uint32_t port_num)
  626. {
  627. if (sub_cycle == fNumPackets - 1) {
  628. for (int port_index = 0; port_index < fNPorts; port_index++) {
  629. memcpy(fIntBuffer[port_index] + sub_cycle * fSubPeriodSize, fNetBuffer + port_index * fLastSubPeriodBytesSize, fLastSubPeriodBytesSize);
  630. }
  631. } else {
  632. for (int port_index = 0; port_index < fNPorts; port_index++) {
  633. memcpy(fIntBuffer[port_index] + sub_cycle * fSubPeriodSize, fNetBuffer + port_index * fSubPeriodBytesSize, fSubPeriodBytesSize);
  634. }
  635. }
  636. return CheckPacket(cycle, sub_cycle);
  637. }
  638. int NetIntAudioBuffer::RenderToNetwork(int sub_cycle, uint32_t port_num)
  639. {
  640. // Last packet of the cycle
  641. if (sub_cycle == fNumPackets - 1) {
  642. for (int port_index = 0; port_index < fNPorts; port_index++) {
  643. memcpy(fNetBuffer + port_index * fLastSubPeriodBytesSize, fIntBuffer[port_index] + sub_cycle * fSubPeriodSize, fLastSubPeriodBytesSize);
  644. }
  645. return fNPorts * fLastSubPeriodBytesSize;
  646. } else {
  647. for (int port_index = 0; port_index < fNPorts; port_index++) {
  648. memcpy(fNetBuffer + port_index * fSubPeriodBytesSize, fIntBuffer[port_index] + sub_cycle * fSubPeriodSize, fSubPeriodBytesSize);
  649. }
  650. return fNPorts * fSubPeriodBytesSize;
  651. }
  652. }
  653. // SessionParams ************************************************************************************
  654. SERVER_EXPORT void SessionParamsHToN(session_params_t* src_params, session_params_t* dst_params)
  655. {
  656. memcpy(dst_params, src_params, sizeof(session_params_t));
  657. dst_params->fPacketID = htonl(src_params->fPacketID);
  658. dst_params->fMtu = htonl(src_params->fMtu);
  659. dst_params->fID = htonl(src_params->fID);
  660. dst_params->fTransportSync = htonl(src_params->fTransportSync);
  661. dst_params->fSendAudioChannels = htonl(src_params->fSendAudioChannels);
  662. dst_params->fReturnAudioChannels = htonl(src_params->fReturnAudioChannels);
  663. dst_params->fSendMidiChannels = htonl(src_params->fSendMidiChannels);
  664. dst_params->fReturnMidiChannels = htonl(src_params->fReturnMidiChannels);
  665. dst_params->fSampleRate = htonl(src_params->fSampleRate);
  666. dst_params->fPeriodSize = htonl(src_params->fPeriodSize);
  667. dst_params->fSampleEncoder = htonl(src_params->fSampleEncoder);
  668. dst_params->fKBps = htonl(src_params->fKBps);
  669. dst_params->fSlaveSyncMode = htonl(src_params->fSlaveSyncMode);
  670. dst_params->fNetworkLatency = htonl(src_params->fNetworkLatency);
  671. }
  672. SERVER_EXPORT void SessionParamsNToH(session_params_t* src_params, session_params_t* dst_params)
  673. {
  674. memcpy(dst_params, src_params, sizeof(session_params_t));
  675. dst_params->fPacketID = ntohl(src_params->fPacketID);
  676. dst_params->fMtu = ntohl(src_params->fMtu);
  677. dst_params->fID = ntohl(src_params->fID);
  678. dst_params->fTransportSync = ntohl(src_params->fTransportSync);
  679. dst_params->fSendAudioChannels = ntohl(src_params->fSendAudioChannels);
  680. dst_params->fReturnAudioChannels = ntohl(src_params->fReturnAudioChannels);
  681. dst_params->fSendMidiChannels = ntohl(src_params->fSendMidiChannels);
  682. dst_params->fReturnMidiChannels = ntohl(src_params->fReturnMidiChannels);
  683. dst_params->fSampleRate = ntohl(src_params->fSampleRate);
  684. dst_params->fPeriodSize = ntohl(src_params->fPeriodSize);
  685. dst_params->fSampleEncoder = ntohl(src_params->fSampleEncoder);
  686. dst_params->fKBps = ntohl(src_params->fKBps);
  687. dst_params->fSlaveSyncMode = ntohl(src_params->fSlaveSyncMode);
  688. dst_params->fNetworkLatency = ntohl(src_params->fNetworkLatency);
  689. }
  690. SERVER_EXPORT void SessionParamsDisplay(session_params_t* params)
  691. {
  692. char encoder[16];
  693. switch (params->fSampleEncoder)
  694. {
  695. case JackFloatEncoder:
  696. strcpy(encoder, "float");
  697. break;
  698. case JackIntEncoder:
  699. strcpy(encoder, "integer");
  700. break;
  701. case JackCeltEncoder:
  702. strcpy(encoder, "CELT");
  703. break;
  704. }
  705. jack_info("**************** Network parameters ****************");
  706. jack_info("Name : %s", params->fName);
  707. jack_info("Protocol revision : %d", params->fProtocolVersion);
  708. jack_info("MTU : %u", params->fMtu);
  709. jack_info("Master name : %s", params->fMasterNetName);
  710. jack_info("Slave name : %s", params->fSlaveNetName);
  711. jack_info("ID : %u", params->fID);
  712. jack_info("Transport Sync : %s", (params->fTransportSync) ? "yes" : "no");
  713. jack_info("Send channels (audio - midi) : %d - %d", params->fSendAudioChannels, params->fSendMidiChannels);
  714. jack_info("Return channels (audio - midi) : %d - %d", params->fReturnAudioChannels, params->fReturnMidiChannels);
  715. jack_info("Sample rate : %u frames per second", params->fSampleRate);
  716. jack_info("Period size : %u frames per period", params->fPeriodSize);
  717. jack_info("Network latency : %u cycles", params->fNetworkLatency);
  718. switch (params->fSampleEncoder) {
  719. case (JackFloatEncoder):
  720. jack_info("SampleEncoder : %s", "Float");
  721. break;
  722. case (JackIntEncoder):
  723. jack_info("SampleEncoder : %s", "16 bits integer");
  724. break;
  725. case (JackCeltEncoder):
  726. jack_info("SampleEncoder : %s", "CELT");
  727. jack_info("kBits : %d", params->fKBps);
  728. break;
  729. };
  730. jack_info("Slave mode : %s", (params->fSlaveSyncMode) ? "sync" : "async");
  731. jack_info("****************************************************");
  732. }
  733. SERVER_EXPORT sync_packet_type_t GetPacketType(session_params_t* params)
  734. {
  735. switch (params->fPacketID)
  736. {
  737. case 0:
  738. return SLAVE_AVAILABLE;
  739. case 1:
  740. return SLAVE_SETUP;
  741. case 2:
  742. return START_MASTER;
  743. case 3:
  744. return START_SLAVE;
  745. case 4:
  746. return KILL_MASTER;
  747. }
  748. return INVALID;
  749. }
  750. SERVER_EXPORT int SetPacketType(session_params_t* params, sync_packet_type_t packet_type)
  751. {
  752. switch (packet_type)
  753. {
  754. case INVALID:
  755. return -1;
  756. case SLAVE_AVAILABLE:
  757. params->fPacketID = 0;
  758. break;
  759. case SLAVE_SETUP:
  760. params->fPacketID = 1;
  761. break;
  762. case START_MASTER:
  763. params->fPacketID = 2;
  764. break;
  765. case START_SLAVE:
  766. params->fPacketID = 3;
  767. break;
  768. case KILL_MASTER:
  769. params->fPacketID = 4;
  770. }
  771. return 0;
  772. }
  773. // Packet header **********************************************************************************
  774. SERVER_EXPORT void PacketHeaderHToN(packet_header_t* src_header, packet_header_t* dst_header)
  775. {
  776. memcpy(dst_header, src_header, sizeof(packet_header_t));
  777. dst_header->fID = htonl(src_header->fID);
  778. dst_header->fNumPacket = htonl(src_header->fNumPacket);
  779. dst_header->fPacketSize = htonl(src_header->fPacketSize);
  780. dst_header->fActivePorts = htonl(src_header->fActivePorts);
  781. dst_header->fCycle = htonl(src_header->fCycle);
  782. dst_header->fSubCycle = htonl(src_header->fSubCycle);
  783. dst_header->fIsLastPckt = htonl(src_header->fIsLastPckt);
  784. }
  785. SERVER_EXPORT void PacketHeaderNToH(packet_header_t* src_header, packet_header_t* dst_header)
  786. {
  787. memcpy(dst_header, src_header, sizeof(packet_header_t));
  788. dst_header->fID = ntohl(src_header->fID);
  789. dst_header->fNumPacket = ntohl(src_header->fNumPacket);
  790. dst_header->fPacketSize = ntohl(src_header->fPacketSize);
  791. dst_header->fActivePorts = ntohl(src_header->fActivePorts);
  792. dst_header->fCycle = ntohl(src_header->fCycle);
  793. dst_header->fSubCycle = ntohl(src_header->fSubCycle);
  794. dst_header->fIsLastPckt = ntohl(src_header->fIsLastPckt);
  795. }
  796. SERVER_EXPORT void PacketHeaderDisplay(packet_header_t* header)
  797. {
  798. char bitdepth[16];
  799. jack_info("********************Header********************");
  800. jack_info("Data type : %c", header->fDataType);
  801. jack_info("Data stream : %c", header->fDataStream);
  802. jack_info("ID : %u", header->fID);
  803. jack_info("Cycle : %u", header->fCycle);
  804. jack_info("SubCycle : %u", header->fSubCycle);
  805. jack_info("Active ports : %u", header->fActivePorts);
  806. jack_info("DATA packets : %u", header->fNumPacket);
  807. jack_info("DATA size : %u", header->fPacketSize);
  808. jack_info("Last packet : '%s'", (header->fIsLastPckt) ? "yes" : "no");
  809. jack_info("Bitdepth : %s", bitdepth);
  810. jack_info("**********************************************");
  811. }
  812. SERVER_EXPORT void NetTransportDataDisplay(net_transport_data_t* data)
  813. {
  814. jack_info("********************Network Transport********************");
  815. jack_info("Transport new state : %u", data->fNewState);
  816. jack_info("Transport timebase master : %u", data->fTimebaseMaster);
  817. jack_info("Transport cycle state : %u", data->fState);
  818. jack_info("**********************************************");
  819. }
  820. SERVER_EXPORT void MidiBufferHToN(JackMidiBuffer* src_buffer, JackMidiBuffer* dst_buffer)
  821. {
  822. dst_buffer->magic = htonl(src_buffer->magic);
  823. dst_buffer->buffer_size = htonl(src_buffer->buffer_size);
  824. dst_buffer->nframes = htonl(src_buffer->nframes);
  825. dst_buffer->write_pos = htonl(src_buffer->write_pos);
  826. dst_buffer->event_count = htonl(src_buffer->event_count);
  827. dst_buffer->lost_events = htonl(src_buffer->lost_events);
  828. dst_buffer->mix_index = htonl(src_buffer->mix_index);
  829. }
  830. SERVER_EXPORT void MidiBufferNToH(JackMidiBuffer* src_buffer, JackMidiBuffer* dst_buffer)
  831. {
  832. dst_buffer->magic = ntohl(src_buffer->magic);
  833. dst_buffer->buffer_size = ntohl(src_buffer->buffer_size);
  834. dst_buffer->nframes = ntohl(src_buffer->nframes);
  835. dst_buffer->write_pos = ntohl(src_buffer->write_pos);
  836. dst_buffer->event_count = ntohl(src_buffer->event_count);
  837. dst_buffer->lost_events = ntohl(src_buffer->lost_events);
  838. dst_buffer->mix_index = ntohl(src_buffer->mix_index);
  839. }
  840. SERVER_EXPORT void TransportDataHToN(net_transport_data_t* src_params, net_transport_data_t* dst_params)
  841. {
  842. dst_params->fNewState = htonl(src_params->fNewState);
  843. dst_params->fTimebaseMaster = htonl(src_params->fTimebaseMaster);
  844. dst_params->fState = htonl(src_params->fState);
  845. dst_params->fPosition.unique_1 = htonll(src_params->fPosition.unique_1);
  846. dst_params->fPosition.usecs = htonl(src_params->fPosition.usecs);
  847. dst_params->fPosition.frame_rate = htonl(src_params->fPosition.frame_rate);
  848. dst_params->fPosition.frame = htonl(src_params->fPosition.frame);
  849. dst_params->fPosition.valid = (jack_position_bits_t)htonl((uint32_t)src_params->fPosition.valid);
  850. dst_params->fPosition.bar = htonl(src_params->fPosition.bar);
  851. dst_params->fPosition.beat = htonl(src_params->fPosition.beat);
  852. dst_params->fPosition.tick = htonl(src_params->fPosition.tick);
  853. dst_params->fPosition.bar_start_tick = htonll((uint64_t)src_params->fPosition.bar_start_tick);
  854. dst_params->fPosition.beats_per_bar = htonl((uint32_t)src_params->fPosition.beats_per_bar);
  855. dst_params->fPosition.beat_type = htonl((uint32_t)src_params->fPosition.beat_type);
  856. dst_params->fPosition.ticks_per_beat = htonll((uint64_t)src_params->fPosition.ticks_per_beat);
  857. dst_params->fPosition.beats_per_minute = htonll((uint64_t)src_params->fPosition.beats_per_minute);
  858. dst_params->fPosition.frame_time = htonll((uint64_t)src_params->fPosition.frame_time);
  859. dst_params->fPosition.next_time = htonll((uint64_t)src_params->fPosition.next_time);
  860. dst_params->fPosition.bbt_offset = htonl(src_params->fPosition.bbt_offset);
  861. dst_params->fPosition.audio_frames_per_video_frame = htonl((uint32_t)src_params->fPosition.audio_frames_per_video_frame);
  862. dst_params->fPosition.video_offset = htonl(src_params->fPosition.video_offset);
  863. dst_params->fPosition.unique_2 = htonll(src_params->fPosition.unique_2);
  864. }
  865. SERVER_EXPORT void TransportDataNToH(net_transport_data_t* src_params, net_transport_data_t* dst_params)
  866. {
  867. dst_params->fNewState = ntohl(src_params->fNewState);
  868. dst_params->fTimebaseMaster = ntohl(src_params->fTimebaseMaster);
  869. dst_params->fState = ntohl(src_params->fState);
  870. dst_params->fPosition.unique_1 = ntohll(src_params->fPosition.unique_1);
  871. dst_params->fPosition.usecs = ntohl(src_params->fPosition.usecs);
  872. dst_params->fPosition.frame_rate = ntohl(src_params->fPosition.frame_rate);
  873. dst_params->fPosition.frame = ntohl(src_params->fPosition.frame);
  874. dst_params->fPosition.valid = (jack_position_bits_t)ntohl((uint32_t)src_params->fPosition.valid);
  875. dst_params->fPosition.bar = ntohl(src_params->fPosition.bar);
  876. dst_params->fPosition.beat = ntohl(src_params->fPosition.beat);
  877. dst_params->fPosition.tick = ntohl(src_params->fPosition.tick);
  878. dst_params->fPosition.bar_start_tick = ntohll((uint64_t)src_params->fPosition.bar_start_tick);
  879. dst_params->fPosition.beats_per_bar = ntohl((uint32_t)src_params->fPosition.beats_per_bar);
  880. dst_params->fPosition.beat_type = ntohl((uint32_t)src_params->fPosition.beat_type);
  881. dst_params->fPosition.ticks_per_beat = ntohll((uint64_t)src_params->fPosition.ticks_per_beat);
  882. dst_params->fPosition.beats_per_minute = ntohll((uint64_t)src_params->fPosition.beats_per_minute);
  883. dst_params->fPosition.frame_time = ntohll((uint64_t)src_params->fPosition.frame_time);
  884. dst_params->fPosition.next_time = ntohll((uint64_t)src_params->fPosition.next_time);
  885. dst_params->fPosition.bbt_offset = ntohl(src_params->fPosition.bbt_offset);
  886. dst_params->fPosition.audio_frames_per_video_frame = ntohl((uint32_t)src_params->fPosition.audio_frames_per_video_frame);
  887. dst_params->fPosition.video_offset = ntohl(src_params->fPosition.video_offset);
  888. dst_params->fPosition.unique_2 = ntohll(src_params->fPosition.unique_2);
  889. }
  890. // Utility *******************************************************************************************************
  891. SERVER_EXPORT int SocketAPIInit()
  892. {
  893. #ifdef WIN32
  894. WORD wVersionRequested = MAKEWORD(2, 2);
  895. WSADATA wsaData;
  896. if (WSAStartup(wVersionRequested, &wsaData) != 0) {
  897. jack_error("WSAStartup error : %s", strerror(NET_ERROR_CODE));
  898. return -1;
  899. }
  900. if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2) {
  901. jack_error("Could not find a useable version of Winsock.dll\n");
  902. WSACleanup();
  903. return -1;
  904. }
  905. #endif
  906. return 0;
  907. }
  908. SERVER_EXPORT int SocketAPIEnd()
  909. {
  910. #ifdef WIN32
  911. return WSACleanup();
  912. #endif
  913. return 0;
  914. }
  915. SERVER_EXPORT const char* GetTransportState(int transport_state)
  916. {
  917. switch (transport_state)
  918. {
  919. case JackTransportRolling:
  920. return "rolling";
  921. case JackTransportStarting:
  922. return "starting";
  923. case JackTransportStopped:
  924. return "stopped";
  925. case JackTransportNetStarting:
  926. return "netstarting";
  927. }
  928. return NULL;
  929. }
  930. }