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.

372 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. void NetMidiBuffer::DisplayEvents()
  45. {
  46. for ( int port_index = 0; port_index < fNPorts; port_index++ )
  47. {
  48. for ( uint event = 0; event < fPortBuffer[port_index]->event_count; event++ )
  49. if ( fPortBuffer[port_index]->IsValid() )
  50. jack_info ( "port %d : midi event %u/%u -> time : %u, size : %u",
  51. port_index + 1, event + 1, fPortBuffer[port_index]->event_count,
  52. fPortBuffer[port_index]->events[event].time, fPortBuffer[port_index]->events[event].size );
  53. }
  54. }
  55. int NetMidiBuffer::RenderFromJackPorts()
  56. {
  57. int pos = 0;
  58. size_t copy_size;
  59. for ( int port_index = 0; port_index < fNPorts; port_index++ )
  60. {
  61. copy_size = sizeof ( JackMidiBuffer ) + fPortBuffer[port_index]->event_count * sizeof ( JackMidiEvent );
  62. memcpy ( fBuffer + pos, fPortBuffer[port_index], copy_size );
  63. pos += copy_size;
  64. memcpy ( fBuffer + pos, fPortBuffer[port_index] + ( fPortBuffer[port_index]->buffer_size - fPortBuffer[port_index]->write_pos ),
  65. fPortBuffer[port_index]->write_pos );
  66. pos += fPortBuffer[port_index]->write_pos;
  67. }
  68. return pos;
  69. }
  70. int NetMidiBuffer::RenderToJackPorts()
  71. {
  72. int pos = 0;
  73. int copy_size;
  74. for ( int port_index = 0; port_index < fNPorts; port_index++ )
  75. {
  76. copy_size = sizeof ( JackMidiBuffer ) + reinterpret_cast<JackMidiBuffer*> ( fBuffer + pos )->event_count * sizeof ( JackMidiEvent );
  77. memcpy ( fPortBuffer[port_index], fBuffer + pos, copy_size );
  78. pos += copy_size;
  79. memcpy ( fPortBuffer[port_index] + ( fPortBuffer[port_index]->buffer_size - fPortBuffer[port_index]->write_pos ),
  80. fBuffer + pos, fPortBuffer[port_index]->write_pos );
  81. pos += fPortBuffer[port_index]->write_pos;
  82. }
  83. return pos;
  84. }
  85. int NetMidiBuffer::RenderFromNetwork ( int subcycle, size_t copy_size )
  86. {
  87. memcpy ( fBuffer + subcycle * fMaxPcktSize, fNetBuffer, copy_size );
  88. return copy_size;
  89. }
  90. int NetMidiBuffer::RenderToNetwork ( int subcycle, size_t total_size )
  91. {
  92. int size = total_size - subcycle * fMaxPcktSize;
  93. int copy_size = ( size <= fMaxPcktSize ) ? size : fMaxPcktSize;
  94. memcpy ( fNetBuffer, fBuffer + subcycle * fMaxPcktSize, copy_size );
  95. return copy_size;
  96. }
  97. // net audio buffer *********************************************************************************
  98. NetAudioBuffer::NetAudioBuffer ( session_params_t* params, uint32_t nports, char* net_buffer )
  99. {
  100. fNPorts = nports;
  101. fPeriodSize = params->fPeriodSize;
  102. fSubPeriodSize = params->fFramesPerPacket;
  103. fSubPeriodBytesSize = fSubPeriodSize * sizeof ( sample_t );
  104. fPortBuffer = new sample_t* [fNPorts];
  105. for ( int port_index = 0; port_index < fNPorts; port_index++ )
  106. fPortBuffer[port_index] = NULL;
  107. fNetBuffer = net_buffer;
  108. }
  109. NetAudioBuffer::~NetAudioBuffer()
  110. {
  111. delete[] fPortBuffer;
  112. }
  113. size_t NetAudioBuffer::GetSize()
  114. {
  115. return fNPorts * fSubPeriodBytesSize;
  116. }
  117. void NetAudioBuffer::SetBuffer(int index, sample_t* buffer)
  118. {
  119. fPortBuffer[index] = buffer;
  120. }
  121. void NetAudioBuffer::RenderFromJackPorts ( int subcycle )
  122. {
  123. for ( int port_index = 0; port_index < fNPorts; port_index++ )
  124. memcpy ( fNetBuffer + port_index * fSubPeriodBytesSize, fPortBuffer[port_index] + subcycle * fSubPeriodSize, fSubPeriodBytesSize );
  125. }
  126. void NetAudioBuffer::RenderToJackPorts ( int subcycle )
  127. {
  128. for ( int port_index = 0; port_index < fNPorts; port_index++ )
  129. memcpy ( fPortBuffer[port_index] + subcycle * fSubPeriodSize, fNetBuffer + port_index * fSubPeriodBytesSize, fSubPeriodBytesSize );
  130. }
  131. // SessionParams ************************************************************************************
  132. EXPORT void SessionParamsHToN ( session_params_t* params )
  133. {
  134. params->fPacketID = htonl ( params->fPacketID );
  135. params->fMtu = htonl ( params->fMtu );
  136. params->fID = htonl ( params->fID );
  137. params->fTransportSync = htonl ( params->fTransportSync );
  138. params->fSendAudioChannels = htonl ( params->fSendAudioChannels );
  139. params->fReturnAudioChannels = htonl ( params->fReturnAudioChannels );
  140. params->fSendMidiChannels = htonl ( params->fSendMidiChannels );
  141. params->fReturnMidiChannels = htonl ( params->fReturnMidiChannels );
  142. params->fSampleRate = htonl ( params->fSampleRate );
  143. params->fPeriodSize = htonl ( params->fPeriodSize );
  144. params->fFramesPerPacket = htonl ( params->fFramesPerPacket );
  145. params->fBitdepth = htonl ( params->fBitdepth );
  146. }
  147. EXPORT void SessionParamsNToH ( session_params_t* params )
  148. {
  149. params->fPacketID = ntohl ( params->fPacketID );
  150. params->fMtu = ntohl ( params->fMtu );
  151. params->fID = ntohl ( params->fID );
  152. params->fTransportSync = ntohl ( params->fTransportSync );
  153. params->fSendAudioChannels = ntohl ( params->fSendAudioChannels );
  154. params->fReturnAudioChannels = ntohl ( params->fReturnAudioChannels );
  155. params->fSendMidiChannels = ntohl ( params->fSendMidiChannels );
  156. params->fReturnMidiChannels = ntohl ( params->fReturnMidiChannels );
  157. params->fSampleRate = ntohl ( params->fSampleRate );
  158. params->fPeriodSize = ntohl ( params->fPeriodSize );
  159. params->fFramesPerPacket = ntohl ( params->fFramesPerPacket );
  160. params->fBitdepth = ntohl ( params->fBitdepth );
  161. }
  162. EXPORT void SessionParamsDisplay ( session_params_t* params )
  163. {
  164. char bitdepth[16];
  165. ( params->fBitdepth ) ? sprintf ( bitdepth, "%u", params->fBitdepth ) : sprintf ( bitdepth, "%s", "float" );
  166. jack_info ( "**************** Network parameters ****************" );
  167. jack_info ( "Name : %s", params->fName );
  168. jack_info ( "Protocol revision : %c", params->fProtocolVersion );
  169. jack_info ( "MTU : %u", params->fMtu );
  170. jack_info ( "Master name : %s", params->fMasterNetName );
  171. jack_info ( "Slave name : %s", params->fSlaveNetName );
  172. jack_info ( "ID : %u", params->fID );
  173. jack_info ( "Transport Sync : %s", (params->fTransportSync) ? "yes" : "no" );
  174. jack_info ( "Send channels (audio - midi) : %d - %d", params->fSendAudioChannels, params->fSendMidiChannels );
  175. jack_info ( "Return channels (audio - midi) : %d - %d", params->fReturnAudioChannels, params->fReturnMidiChannels );
  176. jack_info ( "Sample rate : %u frames per second", params->fSampleRate );
  177. jack_info ( "Period size : %u frames per period", params->fPeriodSize );
  178. jack_info ( "Frames per packet : %u", params->fFramesPerPacket );
  179. jack_info ( "Packet per period : %u", params->fPeriodSize / params->fFramesPerPacket );
  180. jack_info ( "Bitdepth : %s", bitdepth );
  181. jack_info ( "****************************************************" );
  182. }
  183. EXPORT sync_packet_type_t GetPacketType ( session_params_t* params )
  184. {
  185. switch ( params->fPacketID )
  186. {
  187. case 0:
  188. return SLAVE_AVAILABLE;
  189. case 1:
  190. return SLAVE_SETUP;
  191. case 2:
  192. return START_MASTER;
  193. case 3:
  194. return START_SLAVE;
  195. case 4:
  196. return KILL_MASTER;
  197. }
  198. return INVALID;
  199. }
  200. EXPORT int SetPacketType ( session_params_t* params, sync_packet_type_t packet_type )
  201. {
  202. switch ( packet_type )
  203. {
  204. case INVALID:
  205. return -1;
  206. case SLAVE_AVAILABLE:
  207. params->fPacketID = 0;
  208. break;
  209. case SLAVE_SETUP:
  210. params->fPacketID = 1;
  211. break;
  212. case START_MASTER:
  213. params->fPacketID = 2;
  214. break;
  215. case START_SLAVE:
  216. params->fPacketID = 3;
  217. break;
  218. case KILL_MASTER:
  219. params->fPacketID = 4;
  220. }
  221. return 0;
  222. }
  223. // Packet header **********************************************************************************
  224. EXPORT void PacketHeaderHToN ( packet_header_t* header )
  225. {
  226. header->fID = htonl ( header->fID );
  227. header->fMidiDataSize = htonl ( header->fMidiDataSize );
  228. header->fBitdepth = htonl ( header->fBitdepth );
  229. header->fNMidiPckt = htonl ( header->fNMidiPckt );
  230. header->fCycle = ntohl ( header->fCycle );
  231. header->fSubCycle = htonl ( header->fSubCycle );
  232. }
  233. EXPORT void PacketHeaderNToH ( packet_header_t* header )
  234. {
  235. header->fID = ntohl ( header->fID );
  236. header->fMidiDataSize = ntohl ( header->fMidiDataSize );
  237. header->fBitdepth = ntohl ( header->fBitdepth );
  238. header->fNMidiPckt = ntohl ( header->fNMidiPckt );
  239. header->fCycle = ntohl ( header->fCycle );
  240. header->fSubCycle = ntohl ( header->fSubCycle );
  241. }
  242. EXPORT void PacketHeaderDisplay ( packet_header_t* header )
  243. {
  244. char bitdepth[16];
  245. ( header->fBitdepth ) ? sprintf ( bitdepth, "%u", header->fBitdepth ) : sprintf ( bitdepth, "%s", "float" );
  246. jack_info ( "********************Header********************" );
  247. jack_info ( "Data type : %c", header->fDataType );
  248. jack_info ( "Data stream : %c", header->fDataStream );
  249. jack_info ( "ID : %u", header->fID );
  250. jack_info ( "Cycle : %u", header->fCycle );
  251. jack_info ( "SubCycle : %u", header->fSubCycle );
  252. jack_info ( "Midi packets : %u", header->fNMidiPckt );
  253. jack_info ( "Midi data size : %u", header->fMidiDataSize );
  254. jack_info ( "Last packet : '%c'", header->fIsLastPckt );
  255. jack_info ( "Bitdepth : %s", bitdepth );
  256. jack_info ( "**********************************************" );
  257. }
  258. // Utility *******************************************************************************************************
  259. EXPORT int SocketAPIInit()
  260. {
  261. #ifdef WIN32
  262. WORD wVersionRequested = MAKEWORD ( 2, 2 );
  263. WSADATA wsaData;
  264. if ( WSAStartup(wVersionRequested, &wsaData) != 0 )
  265. {
  266. jack_error ( "WSAStartup error : %s", strerror ( NET_ERROR_CODE ) );
  267. return -1;
  268. }
  269. if ( LOBYTE ( wsaData.wVersion ) != 2 || HIBYTE ( wsaData.wVersion ) != 2 )
  270. {
  271. jack_error ( "Could not find a useable version of Winsock.dll\n" );
  272. WSACleanup();
  273. return -1;
  274. }
  275. #endif
  276. return 0;
  277. }
  278. EXPORT int SocketAPIEnd()
  279. {
  280. #ifdef WIN32
  281. return WSACleanup();
  282. #endif
  283. return 0;
  284. }
  285. EXPORT jack_nframes_t SetFramesPerPacket ( session_params_t* params )
  286. {
  287. if ( !params->fSendAudioChannels && !params->fReturnAudioChannels )
  288. return ( params->fFramesPerPacket = params->fPeriodSize );
  289. size_t period = ( int ) powf ( 2.f, ( int ) ( log ( ( params->fMtu - sizeof ( packet_header_t ) )
  290. / ( max ( params->fReturnAudioChannels, params->fSendAudioChannels ) * sizeof ( sample_t ) ) ) / log ( 2 ) ) );
  291. ( period > params->fPeriodSize ) ? params->fFramesPerPacket = params->fPeriodSize : params->fFramesPerPacket = period;
  292. return params->fFramesPerPacket;
  293. }
  294. EXPORT int GetNMidiPckt ( session_params_t* params, size_t data_size )
  295. {
  296. //even if there is no midi data, jack need an empty buffer to know there is no event to read
  297. //99% of the cases : all data in one packet
  298. if ( data_size <= ( params->fMtu - sizeof ( packet_header_t ) ) )
  299. return 1;
  300. //else, get the number of needed packets (simply slice the biiig buffer)
  301. int npckt = data_size / ( params->fMtu - sizeof ( packet_header_t ) );
  302. if ( data_size % ( params->fMtu - sizeof ( packet_header_t ) ) )
  303. return ++npckt;
  304. return npckt;
  305. }
  306. EXPORT int SetRxTimeout ( JackNetSocket* socket, session_params_t* params )
  307. {
  308. //time in ms : 1,25 * 'one packet time'
  309. float time = 1250 * ( static_cast<float> ( params->fFramesPerPacket ) / static_cast<float> ( params->fSampleRate ) );
  310. int ms = ( int ) time;
  311. return ( socket->SetTimeOut ( ms ) == SOCKET_ERROR ) ? SOCKET_ERROR : ms;
  312. }
  313. // Packet *******************************************************************************************************
  314. EXPORT bool IsNextPacket ( packet_header_t* previous, packet_header_t* next, uint subcycles )
  315. {
  316. //ignore first cycle
  317. if ( previous->fCycle <= 1 )
  318. return true;
  319. //same PcktID (cycle), next SubPcktID (subcycle)
  320. if ( ( previous->fSubCycle < ( subcycles - 1 ) ) && ( next->fCycle == previous->fCycle ) && ( next->fSubCycle == ( previous->fSubCycle + 1 ) ) )
  321. return true;
  322. //next PcktID (cycle), SubPcktID reset to 1 (first subcyle)
  323. if ( ( next->fCycle == ( previous->fCycle + 1 ) ) && ( previous->fSubCycle == ( subcycles - 1 ) ) && ( next->fSubCycle == 0 ) )
  324. return true;
  325. //else, next is'nt next, return false
  326. return false;
  327. }
  328. }