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.

980 lines
36KB

  1. /*
  2. Copyright (C) 2008 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. fNetBuffer = net_buffer;
  78. fCycleSize = params->fMtu * (max(params->fSendMidiChannels, params->fReturnMidiChannels) *
  79. params->fPeriodSize * sizeof(sample_t) / (params->fMtu - sizeof(packet_header_t)));
  80. }
  81. NetMidiBuffer::~NetMidiBuffer()
  82. {
  83. delete[] fBuffer;
  84. delete[] fPortBuffer;
  85. }
  86. size_t NetMidiBuffer::GetCycleSize()
  87. {
  88. return fCycleSize;
  89. }
  90. int NetMidiBuffer::GetNumPackets()
  91. {
  92. /*
  93. return (data_size % PACKET_AVAILABLE_SIZE)
  94. ? (data_size / PACKET_AVAILABLE_SIZE + 1)
  95. : data_size / PACKET_AVAILABLE_SIZE;
  96. */
  97. //TODO
  98. return 0;
  99. }
  100. void NetMidiBuffer::SetBuffer ( int index, JackMidiBuffer* buffer )
  101. {
  102. fPortBuffer[index] = buffer;
  103. }
  104. JackMidiBuffer* NetMidiBuffer::GetBuffer ( int index )
  105. {
  106. return fPortBuffer[index];
  107. }
  108. void NetMidiBuffer::DisplayEvents()
  109. {
  110. for ( int port_index = 0; port_index < fNPorts; port_index++ )
  111. {
  112. for ( uint event = 0; event < fPortBuffer[port_index]->event_count; event++ )
  113. if ( fPortBuffer[port_index]->IsValid() )
  114. jack_info ( "port %d : midi event %u/%u -> time : %u, size : %u",
  115. port_index + 1, event + 1, fPortBuffer[port_index]->event_count,
  116. fPortBuffer[port_index]->events[event].time, fPortBuffer[port_index]->events[event].size );
  117. }
  118. }
  119. int NetMidiBuffer::RenderFromJackPorts()
  120. {
  121. int pos = 0;
  122. size_t copy_size;
  123. for ( int port_index = 0; port_index < fNPorts; port_index++ )
  124. {
  125. char* write_pos = fBuffer + pos;
  126. copy_size = sizeof ( JackMidiBuffer ) + fPortBuffer[port_index]->event_count * sizeof ( JackMidiEvent );
  127. memcpy ( fBuffer + pos, fPortBuffer[port_index], copy_size );
  128. pos += copy_size;
  129. memcpy ( fBuffer + pos, fPortBuffer[port_index] + ( fPortBuffer[port_index]->buffer_size - fPortBuffer[port_index]->write_pos ),
  130. fPortBuffer[port_index]->write_pos );
  131. pos += fPortBuffer[port_index]->write_pos;
  132. JackMidiBuffer* midi_buffer = reinterpret_cast<JackMidiBuffer*>(write_pos);
  133. MidiBufferHToN(midi_buffer, midi_buffer);
  134. }
  135. return pos;
  136. }
  137. int NetMidiBuffer::RenderToJackPorts()
  138. {
  139. int pos = 0;
  140. int copy_size;
  141. for ( int port_index = 0; port_index < fNPorts; port_index++ )
  142. {
  143. JackMidiBuffer* midi_buffer = reinterpret_cast<JackMidiBuffer*>(fBuffer + pos);
  144. MidiBufferNToH(midi_buffer, midi_buffer);
  145. copy_size = sizeof ( JackMidiBuffer ) + reinterpret_cast<JackMidiBuffer*> ( fBuffer + pos )->event_count * sizeof ( JackMidiEvent );
  146. memcpy ( fPortBuffer[port_index], fBuffer + pos, copy_size );
  147. pos += copy_size;
  148. memcpy ( fPortBuffer[port_index] + ( fPortBuffer[port_index]->buffer_size - fPortBuffer[port_index]->write_pos ),
  149. fBuffer + pos, fPortBuffer[port_index]->write_pos );
  150. pos += fPortBuffer[port_index]->write_pos;
  151. }
  152. return pos;
  153. }
  154. int NetMidiBuffer::RenderFromNetwork ( int subcycle, size_t copy_size )
  155. {
  156. memcpy ( fBuffer + subcycle * fMaxPcktSize, fNetBuffer, copy_size );
  157. return copy_size;
  158. }
  159. int NetMidiBuffer::RenderToNetwork ( int subcycle, size_t total_size )
  160. {
  161. int size = total_size - subcycle * fMaxPcktSize;
  162. int copy_size = (size <= fMaxPcktSize) ? size : fMaxPcktSize;
  163. memcpy(fNetBuffer, fBuffer + subcycle * fMaxPcktSize, copy_size);
  164. return copy_size;
  165. }
  166. // net audio buffer *********************************************************************************
  167. NetFloatAudioBuffer::NetFloatAudioBuffer ( session_params_t* params, uint32_t nports, char* net_buffer )
  168. : fPortBuffer(params, nports), fNetBuffer(net_buffer)
  169. {}
  170. NetFloatAudioBuffer::~NetFloatAudioBuffer()
  171. {}
  172. size_t NetFloatAudioBuffer::GetCycleSize()
  173. {
  174. return fPortBuffer.GetCycleSize();
  175. }
  176. void NetFloatAudioBuffer::SetBuffer ( int index, sample_t* buffer )
  177. {
  178. fPortBuffer.SetBuffer(index, buffer);
  179. }
  180. sample_t* NetFloatAudioBuffer::GetBuffer ( int index )
  181. {
  182. return fPortBuffer.GetBuffer(index);
  183. }
  184. int NetFloatAudioBuffer::RenderFromJackPorts ()
  185. {
  186. return fPortBuffer.RenderFromJackPorts();
  187. }
  188. int NetFloatAudioBuffer::RenderToJackPorts ()
  189. {
  190. return fPortBuffer.RenderToJackPorts();
  191. }
  192. //network<->buffer
  193. int NetFloatAudioBuffer::RenderFromNetwork ( int cycle, int subcycle, size_t copy_size )
  194. {
  195. return fPortBuffer.RenderFromNetwork(fNetBuffer, cycle, subcycle, copy_size);
  196. }
  197. int NetFloatAudioBuffer::RenderToNetwork (int subcycle, size_t total_size )
  198. {
  199. return fPortBuffer.RenderToNetwork(fNetBuffer, subcycle, total_size);
  200. }
  201. // Celt audio buffer *********************************************************************************
  202. #if HAVE_CELT
  203. #define KPS 32
  204. #define KPS_DIV 8
  205. NetCeltAudioBuffer::NetCeltAudioBuffer ( session_params_t* params, uint32_t nports, char* net_buffer, int kbps )
  206. : fNetBuffer(net_buffer)
  207. {
  208. int res1, res2;
  209. fNPorts = nports;
  210. fPeriodSize = params->fPeriodSize;
  211. fCeltMode = new CELTMode *[fNPorts];
  212. fCeltEncoder = new CELTEncoder *[fNPorts];
  213. fCeltDecoder = new CELTDecoder *[fNPorts];
  214. memset(fCeltMode, 0, fNPorts * sizeof(CELTMode*));
  215. memset(fCeltEncoder, 0, fNPorts * sizeof(CELTEncoder*));
  216. memset(fCeltDecoder, 0, fNPorts * sizeof(CELTDecoder*));
  217. int error = CELT_OK;
  218. for (int i = 0; i < fNPorts; i++) {
  219. fCeltMode[i] = celt_mode_create(params->fSampleRate, params->fPeriodSize, &error);
  220. if (error != CELT_OK)
  221. goto error;
  222. #if HAVE_CELT_API_0_11
  223. fCeltEncoder[i] = celt_encoder_create_custom(fCeltMode[i], 1, &error);
  224. if (error != CELT_OK)
  225. goto error;
  226. celt_encoder_ctl(fCeltEncoder[i], CELT_SET_COMPLEXITY(1));
  227. fCeltDecoder[i] = celt_decoder_create_custom(fCeltMode[i], 1, &error);
  228. if (error != CELT_OK)
  229. goto error;
  230. celt_decoder_ctl(fCeltDecoder[i], CELT_SET_COMPLEXITY(1));
  231. #elif HAVE_CELT_API_0_7 || HAVE_CELT_API_0_8
  232. fCeltEncoder[i] = celt_encoder_create(fCeltMode[i], 1, &error);
  233. if (error != CELT_OK)
  234. goto error;
  235. celt_encoder_ctl(fCeltEncoder[i], CELT_SET_COMPLEXITY(1));
  236. fCeltDecoder[i] = celt_decoder_create(fCeltMode[i], 1, &error);
  237. if (error != CELT_OK)
  238. goto error;
  239. celt_decoder_ctl(fCeltDecoder[i], CELT_SET_COMPLEXITY(1));
  240. #else
  241. fCeltEncoder[i] = celt_encoder_create(fCeltMode[i]);
  242. if (error != CELT_OK)
  243. goto error;
  244. celt_encoder_ctl(fCeltEncoder[i], CELT_SET_COMPLEXITY(1));
  245. fCeltDecoder[i] = celt_decoder_create(fCeltMode[i]);
  246. if (error != CELT_OK)
  247. goto error;
  248. celt_decoder_ctl(fCeltDecoder[i], CELT_SET_COMPLEXITY(1));
  249. #endif
  250. }
  251. fPortBuffer = new sample_t* [fNPorts];
  252. for (int port_index = 0; port_index < fNPorts; port_index++)
  253. fPortBuffer[port_index] = NULL;
  254. /*
  255. celt_int32 lookahead;
  256. celt_mode_info( celt_mode, CELT_GET_LOOKAHEAD, &lookahead );
  257. */
  258. fCompressedSizeByte = (kbps * params->fPeriodSize * 1024) / (params->fSampleRate * 8);
  259. //fCompressedSizeByte = (params->fPeriodSize * sizeof(sample_t)) / KPS_DIV; // TODO
  260. fCompressedBuffer = new unsigned char* [fNPorts];
  261. for (int port_index = 0; port_index < fNPorts; port_index++)
  262. fCompressedBuffer[port_index] = new unsigned char[fCompressedSizeByte];
  263. jack_log("NetCeltAudioBuffer fCompressedSizeByte %d", fCompressedSizeByte);
  264. res1 = (fNPorts * fCompressedSizeByte) % (params->fMtu - sizeof(packet_header_t));
  265. res2 = (fNPorts * fCompressedSizeByte) / (params->fMtu - sizeof(packet_header_t));
  266. jack_log("NetCeltAudioBuffer res1 = %d res2 = %d", res1, res2);
  267. fNumPackets = (res1) ? (res2 + 1) : res2;
  268. fSubPeriodBytesSize = fCompressedSizeByte / fNumPackets;
  269. fLastSubPeriodBytesSize = fSubPeriodBytesSize + fCompressedSizeByte % fNumPackets;
  270. jack_log("NetCeltAudioBuffer fNumPackets = %d fSubPeriodBytesSize = %d, fLastSubPeriodBytesSize = %d", fNumPackets, fSubPeriodBytesSize, fLastSubPeriodBytesSize);
  271. fCycleDuration = float(fSubPeriodBytesSize / sizeof(sample_t)) / float(params->fSampleRate);
  272. fCycleSize = params->fMtu * fNumPackets;
  273. fLastSubCycle = -1;
  274. return;
  275. error:
  276. FreeCelt();
  277. throw std::bad_alloc();
  278. }
  279. NetCeltAudioBuffer::~NetCeltAudioBuffer()
  280. {
  281. FreeCelt();
  282. for (int port_index = 0; port_index < fNPorts; port_index++)
  283. delete [] fCompressedBuffer[port_index];
  284. delete [] fCompressedBuffer;
  285. delete [] fPortBuffer;
  286. }
  287. void NetCeltAudioBuffer::FreeCelt()
  288. {
  289. for (int i = 0; i < fNPorts; i++) {
  290. if (fCeltEncoder[i])
  291. celt_encoder_destroy(fCeltEncoder[i]);
  292. if (fCeltDecoder[i])
  293. celt_decoder_destroy(fCeltDecoder[i]);
  294. if (fCeltMode[i])
  295. celt_mode_destroy(fCeltMode[i]);
  296. }
  297. delete [] fCeltMode;
  298. delete [] fCeltEncoder;
  299. delete [] fCeltDecoder;
  300. }
  301. size_t NetCeltAudioBuffer::GetCycleSize()
  302. {
  303. return fCycleSize;
  304. }
  305. float NetCeltAudioBuffer::GetCycleDuration()
  306. {
  307. return fCycleDuration;
  308. }
  309. int NetCeltAudioBuffer::GetNumPackets()
  310. {
  311. return fNumPackets;
  312. }
  313. void NetCeltAudioBuffer::SetBuffer(int index, sample_t* buffer)
  314. {
  315. assert(fPortBuffer);
  316. fPortBuffer[index] = buffer;
  317. }
  318. sample_t* NetCeltAudioBuffer::GetBuffer(int index)
  319. {
  320. assert(fPortBuffer);
  321. return fPortBuffer[index];
  322. }
  323. int NetCeltAudioBuffer::RenderFromJackPorts()
  324. {
  325. float floatbuf[fPeriodSize];
  326. for (int port_index = 0; port_index < fNPorts; port_index++) {
  327. memcpy(floatbuf, fPortBuffer[port_index], fPeriodSize * sizeof(float));
  328. #if HAVE_CELT_API_0_8 || HAVE_CELT_API_0_11
  329. int res = celt_encode_float(fCeltEncoder[port_index], floatbuf, fPeriodSize, fCompressedBuffer[port_index], fCompressedSizeByte);
  330. #else
  331. int res = celt_encode_float(fCeltEncoder[port_index], floatbuf, NULL, fCompressedBuffer[port_index], fCompressedSizeByte);
  332. #endif
  333. if (res != fCompressedSizeByte) {
  334. jack_error("celt_encode_float error fCompressedSizeByte = %d res = %d", fCompressedSizeByte, res);
  335. }
  336. }
  337. return fNPorts * fCompressedSizeByte; // in bytes
  338. }
  339. int NetCeltAudioBuffer::RenderToJackPorts()
  340. {
  341. for (int port_index = 0; port_index < fNPorts; port_index++) {
  342. #if HAVE_CELT_API_0_8 || HAVE_CELT_API_0_11
  343. int res = celt_decode_float(fCeltDecoder[port_index], fCompressedBuffer[port_index], fCompressedSizeByte, fPortBuffer[port_index], fPeriodSize );
  344. #else
  345. int res = celt_decode_float(fCeltDecoder[port_index], fCompressedBuffer[port_index], fCompressedSizeByte, fPortBuffer[port_index]);
  346. #endif
  347. if (res != CELT_OK) {
  348. jack_error("celt_decode_float error res = %d", fCompressedSizeByte, res);
  349. }
  350. }
  351. fLastSubCycle = -1;
  352. //return fPeriodSize * sizeof(sample_t); // in bytes; TODO
  353. return 0;
  354. }
  355. //network<->buffer
  356. int NetCeltAudioBuffer::RenderFromNetwork(int cycle, int subcycle, size_t copy_size)
  357. {
  358. if (subcycle == fNumPackets - 1) {
  359. for (int port_index = 0; port_index < fNPorts; port_index++)
  360. memcpy(fCompressedBuffer[port_index] + subcycle * fSubPeriodBytesSize, fNetBuffer + port_index * fLastSubPeriodBytesSize, fLastSubPeriodBytesSize);
  361. } else {
  362. for (int port_index = 0; port_index < fNPorts; port_index++)
  363. memcpy(fCompressedBuffer[port_index] + subcycle * fSubPeriodBytesSize, fNetBuffer + port_index * fSubPeriodBytesSize, fSubPeriodBytesSize);
  364. }
  365. if (subcycle != fLastSubCycle + 1)
  366. jack_error("Packet(s) missing from... %d %d", fLastSubCycle, subcycle);
  367. fLastSubCycle = subcycle;
  368. return copy_size;
  369. }
  370. int NetCeltAudioBuffer::RenderToNetwork(int subcycle, size_t total_size)
  371. {
  372. if (subcycle == fNumPackets - 1) {
  373. for (int port_index = 0; port_index < fNPorts; port_index++)
  374. memcpy(fNetBuffer + port_index * fLastSubPeriodBytesSize, fCompressedBuffer[port_index] + subcycle * fSubPeriodBytesSize, fLastSubPeriodBytesSize);
  375. return fNPorts * fLastSubPeriodBytesSize;
  376. } else {
  377. for (int port_index = 0; port_index < fNPorts; port_index++)
  378. memcpy(fNetBuffer + port_index * fSubPeriodBytesSize, fCompressedBuffer[port_index] + subcycle * fSubPeriodBytesSize, fSubPeriodBytesSize);
  379. return fNPorts * fSubPeriodBytesSize;
  380. }
  381. return fNPorts * fSubPeriodBytesSize;
  382. }
  383. #endif
  384. NetIntAudioBuffer::NetIntAudioBuffer ( session_params_t* params, uint32_t nports, char* net_buffer )
  385. : fNetBuffer(net_buffer)
  386. {
  387. int res1, res2;
  388. fNPorts = nports;
  389. fPeriodSize = params->fPeriodSize;
  390. fPortBuffer = new sample_t* [fNPorts];
  391. for (int port_index = 0; port_index < fNPorts; port_index++)
  392. fPortBuffer[port_index] = NULL;
  393. fIntBuffer = new short* [fNPorts];
  394. for (int port_index = 0; port_index < fNPorts; port_index++)
  395. fIntBuffer[port_index] = new short[fPeriodSize];
  396. fCompressedSizeByte = (params->fPeriodSize * sizeof(short));
  397. jack_log("fCompressedSizeByte %d", fCompressedSizeByte);
  398. res1 = (fNPorts * fCompressedSizeByte) % (params->fMtu - sizeof(packet_header_t));
  399. res2 = (fNPorts * fCompressedSizeByte) / (params->fMtu - sizeof(packet_header_t));
  400. jack_log("res1 = %d res2 = %d", res1, res2);
  401. fNumPackets = (res1) ? (res2 + 1) : res2;
  402. fSubPeriodBytesSize = fCompressedSizeByte / fNumPackets;
  403. fSubPeriodSize = fSubPeriodBytesSize / sizeof(short);
  404. fLastSubPeriodBytesSize = fSubPeriodBytesSize + fCompressedSizeByte % fNumPackets;
  405. fLastSubPeriodSize = fLastSubPeriodBytesSize / sizeof(short);
  406. jack_log("fNumPackets = %d fSubPeriodBytesSize = %d, fLastSubPeriodBytesSize = %d", fNumPackets, fSubPeriodBytesSize, fLastSubPeriodBytesSize);
  407. fCycleDuration = float(fSubPeriodBytesSize / sizeof(sample_t)) / float(params->fSampleRate);
  408. fCycleSize = params->fMtu * fNumPackets;
  409. fLastSubCycle = -1;
  410. return;
  411. }
  412. NetIntAudioBuffer::~NetIntAudioBuffer()
  413. {
  414. for (int port_index = 0; port_index < fNPorts; port_index++)
  415. delete [] fIntBuffer[port_index];
  416. delete [] fIntBuffer;
  417. delete [] fPortBuffer;
  418. }
  419. size_t NetIntAudioBuffer::GetCycleSize()
  420. {
  421. return fCycleSize;
  422. }
  423. float NetIntAudioBuffer::GetCycleDuration()
  424. {
  425. return fCycleDuration;
  426. }
  427. int NetIntAudioBuffer::GetNumPackets()
  428. {
  429. return fNumPackets;
  430. }
  431. void NetIntAudioBuffer::SetBuffer(int index, sample_t* buffer)
  432. {
  433. fPortBuffer[index] = buffer;
  434. }
  435. sample_t* NetIntAudioBuffer::GetBuffer(int index)
  436. {
  437. return fPortBuffer[index];
  438. }
  439. int NetIntAudioBuffer::RenderFromJackPorts()
  440. {
  441. for (int port_index = 0; port_index < fNPorts; port_index++) {
  442. for (unsigned int frame = 0; frame < fPeriodSize; frame++)
  443. fIntBuffer[port_index][frame] = short(fPortBuffer[port_index][frame] * 32768.f);
  444. }
  445. return fNPorts * fCompressedSizeByte; // in bytes
  446. }
  447. int NetIntAudioBuffer::RenderToJackPorts()
  448. {
  449. for (int port_index = 0; port_index < fNPorts; port_index++) {
  450. float coef = 1.f / 32768.f;
  451. for (unsigned int frame = 0; frame < fPeriodSize; frame++)
  452. fPortBuffer[port_index][frame] = float(fIntBuffer[port_index][frame] * coef);
  453. }
  454. fLastSubCycle = -1;
  455. //return fPeriodSize * sizeof(sample_t); // in bytes; TODO
  456. return 0;
  457. }
  458. //network<->buffer
  459. int NetIntAudioBuffer::RenderFromNetwork(int cycle, int subcycle, size_t copy_size)
  460. {
  461. if (subcycle == fNumPackets - 1) {
  462. for (int port_index = 0; port_index < fNPorts; port_index++)
  463. memcpy(fIntBuffer[port_index] + subcycle * fSubPeriodSize, fNetBuffer + port_index * fLastSubPeriodBytesSize, fLastSubPeriodBytesSize);
  464. } else {
  465. for (int port_index = 0; port_index < fNPorts; port_index++)
  466. memcpy(fIntBuffer[port_index] + subcycle * fSubPeriodSize, fNetBuffer + port_index * fSubPeriodBytesSize, fSubPeriodBytesSize);
  467. }
  468. if (subcycle != fLastSubCycle + 1)
  469. jack_error("Packet(s) missing from... %d %d", fLastSubCycle, subcycle);
  470. fLastSubCycle = subcycle;
  471. return copy_size;
  472. }
  473. int NetIntAudioBuffer::RenderToNetwork(int subcycle, size_t total_size)
  474. {
  475. if (subcycle == fNumPackets - 1) {
  476. for (int port_index = 0; port_index < fNPorts; port_index++)
  477. memcpy(fNetBuffer + port_index * fLastSubPeriodBytesSize, fIntBuffer[port_index] + subcycle * fSubPeriodSize, fLastSubPeriodBytesSize);
  478. return fNPorts * fLastSubPeriodBytesSize;
  479. } else {
  480. for (int port_index = 0; port_index < fNPorts; port_index++)
  481. memcpy(fNetBuffer + port_index * fSubPeriodBytesSize, fIntBuffer[port_index] + subcycle * fSubPeriodSize, fSubPeriodBytesSize);
  482. return fNPorts * fSubPeriodBytesSize;
  483. }
  484. }
  485. // Buffered
  486. /*
  487. NetBufferedAudioBuffer::NetBufferedAudioBuffer ( session_params_t* params, uint32_t nports, char* net_buffer )
  488. {
  489. fMaxCycle = 0;
  490. fNetBuffer = net_buffer;
  491. for (int i = 0; i < AUDIO_BUFFER_SIZE; i++) {
  492. fPortBuffer[i].Init(params, nports);
  493. }
  494. fJackPortBuffer = new sample_t* [nports];
  495. for ( uint32_t port_index = 0; port_index < nports; port_index++ )
  496. fJackPortBuffer[port_index] = NULL;
  497. }
  498. NetBufferedAudioBuffer::~NetBufferedAudioBuffer()
  499. {
  500. delete [] fJackPortBuffer;
  501. }
  502. size_t NetBufferedAudioBuffer::GetCycleSize()
  503. {
  504. return fPortBuffer[0].GetCycleSize();
  505. }
  506. void NetBufferedAudioBuffer::SetBuffer ( int index, sample_t* buffer )
  507. {
  508. fJackPortBuffer[index] = buffer;
  509. }
  510. sample_t* NetBufferedAudioBuffer::GetBuffer ( int index )
  511. {
  512. return fJackPortBuffer[index];
  513. }
  514. void NetBufferedAudioBuffer::RenderFromJackPorts (int subcycle )
  515. {
  516. fPortBuffer[0].RenderFromJackPorts(fNetBuffer, subcycle); // Always use first buffer...
  517. }
  518. void NetBufferedAudioBuffer::RenderToJackPorts (int cycle, int subcycle)
  519. {
  520. if (cycle < fMaxCycle) {
  521. jack_info("Wrong order fCycle %d subcycle %d fMaxCycle %d", cycle, subcycle, fMaxCycle);
  522. }
  523. fPortBuffer[cycle % AUDIO_BUFFER_SIZE].RenderToJackPorts(fNetBuffer, subcycle);
  524. }
  525. void NetBufferedAudioBuffer::FinishRenderToJackPorts (int cycle)
  526. {
  527. fMaxCycle = std::max(fMaxCycle, cycle);
  528. fPortBuffer[(cycle + 1) % AUDIO_BUFFER_SIZE].Copy(fJackPortBuffer); // Copy internal buffer in JACK ports
  529. }
  530. */
  531. // SessionParams ************************************************************************************
  532. SERVER_EXPORT void SessionParamsHToN ( session_params_t* src_params, session_params_t* dst_params )
  533. {
  534. memcpy(dst_params, src_params, sizeof(session_params_t));
  535. dst_params->fPacketID = htonl ( src_params->fPacketID );
  536. dst_params->fMtu = htonl ( src_params->fMtu );
  537. dst_params->fID = htonl ( src_params->fID );
  538. dst_params->fTransportSync = htonl ( src_params->fTransportSync );
  539. dst_params->fSendAudioChannels = htonl ( src_params->fSendAudioChannels );
  540. dst_params->fReturnAudioChannels = htonl ( src_params->fReturnAudioChannels );
  541. dst_params->fSendMidiChannels = htonl ( src_params->fSendMidiChannels );
  542. dst_params->fReturnMidiChannels = htonl ( src_params->fReturnMidiChannels );
  543. dst_params->fSampleRate = htonl ( src_params->fSampleRate );
  544. dst_params->fPeriodSize = htonl ( src_params->fPeriodSize );
  545. dst_params->fSampleEncoder = htonl ( src_params->fSampleEncoder );
  546. dst_params->fSlaveSyncMode = htonl ( src_params->fSlaveSyncMode );
  547. }
  548. SERVER_EXPORT void SessionParamsNToH ( session_params_t* src_params, session_params_t* dst_params )
  549. {
  550. memcpy(dst_params, src_params, sizeof(session_params_t));
  551. dst_params->fPacketID = ntohl ( src_params->fPacketID );
  552. dst_params->fMtu = ntohl ( src_params->fMtu );
  553. dst_params->fID = ntohl ( src_params->fID );
  554. dst_params->fTransportSync = ntohl ( src_params->fTransportSync );
  555. dst_params->fSendAudioChannels = ntohl ( src_params->fSendAudioChannels );
  556. dst_params->fReturnAudioChannels = ntohl ( src_params->fReturnAudioChannels );
  557. dst_params->fSendMidiChannels = ntohl ( src_params->fSendMidiChannels );
  558. dst_params->fReturnMidiChannels = ntohl ( src_params->fReturnMidiChannels );
  559. dst_params->fSampleRate = ntohl ( src_params->fSampleRate );
  560. dst_params->fPeriodSize = ntohl ( src_params->fPeriodSize );
  561. dst_params->fSampleEncoder = ntohl ( src_params->fSampleEncoder );
  562. dst_params->fSlaveSyncMode = ntohl ( src_params->fSlaveSyncMode );
  563. }
  564. SERVER_EXPORT void SessionParamsDisplay ( session_params_t* params )
  565. {
  566. char encoder[16];
  567. switch ( params->fSampleEncoder )
  568. {
  569. case JackFloatEncoder:
  570. strcpy ( encoder, "float" );
  571. break;
  572. case JackIntEncoder:
  573. strcpy ( encoder, "integer" );
  574. break;
  575. case JackCeltEncoder:
  576. strcpy ( encoder, "CELT" );
  577. break;
  578. }
  579. char mode[8];
  580. switch ( params->fNetworkMode )
  581. {
  582. case 's' :
  583. strcpy ( mode, "slow" );
  584. break;
  585. case 'n' :
  586. strcpy ( mode, "normal" );
  587. break;
  588. case 'f' :
  589. strcpy ( mode, "fast" );
  590. break;
  591. }
  592. jack_info ( "**************** Network parameters ****************" );
  593. jack_info ( "Name : %s", params->fName );
  594. jack_info ( "Protocol revision : %d", params->fProtocolVersion );
  595. jack_info ( "MTU : %u", params->fMtu );
  596. jack_info ( "Master name : %s", params->fMasterNetName );
  597. jack_info ( "Slave name : %s", params->fSlaveNetName );
  598. jack_info ( "ID : %u", params->fID );
  599. jack_info ( "Transport Sync : %s", ( params->fTransportSync ) ? "yes" : "no" );
  600. jack_info ( "Send channels (audio - midi) : %d - %d", params->fSendAudioChannels, params->fSendMidiChannels );
  601. jack_info ( "Return channels (audio - midi) : %d - %d", params->fReturnAudioChannels, params->fReturnMidiChannels );
  602. jack_info ( "Sample rate : %u frames per second", params->fSampleRate );
  603. jack_info ( "Period size : %u frames per period", params->fPeriodSize );
  604. switch (params->fSampleEncoder) {
  605. case (JackFloatEncoder):
  606. jack_info ( "SampleEncoder : %s", "Float" );
  607. break;
  608. case (JackIntEncoder):
  609. jack_info ( "SampleEncoder : %s", "16 bits integer");
  610. break;
  611. case (JackCeltEncoder):
  612. jack_info ( "SampleEncoder : %s", "CELT");
  613. jack_info ( "kBits : %d", params->fKBps);
  614. break;
  615. };
  616. jack_info ( "Slave mode : %s", ( params->fSlaveSyncMode ) ? "sync" : "async" );
  617. jack_info ( "Network mode : %s", mode );
  618. jack_info ( "****************************************************" );
  619. }
  620. SERVER_EXPORT sync_packet_type_t GetPacketType ( session_params_t* params )
  621. {
  622. switch ( params->fPacketID )
  623. {
  624. case 0:
  625. return SLAVE_AVAILABLE;
  626. case 1:
  627. return SLAVE_SETUP;
  628. case 2:
  629. return START_MASTER;
  630. case 3:
  631. return START_SLAVE;
  632. case 4:
  633. return KILL_MASTER;
  634. }
  635. return INVALID;
  636. }
  637. SERVER_EXPORT int SetPacketType ( session_params_t* params, sync_packet_type_t packet_type )
  638. {
  639. switch ( packet_type )
  640. {
  641. case INVALID:
  642. return -1;
  643. case SLAVE_AVAILABLE:
  644. params->fPacketID = 0;
  645. break;
  646. case SLAVE_SETUP:
  647. params->fPacketID = 1;
  648. break;
  649. case START_MASTER:
  650. params->fPacketID = 2;
  651. break;
  652. case START_SLAVE:
  653. params->fPacketID = 3;
  654. break;
  655. case KILL_MASTER:
  656. params->fPacketID = 4;
  657. }
  658. return 0;
  659. }
  660. // Packet header **********************************************************************************
  661. SERVER_EXPORT void PacketHeaderHToN ( packet_header_t* src_header, packet_header_t* dst_header )
  662. {
  663. memcpy(dst_header, src_header, sizeof(packet_header_t));
  664. dst_header->fID = htonl ( src_header->fID );
  665. dst_header->fNumPacket = htonl ( src_header->fNumPacket );
  666. dst_header->fPacketSize = htonl ( src_header->fPacketSize );
  667. dst_header->fCycle = htonl ( src_header->fCycle );
  668. dst_header->fSubCycle = htonl ( src_header->fSubCycle );
  669. dst_header->fIsLastPckt = htonl ( src_header->fIsLastPckt );
  670. }
  671. SERVER_EXPORT void PacketHeaderNToH ( packet_header_t* src_header, packet_header_t* dst_header )
  672. {
  673. memcpy(dst_header, src_header, sizeof(packet_header_t));
  674. dst_header->fID = ntohl ( src_header->fID );
  675. dst_header->fNumPacket = ntohl ( src_header->fNumPacket );
  676. dst_header->fPacketSize = ntohl ( src_header->fPacketSize );
  677. dst_header->fCycle = ntohl ( src_header->fCycle );
  678. dst_header->fSubCycle = ntohl ( src_header->fSubCycle );
  679. dst_header->fIsLastPckt = ntohl ( src_header->fIsLastPckt );
  680. }
  681. SERVER_EXPORT void PacketHeaderDisplay ( packet_header_t* header )
  682. {
  683. char bitdepth[16];
  684. jack_info ( "********************Header********************" );
  685. jack_info ( "Data type : %c", header->fDataType );
  686. jack_info ( "Data stream : %c", header->fDataStream );
  687. jack_info ( "ID : %u", header->fID );
  688. jack_info ( "Cycle : %u", header->fCycle );
  689. jack_info ( "SubCycle : %u", header->fSubCycle );
  690. jack_info ( "Midi packets : %u", header->fNumPacket );
  691. jack_info ( "Last packet : '%s'", ( header->fIsLastPckt ) ? "yes" : "no" );
  692. jack_info ( "Bitdepth : %s", bitdepth );
  693. jack_info ( "**********************************************" );
  694. }
  695. SERVER_EXPORT void NetTransportDataDisplay ( net_transport_data_t* data )
  696. {
  697. jack_info ( "********************Network Transport********************" );
  698. jack_info ( "Transport new state : %u", data->fNewState );
  699. jack_info ( "Transport timebase master : %u", data->fTimebaseMaster );
  700. jack_info ( "Transport cycle state : %u", data->fState );
  701. jack_info ( "**********************************************" );
  702. }
  703. SERVER_EXPORT void MidiBufferHToN ( JackMidiBuffer* src_buffer, JackMidiBuffer* dst_buffer )
  704. {
  705. dst_buffer->magic = htonl(src_buffer->magic);
  706. dst_buffer->buffer_size = htonl(src_buffer->buffer_size);
  707. dst_buffer->nframes = htonl(src_buffer->nframes);
  708. dst_buffer->write_pos = htonl(src_buffer->write_pos);
  709. dst_buffer->event_count = htonl(src_buffer->event_count);
  710. dst_buffer->lost_events = htonl(src_buffer->lost_events);
  711. dst_buffer->mix_index = htonl(src_buffer->mix_index);
  712. }
  713. SERVER_EXPORT void MidiBufferNToH ( JackMidiBuffer* src_buffer, JackMidiBuffer* dst_buffer )
  714. {
  715. dst_buffer->magic = ntohl(src_buffer->magic);
  716. dst_buffer->buffer_size = ntohl(src_buffer->buffer_size);
  717. dst_buffer->nframes = ntohl(src_buffer->nframes);
  718. dst_buffer->write_pos = ntohl(src_buffer->write_pos);
  719. dst_buffer->event_count = ntohl(src_buffer->event_count);
  720. dst_buffer->lost_events = ntohl(src_buffer->lost_events);
  721. dst_buffer->mix_index = ntohl(src_buffer->mix_index);
  722. }
  723. SERVER_EXPORT void TransportDataHToN ( net_transport_data_t* src_params, net_transport_data_t* dst_params )
  724. {
  725. dst_params->fNewState = htonl(src_params->fNewState);
  726. dst_params->fTimebaseMaster = htonl(src_params->fTimebaseMaster);
  727. dst_params->fState = htonl(src_params->fState);
  728. dst_params->fPosition.unique_1 = htonll(src_params->fPosition.unique_1);
  729. dst_params->fPosition.usecs = htonl(src_params->fPosition.usecs);
  730. dst_params->fPosition.frame_rate = htonl(src_params->fPosition.frame_rate);
  731. dst_params->fPosition.frame = htonl(src_params->fPosition.frame);
  732. dst_params->fPosition.valid = (jack_position_bits_t)htonl((uint32_t)src_params->fPosition.valid);
  733. dst_params->fPosition.bar = htonl(src_params->fPosition.bar);
  734. dst_params->fPosition.beat = htonl(src_params->fPosition.beat);
  735. dst_params->fPosition.tick = htonl(src_params->fPosition.tick);
  736. dst_params->fPosition.bar_start_tick = htonll((uint64_t)src_params->fPosition.bar_start_tick);
  737. dst_params->fPosition.beats_per_bar = htonl((uint32_t)src_params->fPosition.beats_per_bar);
  738. dst_params->fPosition.beat_type = htonl((uint32_t)src_params->fPosition.beat_type);
  739. dst_params->fPosition.ticks_per_beat = htonll((uint64_t)src_params->fPosition.ticks_per_beat);
  740. dst_params->fPosition.beats_per_minute = htonll((uint64_t)src_params->fPosition.beats_per_minute);
  741. dst_params->fPosition.frame_time = htonll((uint64_t)src_params->fPosition.frame_time);
  742. dst_params->fPosition.next_time = htonll((uint64_t)src_params->fPosition.next_time);
  743. dst_params->fPosition.bbt_offset = htonl(src_params->fPosition.bbt_offset);
  744. dst_params->fPosition.audio_frames_per_video_frame = htonl((uint32_t)src_params->fPosition.audio_frames_per_video_frame);
  745. dst_params->fPosition.video_offset = htonl(src_params->fPosition.video_offset);
  746. dst_params->fPosition.unique_2 = htonll(src_params->fPosition.unique_2);
  747. }
  748. SERVER_EXPORT void TransportDataNToH ( net_transport_data_t* src_params, net_transport_data_t* dst_params )
  749. {
  750. dst_params->fNewState = ntohl(src_params->fNewState);
  751. dst_params->fTimebaseMaster = ntohl(src_params->fTimebaseMaster);
  752. dst_params->fState = ntohl(src_params->fState);
  753. dst_params->fPosition.unique_1 = ntohll(src_params->fPosition.unique_1);
  754. dst_params->fPosition.usecs = ntohl(src_params->fPosition.usecs);
  755. dst_params->fPosition.frame_rate = ntohl(src_params->fPosition.frame_rate);
  756. dst_params->fPosition.frame = ntohl(src_params->fPosition.frame);
  757. dst_params->fPosition.valid = (jack_position_bits_t)ntohl((uint32_t)src_params->fPosition.valid);
  758. dst_params->fPosition.bar = ntohl(src_params->fPosition.bar);
  759. dst_params->fPosition.beat = ntohl(src_params->fPosition.beat);
  760. dst_params->fPosition.tick = ntohl(src_params->fPosition.tick);
  761. dst_params->fPosition.bar_start_tick = ntohll((uint64_t)src_params->fPosition.bar_start_tick);
  762. dst_params->fPosition.beats_per_bar = ntohl((uint32_t)src_params->fPosition.beats_per_bar);
  763. dst_params->fPosition.beat_type = ntohl((uint32_t)src_params->fPosition.beat_type);
  764. dst_params->fPosition.ticks_per_beat = ntohll((uint64_t)src_params->fPosition.ticks_per_beat);
  765. dst_params->fPosition.beats_per_minute = ntohll((uint64_t)src_params->fPosition.beats_per_minute);
  766. dst_params->fPosition.frame_time = ntohll((uint64_t)src_params->fPosition.frame_time);
  767. dst_params->fPosition.next_time = ntohll((uint64_t)src_params->fPosition.next_time);
  768. dst_params->fPosition.bbt_offset = ntohl(src_params->fPosition.bbt_offset);
  769. dst_params->fPosition.audio_frames_per_video_frame = ntohl((uint32_t)src_params->fPosition.audio_frames_per_video_frame);
  770. dst_params->fPosition.video_offset = ntohl(src_params->fPosition.video_offset);
  771. dst_params->fPosition.unique_2 = ntohll(src_params->fPosition.unique_2);
  772. }
  773. // Utility *******************************************************************************************************
  774. SERVER_EXPORT int SocketAPIInit()
  775. {
  776. #ifdef WIN32
  777. WORD wVersionRequested = MAKEWORD ( 2, 2 );
  778. WSADATA wsaData;
  779. if ( WSAStartup ( wVersionRequested, &wsaData ) != 0 )
  780. {
  781. jack_error ( "WSAStartup error : %s", strerror ( NET_ERROR_CODE ) );
  782. return -1;
  783. }
  784. if ( LOBYTE ( wsaData.wVersion ) != 2 || HIBYTE ( wsaData.wVersion ) != 2 )
  785. {
  786. jack_error ( "Could not find a useable version of Winsock.dll\n" );
  787. WSACleanup();
  788. return -1;
  789. }
  790. #endif
  791. return 0;
  792. }
  793. SERVER_EXPORT int SocketAPIEnd()
  794. {
  795. #ifdef WIN32
  796. return WSACleanup();
  797. #endif
  798. return 0;
  799. }
  800. SERVER_EXPORT const char* GetTransportState ( int transport_state )
  801. {
  802. switch ( transport_state )
  803. {
  804. case JackTransportRolling:
  805. return "rolling";
  806. case JackTransportStarting:
  807. return "starting";
  808. case JackTransportStopped:
  809. return "stopped";
  810. case JackTransportNetStarting:
  811. return "netstarting";
  812. }
  813. return NULL;
  814. }
  815. }