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.

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