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.

355 lines
12KB

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