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.

416 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. void NetAudioBuffer::RenderFromJackPorts ( int subcycle )
  130. {
  131. for ( int port_index = 0; port_index < fNPorts; port_index++ )
  132. memcpy ( fNetBuffer + port_index * fSubPeriodBytesSize, fPortBuffer[port_index] + subcycle * fSubPeriodSize, fSubPeriodBytesSize );
  133. }
  134. void NetAudioBuffer::RenderToJackPorts ( int subcycle )
  135. {
  136. for ( int port_index = 0; port_index < fNPorts; port_index++ )
  137. memcpy ( fPortBuffer[port_index] + subcycle * fSubPeriodSize, fNetBuffer + port_index * fSubPeriodBytesSize, fSubPeriodBytesSize );
  138. }
  139. // SessionParams ************************************************************************************
  140. EXPORT void SessionParamsHToN ( session_params_t* params )
  141. {
  142. params->fPacketID = htonl ( params->fPacketID );
  143. params->fMtu = htonl ( params->fMtu );
  144. params->fID = htonl ( params->fID );
  145. params->fTransportSync = htonl ( params->fTransportSync );
  146. params->fSendAudioChannels = htonl ( params->fSendAudioChannels );
  147. params->fReturnAudioChannels = htonl ( params->fReturnAudioChannels );
  148. params->fSendMidiChannels = htonl ( params->fSendMidiChannels );
  149. params->fReturnMidiChannels = htonl ( params->fReturnMidiChannels );
  150. params->fSampleRate = htonl ( params->fSampleRate );
  151. params->fPeriodSize = htonl ( params->fPeriodSize );
  152. params->fFramesPerPacket = htonl ( params->fFramesPerPacket );
  153. params->fBitdepth = htonl ( params->fBitdepth );
  154. params->fSlaveSyncMode = htonl ( params->fSlaveSyncMode );
  155. }
  156. EXPORT void SessionParamsNToH ( session_params_t* params )
  157. {
  158. params->fPacketID = ntohl ( params->fPacketID );
  159. params->fMtu = ntohl ( params->fMtu );
  160. params->fID = ntohl ( params->fID );
  161. params->fTransportSync = ntohl ( params->fTransportSync );
  162. params->fSendAudioChannels = ntohl ( params->fSendAudioChannels );
  163. params->fReturnAudioChannels = ntohl ( params->fReturnAudioChannels );
  164. params->fSendMidiChannels = ntohl ( params->fSendMidiChannels );
  165. params->fReturnMidiChannels = ntohl ( params->fReturnMidiChannels );
  166. params->fSampleRate = ntohl ( params->fSampleRate );
  167. params->fPeriodSize = ntohl ( params->fPeriodSize );
  168. params->fFramesPerPacket = ntohl ( params->fFramesPerPacket );
  169. params->fBitdepth = ntohl ( params->fBitdepth );
  170. params->fSlaveSyncMode = ntohl ( params->fSlaveSyncMode );
  171. }
  172. EXPORT void SessionParamsDisplay ( session_params_t* params )
  173. {
  174. char bitdepth[16];
  175. ( params->fBitdepth ) ? sprintf ( bitdepth, "%u", params->fBitdepth ) : sprintf ( bitdepth, "%s", "float" );
  176. char mode[8];
  177. switch ( params->fNetworkMode )
  178. {
  179. case 's' :
  180. strcpy ( mode, "slow" );
  181. break;
  182. case 'n' :
  183. strcpy ( mode, "normal" );
  184. break;
  185. case 'f' :
  186. strcpy ( mode, "fast" );
  187. break;
  188. }
  189. jack_info ( "**************** Network parameters ****************" );
  190. jack_info ( "Name : %s", params->fName );
  191. jack_info ( "Protocol revision : %c", params->fProtocolVersion );
  192. jack_info ( "MTU : %u", params->fMtu );
  193. jack_info ( "Master name : %s", params->fMasterNetName );
  194. jack_info ( "Slave name : %s", params->fSlaveNetName );
  195. jack_info ( "ID : %u", params->fID );
  196. jack_info ( "Transport Sync : %s", ( params->fTransportSync ) ? "yes" : "no" );
  197. jack_info ( "Send channels (audio - midi) : %d - %d", params->fSendAudioChannels, params->fSendMidiChannels );
  198. jack_info ( "Return channels (audio - midi) : %d - %d", params->fReturnAudioChannels, params->fReturnMidiChannels );
  199. jack_info ( "Sample rate : %u frames per second", params->fSampleRate );
  200. jack_info ( "Period size : %u frames per period", params->fPeriodSize );
  201. jack_info ( "Frames per packet : %u", params->fFramesPerPacket );
  202. jack_info ( "Packet per period : %u", params->fPeriodSize / params->fFramesPerPacket );
  203. jack_info ( "Bitdepth : %s", bitdepth );
  204. jack_info ( "Slave mode : %s", ( params->fSlaveSyncMode ) ? "sync" : "async" );
  205. jack_info ( "Network mode : %s", mode );
  206. jack_info ( "****************************************************" );
  207. }
  208. EXPORT sync_packet_type_t GetPacketType ( session_params_t* params )
  209. {
  210. switch ( params->fPacketID )
  211. {
  212. case 0:
  213. return SLAVE_AVAILABLE;
  214. case 1:
  215. return SLAVE_SETUP;
  216. case 2:
  217. return START_MASTER;
  218. case 3:
  219. return START_SLAVE;
  220. case 4:
  221. return KILL_MASTER;
  222. }
  223. return INVALID;
  224. }
  225. EXPORT int SetPacketType ( session_params_t* params, sync_packet_type_t packet_type )
  226. {
  227. switch ( packet_type )
  228. {
  229. case INVALID:
  230. return -1;
  231. case SLAVE_AVAILABLE:
  232. params->fPacketID = 0;
  233. break;
  234. case SLAVE_SETUP:
  235. params->fPacketID = 1;
  236. break;
  237. case START_MASTER:
  238. params->fPacketID = 2;
  239. break;
  240. case START_SLAVE:
  241. params->fPacketID = 3;
  242. break;
  243. case KILL_MASTER:
  244. params->fPacketID = 4;
  245. }
  246. return 0;
  247. }
  248. // Packet header **********************************************************************************
  249. EXPORT void PacketHeaderHToN ( packet_header_t* header )
  250. {
  251. header->fID = htonl ( header->fID );
  252. header->fMidiDataSize = htonl ( header->fMidiDataSize );
  253. header->fBitdepth = htonl ( header->fBitdepth );
  254. header->fNMidiPckt = htonl ( header->fNMidiPckt );
  255. header->fPacketSize = htonl ( header->fPacketSize );
  256. header->fCycle = ntohl ( header->fCycle );
  257. header->fSubCycle = htonl ( header->fSubCycle );
  258. }
  259. EXPORT void PacketHeaderNToH ( packet_header_t* header )
  260. {
  261. header->fID = ntohl ( header->fID );
  262. header->fMidiDataSize = ntohl ( header->fMidiDataSize );
  263. header->fBitdepth = ntohl ( header->fBitdepth );
  264. header->fNMidiPckt = ntohl ( header->fNMidiPckt );
  265. header->fPacketSize = ntohl ( header->fPacketSize );
  266. header->fCycle = ntohl ( header->fCycle );
  267. header->fSubCycle = ntohl ( header->fSubCycle );
  268. }
  269. EXPORT void PacketHeaderDisplay ( packet_header_t* header )
  270. {
  271. char bitdepth[16];
  272. ( header->fBitdepth ) ? sprintf ( bitdepth, "%u", header->fBitdepth ) : sprintf ( bitdepth, "%s", "float" );
  273. jack_info ( "********************Header********************" );
  274. jack_info ( "Data type : %c", header->fDataType );
  275. jack_info ( "Data stream : %c", header->fDataStream );
  276. jack_info ( "ID : %u", header->fID );
  277. jack_info ( "Cycle : %u", header->fCycle );
  278. jack_info ( "SubCycle : %u", header->fSubCycle );
  279. jack_info ( "Midi packets : %u", header->fNMidiPckt );
  280. jack_info ( "Midi data size : %u", header->fMidiDataSize );
  281. jack_info ( "Last packet : '%c'", header->fIsLastPckt );
  282. jack_info ( "Bitdepth : %s", bitdepth );
  283. jack_info ( "**********************************************" );
  284. }
  285. // Utility *******************************************************************************************************
  286. EXPORT int SocketAPIInit()
  287. {
  288. #ifdef WIN32
  289. WORD wVersionRequested = MAKEWORD ( 2, 2 );
  290. WSADATA wsaData;
  291. if ( WSAStartup ( wVersionRequested, &wsaData ) != 0 )
  292. {
  293. jack_error ( "WSAStartup error : %s", strerror ( NET_ERROR_CODE ) );
  294. return -1;
  295. }
  296. if ( LOBYTE ( wsaData.wVersion ) != 2 || HIBYTE ( wsaData.wVersion ) != 2 )
  297. {
  298. jack_error ( "Could not find a useable version of Winsock.dll\n" );
  299. WSACleanup();
  300. return -1;
  301. }
  302. #endif
  303. return 0;
  304. }
  305. EXPORT int SocketAPIEnd()
  306. {
  307. #ifdef WIN32
  308. return WSACleanup();
  309. #endif
  310. return 0;
  311. }
  312. EXPORT jack_nframes_t SetFramesPerPacket ( session_params_t* params )
  313. {
  314. if ( !params->fSendAudioChannels && !params->fReturnAudioChannels )
  315. return ( params->fFramesPerPacket = params->fPeriodSize );
  316. jack_nframes_t period = ( int ) powf ( 2.f, ( int ) ( log ( ( params->fMtu - sizeof ( packet_header_t ) )
  317. / ( max ( params->fReturnAudioChannels, params->fSendAudioChannels ) * sizeof ( sample_t ) ) ) / log ( 2 ) ) );
  318. ( period > params->fPeriodSize ) ? params->fFramesPerPacket = params->fPeriodSize : params->fFramesPerPacket = period;
  319. return params->fFramesPerPacket;
  320. }
  321. EXPORT int GetNetBufferSize ( session_params_t* params )
  322. {
  323. //audio
  324. float audio_size = params->fMtu * ( params->fPeriodSize / params->fFramesPerPacket );
  325. //midi
  326. float midi_size = params->fMtu * ( max ( params->fSendMidiChannels, params->fReturnMidiChannels ) *
  327. params->fPeriodSize * sizeof ( sample_t ) / ( params->fMtu - sizeof ( packet_header_t ) ) );
  328. //return : sizes of sync + audio + midi
  329. return ( params->fMtu + ( int ) audio_size + ( int ) midi_size );
  330. }
  331. EXPORT int GetNMidiPckt ( session_params_t* params, size_t data_size )
  332. {
  333. //even if there is no midi data, jack need an empty buffer to know there is no event to read
  334. //99% of the cases : all data in one packet
  335. if ( data_size <= ( params->fMtu - sizeof ( packet_header_t ) ) )
  336. return 1;
  337. //else, get the number of needed packets (simply slice the biiig buffer)
  338. int npckt = data_size / ( params->fMtu - sizeof ( packet_header_t ) );
  339. if ( data_size % ( params->fMtu - sizeof ( packet_header_t ) ) )
  340. return ++npckt;
  341. return npckt;
  342. }
  343. EXPORT int SetRxTimeout ( JackNetSocket* socket, session_params_t* params )
  344. {
  345. float time;
  346. //fast mode, wait for the entire cycle duration
  347. if ( params->fNetworkMode == 'f' )
  348. time = 900000.f * ( static_cast<float> ( params->fPeriodSize ) / static_cast<float> ( params->fSampleRate ) );
  349. //normal or slow mode, short timeout on recv
  350. else
  351. time = 4000000.f * ( static_cast<float> ( params->fFramesPerPacket ) / static_cast<float> ( params->fSampleRate ) );
  352. return socket->SetTimeOut ( static_cast<int> ( time ) );
  353. }
  354. // Packet *******************************************************************************************************
  355. EXPORT bool IsNextPacket ( packet_header_t* previous, packet_header_t* next, uint subcycles )
  356. {
  357. //ignore first cycle
  358. if ( previous->fCycle <= 1 )
  359. return true;
  360. //same PcktID (cycle), next SubPcktID (subcycle)
  361. if ( ( previous->fSubCycle < ( subcycles - 1 ) ) && ( next->fCycle == previous->fCycle ) && ( next->fSubCycle == ( previous->fSubCycle + 1 ) ) )
  362. return true;
  363. //next PcktID (cycle), SubPcktID reset to 1 (first subcyle)
  364. if ( ( next->fCycle == ( previous->fCycle + 1 ) ) && ( previous->fSubCycle == ( subcycles - 1 ) ) && ( next->fSubCycle == 0 ) )
  365. return true;
  366. //else, next is'nt next, return false
  367. return false;
  368. }
  369. }