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.

943 lines
35KB

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