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.

419 lines
16KB

  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. using namespace std;
  17. namespace Jack
  18. {
  19. // NetMidiBuffer**********************************************************************************
  20. NetMidiBuffer::NetMidiBuffer ( session_params_t* params, uint32_t nports, char* net_buffer )
  21. {
  22. fNPorts = nports;
  23. fMaxBufsize = fNPorts * sizeof ( sample_t ) * params->fPeriodSize ;
  24. fMaxPcktSize = params->fMtu - sizeof ( packet_header_t );
  25. fBuffer = new char[fMaxBufsize];
  26. fPortBuffer = new JackMidiBuffer* [fNPorts];
  27. for ( int port_index = 0; port_index < fNPorts; port_index++ )
  28. fPortBuffer[port_index] = NULL;
  29. fNetBuffer = net_buffer;
  30. }
  31. NetMidiBuffer::~NetMidiBuffer()
  32. {
  33. delete[] fBuffer;
  34. delete[] fPortBuffer;
  35. }
  36. size_t NetMidiBuffer::GetSize()
  37. {
  38. return fMaxBufsize;
  39. }
  40. void NetMidiBuffer::SetBuffer ( int index, JackMidiBuffer* buffer )
  41. {
  42. fPortBuffer[index] = buffer;
  43. }
  44. JackMidiBuffer* NetMidiBuffer::GetBuffer ( int index )
  45. {
  46. return fPortBuffer[index];
  47. }
  48. void NetMidiBuffer::DisplayEvents()
  49. {
  50. for ( int port_index = 0; port_index < fNPorts; port_index++ )
  51. {
  52. for ( uint event = 0; event < fPortBuffer[port_index]->event_count; event++ )
  53. if ( fPortBuffer[port_index]->IsValid() )
  54. jack_info ( "port %d : midi event %u/%u -> time : %u, size : %u",
  55. port_index + 1, event + 1, fPortBuffer[port_index]->event_count,
  56. fPortBuffer[port_index]->events[event].time, fPortBuffer[port_index]->events[event].size );
  57. }
  58. }
  59. int NetMidiBuffer::RenderFromJackPorts()
  60. {
  61. int pos = 0;
  62. size_t copy_size;
  63. for ( int port_index = 0; port_index < fNPorts; port_index++ )
  64. {
  65. copy_size = sizeof ( JackMidiBuffer ) + fPortBuffer[port_index]->event_count * sizeof ( JackMidiEvent );
  66. memcpy ( fBuffer + pos, fPortBuffer[port_index], copy_size );
  67. pos += copy_size;
  68. memcpy ( fBuffer + pos, fPortBuffer[port_index] + ( fPortBuffer[port_index]->buffer_size - fPortBuffer[port_index]->write_pos ),
  69. fPortBuffer[port_index]->write_pos );
  70. pos += fPortBuffer[port_index]->write_pos;
  71. }
  72. return pos;
  73. }
  74. int NetMidiBuffer::RenderToJackPorts()
  75. {
  76. int pos = 0;
  77. int copy_size;
  78. for ( int port_index = 0; port_index < fNPorts; port_index++ )
  79. {
  80. copy_size = sizeof ( JackMidiBuffer ) + reinterpret_cast<JackMidiBuffer*> ( fBuffer + pos )->event_count * sizeof ( JackMidiEvent );
  81. memcpy ( fPortBuffer[port_index], fBuffer + pos, copy_size );
  82. pos += copy_size;
  83. memcpy ( fPortBuffer[port_index] + ( fPortBuffer[port_index]->buffer_size - fPortBuffer[port_index]->write_pos ),
  84. fBuffer + pos, fPortBuffer[port_index]->write_pos );
  85. pos += fPortBuffer[port_index]->write_pos;
  86. }
  87. return pos;
  88. }
  89. int NetMidiBuffer::RenderFromNetwork ( int subcycle, size_t copy_size )
  90. {
  91. memcpy ( fBuffer + subcycle * fMaxPcktSize, fNetBuffer, copy_size );
  92. return copy_size;
  93. }
  94. int NetMidiBuffer::RenderToNetwork ( int subcycle, size_t total_size )
  95. {
  96. int size = total_size - subcycle * fMaxPcktSize;
  97. int copy_size = ( size <= fMaxPcktSize ) ? size : fMaxPcktSize;
  98. memcpy ( fNetBuffer, fBuffer + subcycle * fMaxPcktSize, copy_size );
  99. return copy_size;
  100. }
  101. // net audio buffer *********************************************************************************
  102. NetAudioBuffer::NetAudioBuffer ( session_params_t* params, uint32_t nports, char* net_buffer )
  103. {
  104. fNPorts = nports;
  105. fPeriodSize = params->fPeriodSize;
  106. fSubPeriodSize = params->fFramesPerPacket;
  107. fSubPeriodBytesSize = fSubPeriodSize * sizeof ( sample_t );
  108. fPortBuffer = new sample_t* [fNPorts];
  109. for ( int port_index = 0; port_index < fNPorts; port_index++ )
  110. fPortBuffer[port_index] = NULL;
  111. fNetBuffer = net_buffer;
  112. }
  113. NetAudioBuffer::~NetAudioBuffer()
  114. {
  115. delete[] fPortBuffer;
  116. }
  117. size_t NetAudioBuffer::GetSize()
  118. {
  119. return fNPorts * fSubPeriodBytesSize;
  120. }
  121. void NetAudioBuffer::SetBuffer ( int index, sample_t* buffer )
  122. {
  123. fPortBuffer[index] = buffer;
  124. }
  125. sample_t* NetAudioBuffer::GetBuffer ( int index )
  126. {
  127. return fPortBuffer[index];
  128. }
  129. #ifdef BIG_ENDIAN
  130. static inline float SwapFloat(float f)
  131. {
  132. union
  133. {
  134. float f;
  135. unsigned char b[4];
  136. } dat1, dat2;
  137. dat1.f = f;
  138. dat2.b[0] = dat1.b[3];
  139. dat2.b[1] = dat1.b[2];
  140. dat2.b[2] = dat1.b[1];
  141. dat2.b[3] = dat1.b[0];
  142. return dat2.f;
  143. }
  144. void NetAudioBuffer::RenderFromJackPorts ( int subcycle )
  145. {
  146. for ( int port_index = 0; port_index < fNPorts; port_index++ ) {
  147. float* src = (float*)(fPortBuffer[port_index] + subcycle * fSubPeriodSize);
  148. float* dst = (float*)(fNetBuffer + port_index * fSubPeriodBytesSize);
  149. for (unsigned int sample = 0; sample < fSubPeriodBytesSize / sizeof(float); sample++) {
  150. dst[sample] = SwapFloat(src[sample]);
  151. }
  152. }
  153. }
  154. void NetAudioBuffer::RenderToJackPorts ( int subcycle )
  155. {
  156. for ( int port_index = 0; port_index < fNPorts; port_index++ ) {
  157. float* src = (float*)(fNetBuffer + port_index * fSubPeriodBytesSize);
  158. float* dst = (float*)(fPortBuffer[port_index] + subcycle * fSubPeriodSize);
  159. for (unsigned int sample = 0; sample < fSubPeriodBytesSize / sizeof(float); sample++) {
  160. dst[sample] = SwapFloat(src[sample]);
  161. }
  162. }
  163. }
  164. #else
  165. void NetAudioBuffer::RenderFromJackPorts ( int subcycle )
  166. {
  167. for ( int port_index = 0; port_index < fNPorts; port_index++ )
  168. memcpy ( fNetBuffer + port_index * fSubPeriodBytesSize, fPortBuffer[port_index] + subcycle * fSubPeriodSize, fSubPeriodBytesSize );
  169. }
  170. void NetAudioBuffer::RenderToJackPorts ( int subcycle )
  171. {
  172. for ( int port_index = 0; port_index < fNPorts; port_index++ )
  173. memcpy ( fPortBuffer[port_index] + subcycle * fSubPeriodSize, fNetBuffer + port_index * fSubPeriodBytesSize, fSubPeriodBytesSize );
  174. }
  175. #endif
  176. // SessionParams ************************************************************************************
  177. SERVER_EXPORT void SessionParamsHToN ( session_params_t* src_params, session_params_t* dst_params )
  178. {
  179. memcpy(dst_params, src_params, sizeof(session_params_t));
  180. dst_params->fPacketID = htonl ( src_params->fPacketID );
  181. dst_params->fMtu = htonl ( src_params->fMtu );
  182. dst_params->fID = htonl ( src_params->fID );
  183. dst_params->fTransportSync = htonl ( src_params->fTransportSync );
  184. dst_params->fSendAudioChannels = htonl ( src_params->fSendAudioChannels );
  185. dst_params->fReturnAudioChannels = htonl ( src_params->fReturnAudioChannels );
  186. dst_params->fSendMidiChannels = htonl ( src_params->fSendMidiChannels );
  187. dst_params->fReturnMidiChannels = htonl ( src_params->fReturnMidiChannels );
  188. dst_params->fSampleRate = htonl ( src_params->fSampleRate );
  189. dst_params->fPeriodSize = htonl ( src_params->fPeriodSize );
  190. dst_params->fFramesPerPacket = htonl ( src_params->fFramesPerPacket );
  191. dst_params->fBitdepth = htonl ( src_params->fBitdepth );
  192. dst_params->fSlaveSyncMode = htonl ( src_params->fSlaveSyncMode );
  193. }
  194. SERVER_EXPORT void SessionParamsNToH ( session_params_t* src_params, session_params_t* dst_params )
  195. {
  196. memcpy(dst_params, src_params, sizeof(session_params_t));
  197. dst_params->fPacketID = ntohl ( src_params->fPacketID );
  198. dst_params->fMtu = ntohl ( src_params->fMtu );
  199. dst_params->fID = ntohl ( src_params->fID );
  200. dst_params->fTransportSync = ntohl ( src_params->fTransportSync );
  201. dst_params->fSendAudioChannels = ntohl ( src_params->fSendAudioChannels );
  202. dst_params->fReturnAudioChannels = ntohl ( src_params->fReturnAudioChannels );
  203. dst_params->fSendMidiChannels = ntohl ( src_params->fSendMidiChannels );
  204. dst_params->fReturnMidiChannels = ntohl ( src_params->fReturnMidiChannels );
  205. dst_params->fSampleRate = ntohl ( src_params->fSampleRate );
  206. dst_params->fPeriodSize = ntohl ( src_params->fPeriodSize );
  207. dst_params->fFramesPerPacket = ntohl ( src_params->fFramesPerPacket );
  208. dst_params->fBitdepth = ntohl ( src_params->fBitdepth );
  209. dst_params->fSlaveSyncMode = ntohl ( src_params->fSlaveSyncMode );
  210. }
  211. SERVER_EXPORT void SessionParamsDisplay ( session_params_t* params )
  212. {
  213. char bitdepth[16];
  214. ( params->fBitdepth ) ? sprintf ( bitdepth, "%u", params->fBitdepth ) : sprintf ( bitdepth, "%s", "float" );
  215. char mode[8];
  216. switch ( params->fNetworkMode )
  217. {
  218. case 's' :
  219. strcpy ( mode, "slow" );
  220. break;
  221. case 'n' :
  222. strcpy ( mode, "normal" );
  223. break;
  224. case 'f' :
  225. strcpy ( mode, "fast" );
  226. break;
  227. }
  228. jack_info ( "**************** Network parameters ****************" );
  229. jack_info ( "Name : %s", params->fName );
  230. jack_info ( "Protocol revision : %c", params->fProtocolVersion );
  231. jack_info ( "MTU : %u", params->fMtu );
  232. jack_info ( "Master name : %s", params->fMasterNetName );
  233. jack_info ( "Slave name : %s", params->fSlaveNetName );
  234. jack_info ( "ID : %u", params->fID );
  235. jack_info ( "Transport Sync : %s", ( params->fTransportSync ) ? "yes" : "no" );
  236. jack_info ( "Send channels (audio - midi) : %d - %d", params->fSendAudioChannels, params->fSendMidiChannels );
  237. jack_info ( "Return channels (audio - midi) : %d - %d", params->fReturnAudioChannels, params->fReturnMidiChannels );
  238. jack_info ( "Sample rate : %u frames per second", params->fSampleRate );
  239. jack_info ( "Period size : %u frames per period", params->fPeriodSize );
  240. jack_info ( "Frames per packet : %u", params->fFramesPerPacket );
  241. jack_info ( "Packet per period : %u", params->fPeriodSize / params->fFramesPerPacket );
  242. jack_info ( "Bitdepth : %s", bitdepth );
  243. jack_info ( "Slave mode : %s", ( params->fSlaveSyncMode ) ? "sync" : "async" );
  244. jack_info ( "Network mode : %s", mode );
  245. jack_info ( "****************************************************" );
  246. }
  247. SERVER_EXPORT sync_packet_type_t GetPacketType ( session_params_t* params )
  248. {
  249. switch ( params->fPacketID )
  250. {
  251. case 0:
  252. return SLAVE_AVAILABLE;
  253. case 1:
  254. return SLAVE_SETUP;
  255. case 2:
  256. return START_MASTER;
  257. case 3:
  258. return START_SLAVE;
  259. case 4:
  260. return KILL_MASTER;
  261. }
  262. return INVALID;
  263. }
  264. SERVER_EXPORT int SetPacketType ( session_params_t* params, sync_packet_type_t packet_type )
  265. {
  266. switch ( packet_type )
  267. {
  268. case INVALID:
  269. return -1;
  270. case SLAVE_AVAILABLE:
  271. params->fPacketID = 0;
  272. break;
  273. case SLAVE_SETUP:
  274. params->fPacketID = 1;
  275. break;
  276. case START_MASTER:
  277. params->fPacketID = 2;
  278. break;
  279. case START_SLAVE:
  280. params->fPacketID = 3;
  281. break;
  282. case KILL_MASTER:
  283. params->fPacketID = 4;
  284. }
  285. return 0;
  286. }
  287. // Packet header **********************************************************************************
  288. SERVER_EXPORT void PacketHeaderHToN ( packet_header_t* src_header, packet_header_t* dst_header )
  289. {
  290. memcpy(dst_header, src_header, sizeof(packet_header_t));
  291. dst_header->fID = htonl ( src_header->fID );
  292. dst_header->fMidiDataSize = htonl ( src_header->fMidiDataSize );
  293. dst_header->fBitdepth = htonl ( src_header->fBitdepth );
  294. dst_header->fNMidiPckt = htonl ( src_header->fNMidiPckt );
  295. dst_header->fPacketSize = htonl ( src_header->fPacketSize );
  296. dst_header->fCycle = htonl ( src_header->fCycle );
  297. dst_header->fSubCycle = htonl ( src_header->fSubCycle );
  298. dst_header->fIsLastPckt = htonl ( src_header->fIsLastPckt );
  299. }
  300. SERVER_EXPORT void PacketHeaderNToH ( packet_header_t* src_header, packet_header_t* dst_header )
  301. {
  302. memcpy(dst_header, src_header, sizeof(packet_header_t));
  303. dst_header->fID = ntohl ( src_header->fID );
  304. dst_header->fMidiDataSize = ntohl ( src_header->fMidiDataSize );
  305. dst_header->fBitdepth = ntohl ( src_header->fBitdepth );
  306. dst_header->fNMidiPckt = ntohl ( src_header->fNMidiPckt );
  307. dst_header->fPacketSize = ntohl ( src_header->fPacketSize );
  308. dst_header->fCycle = ntohl ( src_header->fCycle );
  309. dst_header->fSubCycle = ntohl ( src_header->fSubCycle );
  310. dst_header->fIsLastPckt = ntohl ( src_header->fIsLastPckt );
  311. }
  312. SERVER_EXPORT void PacketHeaderDisplay ( packet_header_t* header )
  313. {
  314. char bitdepth[16];
  315. ( header->fBitdepth ) ? sprintf ( bitdepth, "%u", header->fBitdepth ) : sprintf ( bitdepth, "%s", "float" );
  316. jack_info ( "********************Header********************" );
  317. jack_info ( "Data type : %c", header->fDataType );
  318. jack_info ( "Data stream : %c", header->fDataStream );
  319. jack_info ( "ID : %u", header->fID );
  320. jack_info ( "Cycle : %u", header->fCycle );
  321. jack_info ( "SubCycle : %u", header->fSubCycle );
  322. jack_info ( "Midi packets : %u", header->fNMidiPckt );
  323. jack_info ( "Midi data size : %u", header->fMidiDataSize );
  324. jack_info ( "Last packet : '%s'", ( header->fIsLastPckt ) ? "yes" : "no" );
  325. jack_info ( "Bitdepth : %s", bitdepth );
  326. jack_info ( "**********************************************" );
  327. }
  328. // Utility *******************************************************************************************************
  329. SERVER_EXPORT int SocketAPIInit()
  330. {
  331. #ifdef WIN32
  332. WORD wVersionRequested = MAKEWORD ( 2, 2 );
  333. WSADATA wsaData;
  334. if ( WSAStartup ( wVersionRequested, &wsaData ) != 0 )
  335. {
  336. jack_error ( "WSAStartup error : %s", strerror ( NET_ERROR_CODE ) );
  337. return -1;
  338. }
  339. if ( LOBYTE ( wsaData.wVersion ) != 2 || HIBYTE ( wsaData.wVersion ) != 2 )
  340. {
  341. jack_error ( "Could not find a useable version of Winsock.dll\n" );
  342. WSACleanup();
  343. return -1;
  344. }
  345. #endif
  346. return 0;
  347. }
  348. SERVER_EXPORT int SocketAPIEnd()
  349. {
  350. #ifdef WIN32
  351. return WSACleanup();
  352. #endif
  353. return 0;
  354. }
  355. SERVER_EXPORT const char* GetTransportState ( int transport_state )
  356. {
  357. switch ( transport_state )
  358. {
  359. case JackTransportRolling:
  360. return "rolling";
  361. case JackTransportStarting:
  362. return "starting";
  363. case JackTransportStopped:
  364. return "stopped";
  365. case JackTransportNetStarting:
  366. return "netstarting";
  367. }
  368. return NULL;
  369. }
  370. }