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.

390 lines
15KB

  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. jack_info ( "**************** Network parameters ****************" );
  177. jack_info ( "Name : %s", params->fName );
  178. jack_info ( "Protocol revision : %c", params->fProtocolVersion );
  179. jack_info ( "MTU : %u", params->fMtu );
  180. jack_info ( "Master name : %s", params->fMasterNetName );
  181. jack_info ( "Slave name : %s", params->fSlaveNetName );
  182. jack_info ( "ID : %u", params->fID );
  183. jack_info ( "Transport Sync : %s", ( params->fTransportSync ) ? "yes" : "no" );
  184. jack_info ( "Send channels (audio - midi) : %d - %d", params->fSendAudioChannels, params->fSendMidiChannels );
  185. jack_info ( "Return channels (audio - midi) : %d - %d", params->fReturnAudioChannels, params->fReturnMidiChannels );
  186. jack_info ( "Sample rate : %u frames per second", params->fSampleRate );
  187. jack_info ( "Period size : %u frames per period", params->fPeriodSize );
  188. jack_info ( "Frames per packet : %u", params->fFramesPerPacket );
  189. jack_info ( "Packet per period : %u", params->fPeriodSize / params->fFramesPerPacket );
  190. jack_info ( "Bitdepth : %s", bitdepth );
  191. jack_info ( "Slave mode : %s", ( params->fSlaveSyncMode ) ? "sync" : "async" );
  192. jack_info ( "****************************************************" );
  193. }
  194. EXPORT sync_packet_type_t GetPacketType ( session_params_t* params )
  195. {
  196. switch ( params->fPacketID )
  197. {
  198. case 0:
  199. return SLAVE_AVAILABLE;
  200. case 1:
  201. return SLAVE_SETUP;
  202. case 2:
  203. return START_MASTER;
  204. case 3:
  205. return START_SLAVE;
  206. case 4:
  207. return KILL_MASTER;
  208. }
  209. return INVALID;
  210. }
  211. EXPORT int SetPacketType ( session_params_t* params, sync_packet_type_t packet_type )
  212. {
  213. switch ( packet_type )
  214. {
  215. case INVALID:
  216. return -1;
  217. case SLAVE_AVAILABLE:
  218. params->fPacketID = 0;
  219. break;
  220. case SLAVE_SETUP:
  221. params->fPacketID = 1;
  222. break;
  223. case START_MASTER:
  224. params->fPacketID = 2;
  225. break;
  226. case START_SLAVE:
  227. params->fPacketID = 3;
  228. break;
  229. case KILL_MASTER:
  230. params->fPacketID = 4;
  231. }
  232. return 0;
  233. }
  234. // Packet header **********************************************************************************
  235. EXPORT void PacketHeaderHToN ( packet_header_t* header )
  236. {
  237. header->fID = htonl ( header->fID );
  238. header->fMidiDataSize = htonl ( header->fMidiDataSize );
  239. header->fBitdepth = htonl ( header->fBitdepth );
  240. header->fNMidiPckt = htonl ( header->fNMidiPckt );
  241. header->fCycle = ntohl ( header->fCycle );
  242. header->fSubCycle = htonl ( header->fSubCycle );
  243. }
  244. EXPORT void PacketHeaderNToH ( packet_header_t* header )
  245. {
  246. header->fID = ntohl ( header->fID );
  247. header->fMidiDataSize = ntohl ( header->fMidiDataSize );
  248. header->fBitdepth = ntohl ( header->fBitdepth );
  249. header->fNMidiPckt = ntohl ( header->fNMidiPckt );
  250. header->fCycle = ntohl ( header->fCycle );
  251. header->fSubCycle = ntohl ( header->fSubCycle );
  252. }
  253. EXPORT void PacketHeaderDisplay ( packet_header_t* header )
  254. {
  255. char bitdepth[16];
  256. ( header->fBitdepth ) ? sprintf ( bitdepth, "%u", header->fBitdepth ) : sprintf ( bitdepth, "%s", "float" );
  257. jack_info ( "********************Header********************" );
  258. jack_info ( "Data type : %c", header->fDataType );
  259. jack_info ( "Data stream : %c", header->fDataStream );
  260. jack_info ( "ID : %u", header->fID );
  261. jack_info ( "Cycle : %u", header->fCycle );
  262. jack_info ( "SubCycle : %u", header->fSubCycle );
  263. jack_info ( "Midi packets : %u", header->fNMidiPckt );
  264. jack_info ( "Midi data size : %u", header->fMidiDataSize );
  265. jack_info ( "Last packet : '%c'", header->fIsLastPckt );
  266. jack_info ( "Bitdepth : %s", bitdepth );
  267. jack_info ( "**********************************************" );
  268. }
  269. // Utility *******************************************************************************************************
  270. EXPORT int SocketAPIInit()
  271. {
  272. #ifdef WIN32
  273. WORD wVersionRequested = MAKEWORD ( 2, 2 );
  274. WSADATA wsaData;
  275. if ( WSAStartup ( wVersionRequested, &wsaData ) != 0 )
  276. {
  277. jack_error ( "WSAStartup error : %s", strerror ( NET_ERROR_CODE ) );
  278. return -1;
  279. }
  280. if ( LOBYTE ( wsaData.wVersion ) != 2 || HIBYTE ( wsaData.wVersion ) != 2 )
  281. {
  282. jack_error ( "Could not find a useable version of Winsock.dll\n" );
  283. WSACleanup();
  284. return -1;
  285. }
  286. #endif
  287. return 0;
  288. }
  289. EXPORT int SocketAPIEnd()
  290. {
  291. #ifdef WIN32
  292. return WSACleanup();
  293. #endif
  294. return 0;
  295. }
  296. EXPORT jack_nframes_t SetFramesPerPacket ( session_params_t* params )
  297. {
  298. if ( !params->fSendAudioChannels && !params->fReturnAudioChannels )
  299. return ( params->fFramesPerPacket = params->fPeriodSize );
  300. size_t period = ( int ) powf ( 2.f, ( int ) ( log ( ( params->fMtu - sizeof ( packet_header_t ) )
  301. / ( max ( params->fReturnAudioChannels, params->fSendAudioChannels ) * sizeof ( sample_t ) ) ) / log ( 2 ) ) );
  302. ( period > params->fPeriodSize ) ? params->fFramesPerPacket = params->fPeriodSize : params->fFramesPerPacket = period;
  303. return params->fFramesPerPacket;
  304. }
  305. EXPORT int GetNMidiPckt ( session_params_t* params, size_t data_size )
  306. {
  307. //even if there is no midi data, jack need an empty buffer to know there is no event to read
  308. //99% of the cases : all data in one packet
  309. if ( data_size <= ( params->fMtu - sizeof ( packet_header_t ) ) )
  310. return 1;
  311. //else, get the number of needed packets (simply slice the biiig buffer)
  312. int npckt = data_size / ( params->fMtu - sizeof ( packet_header_t ) );
  313. if ( data_size % ( params->fMtu - sizeof ( packet_header_t ) ) )
  314. return ++npckt;
  315. return npckt;
  316. }
  317. EXPORT int SetRxTimeout ( JackNetSocket* socket, session_params_t* params )
  318. {
  319. float time;
  320. //fast mode, wait for the entire cycle duration
  321. if ( params->fNetworkMasterMode == 'f' )
  322. time = 900000.f * ( static_cast<float> ( params->fPeriodSize ) / static_cast<float> ( params->fSampleRate ) );
  323. //slow mode, just try recv during a subcycle audio packet
  324. else
  325. time = 1250000.f * ( static_cast<float> ( params->fFramesPerPacket ) / static_cast<float> ( params->fSampleRate ) );
  326. int usec = ( int ) time;
  327. return socket->SetTimeOut ( usec );
  328. }
  329. // Packet *******************************************************************************************************
  330. EXPORT bool IsNextPacket ( packet_header_t* previous, packet_header_t* next, uint subcycles )
  331. {
  332. //ignore first cycle
  333. if ( previous->fCycle <= 1 )
  334. return true;
  335. //same PcktID (cycle), next SubPcktID (subcycle)
  336. if ( ( previous->fSubCycle < ( subcycles - 1 ) ) && ( next->fCycle == previous->fCycle ) && ( next->fSubCycle == ( previous->fSubCycle + 1 ) ) )
  337. return true;
  338. //next PcktID (cycle), SubPcktID reset to 1 (first subcyle)
  339. if ( ( next->fCycle == ( previous->fCycle + 1 ) ) && ( previous->fSubCycle == ( subcycles - 1 ) ) && ( next->fSubCycle == 0 ) )
  340. return true;
  341. //else, next is'nt next, return false
  342. return false;
  343. }
  344. }