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.

365 lines
14KB

  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->fSendAudioChannels = htonl ( params->fSendAudioChannels );
  138. params->fReturnAudioChannels = htonl ( params->fReturnAudioChannels );
  139. params->fSendMidiChannels = htonl ( params->fSendMidiChannels );
  140. params->fReturnMidiChannels = htonl ( params->fReturnMidiChannels );
  141. params->fSampleRate = htonl ( params->fSampleRate );
  142. params->fPeriodSize = htonl ( params->fPeriodSize );
  143. params->fFramesPerPacket = htonl ( params->fFramesPerPacket );
  144. params->fBitdepth = htonl ( params->fBitdepth );
  145. }
  146. EXPORT void SessionParamsNToH ( session_params_t* params )
  147. {
  148. params->fPacketID = ntohl ( params->fPacketID );
  149. params->fMtu = ntohl ( params->fMtu );
  150. params->fID = ntohl ( params->fID );
  151. params->fSendAudioChannels = ntohl ( params->fSendAudioChannels );
  152. params->fReturnAudioChannels = ntohl ( params->fReturnAudioChannels );
  153. params->fSendMidiChannels = ntohl ( params->fSendMidiChannels );
  154. params->fReturnMidiChannels = ntohl ( params->fReturnMidiChannels );
  155. params->fSampleRate = ntohl ( params->fSampleRate );
  156. params->fPeriodSize = ntohl ( params->fPeriodSize );
  157. params->fFramesPerPacket = ntohl ( params->fFramesPerPacket );
  158. params->fBitdepth = ntohl ( params->fBitdepth );
  159. }
  160. EXPORT void SessionParamsDisplay ( session_params_t* params )
  161. {
  162. jack_info ( "---->Network parameters of '%s'<----", params->fName );
  163. jack_info ( "Protocol revision : %c", params->fProtocolVersion );
  164. jack_info ( "MTU : %u", params->fMtu );
  165. jack_info ( "Master name : %s", params->fMasterNetName );
  166. jack_info ( "Slave name : %s", params->fSlaveNetName );
  167. jack_info ( "ID : %u", params->fID );
  168. jack_info ( "Send channels (audio - midi) : %d - %d", params->fSendAudioChannels, params->fSendMidiChannels );
  169. jack_info ( "Return channels (audio - midi) : %d - %d", params->fReturnAudioChannels, params->fReturnMidiChannels );
  170. jack_info ( "Sample rate : %u frames per second", params->fSampleRate );
  171. jack_info ( "Period size : %u frames per period", params->fPeriodSize );
  172. jack_info ( "Frames per packet : %u", params->fFramesPerPacket );
  173. jack_info ( "Packet per period : %u", params->fPeriodSize / params->fFramesPerPacket );
  174. jack_info ( "Bitdepth (0 for float) : %u", params->fBitdepth );
  175. jack_info ( "Name : %s", params->fName );
  176. jack_info ( "---------------------------------------------" );
  177. }
  178. EXPORT sync_packet_type_t GetPacketType ( session_params_t* params )
  179. {
  180. switch ( params->fPacketID )
  181. {
  182. case 0:
  183. return SLAVE_AVAILABLE;
  184. case 1:
  185. return SLAVE_SETUP;
  186. case 2:
  187. return START_MASTER;
  188. case 3:
  189. return START_SLAVE;
  190. case 4:
  191. return KILL_MASTER;
  192. }
  193. return INVALID;
  194. }
  195. EXPORT int SetPacketType ( session_params_t* params, sync_packet_type_t packet_type )
  196. {
  197. switch ( packet_type )
  198. {
  199. case INVALID:
  200. return -1;
  201. case SLAVE_AVAILABLE:
  202. params->fPacketID = 0;
  203. break;
  204. case SLAVE_SETUP:
  205. params->fPacketID = 1;
  206. break;
  207. case START_MASTER:
  208. params->fPacketID = 2;
  209. break;
  210. case START_SLAVE:
  211. params->fPacketID = 3;
  212. break;
  213. case KILL_MASTER:
  214. params->fPacketID = 4;
  215. }
  216. return 0;
  217. }
  218. // Packet header **********************************************************************************
  219. EXPORT void PacketHeaderHToN ( packet_header_t* header )
  220. {
  221. header->fID = htonl ( header->fID );
  222. header->fMidiDataSize = htonl ( header->fMidiDataSize );
  223. header->fBitdepth = htonl ( header->fBitdepth );
  224. header->fNMidiPckt = htonl ( header->fNMidiPckt );
  225. header->fCycle = ntohl ( header->fCycle );
  226. header->fSubCycle = htonl ( header->fSubCycle );
  227. }
  228. EXPORT void PacketHeaderNToH ( packet_header_t* header )
  229. {
  230. header->fID = ntohl ( header->fID );
  231. header->fMidiDataSize = ntohl ( header->fMidiDataSize );
  232. header->fBitdepth = ntohl ( header->fBitdepth );
  233. header->fNMidiPckt = ntohl ( header->fNMidiPckt );
  234. header->fCycle = ntohl ( header->fCycle );
  235. header->fSubCycle = ntohl ( header->fSubCycle );
  236. }
  237. EXPORT void PacketHeaderDisplay ( packet_header_t* header )
  238. {
  239. jack_info ( "********************Header********************" );
  240. jack_info ( "Data type : %c", header->fDataType );
  241. jack_info ( "Data stream : %c", header->fDataStream );
  242. jack_info ( "ID : %u", header->fID );
  243. jack_info ( "Cycle : %u", header->fCycle );
  244. jack_info ( "SubCycle : %u", header->fSubCycle );
  245. jack_info ( "Midi packets : %u", header->fNMidiPckt );
  246. jack_info ( "Midi data size : %u", header->fMidiDataSize );
  247. jack_info ( "Last packet : '%c'", header->fIsLastPckt );
  248. jack_info ( "Bitdepth : %u (0 for float)", header->fBitdepth );
  249. jack_info ( "**********************************************" );
  250. }
  251. // Utility *******************************************************************************************************
  252. EXPORT int SocketAPIInit()
  253. {
  254. #ifdef WIN32
  255. WORD wVersionRequested = MAKEWORD ( 2, 2 );
  256. WSADATA wsaData;
  257. if ( WSAStartup(wVersionRequested, &wsaData) != 0 )
  258. {
  259. jack_error ( "WSAStartup error : %s", strerror ( NET_ERROR_CODE ) );
  260. return -1;
  261. }
  262. if ( LOBYTE ( wsaData.wVersion ) != 2 || HIBYTE ( wsaData.wVersion ) != 2 )
  263. {
  264. jack_error ( "Could not find a useable version of Winsock.dll\n" );
  265. WSACleanup();
  266. return -1;
  267. }
  268. #endif
  269. return 0;
  270. }
  271. EXPORT int SocketAPIEnd()
  272. {
  273. #ifdef WIN32
  274. return WSACleanup();
  275. #endif
  276. return 0;
  277. }
  278. EXPORT jack_nframes_t SetFramesPerPacket ( session_params_t* params )
  279. {
  280. if ( !params->fSendAudioChannels && !params->fReturnAudioChannels )
  281. return ( params->fFramesPerPacket = params->fPeriodSize );
  282. size_t period = ( int ) powf ( 2.f, ( int ) ( log ( ( params->fMtu - sizeof ( packet_header_t ) )
  283. / ( max ( params->fReturnAudioChannels, params->fSendAudioChannels ) * sizeof ( sample_t ) ) ) / log ( 2 ) ) );
  284. ( period > params->fPeriodSize ) ? params->fFramesPerPacket = params->fPeriodSize : params->fFramesPerPacket = period;
  285. return params->fFramesPerPacket;
  286. }
  287. EXPORT int GetNMidiPckt ( session_params_t* params, size_t data_size )
  288. {
  289. //even if there is no midi data, jack need an empty buffer to know there is no event to read
  290. //99% of the cases : all data in one packet
  291. if ( data_size <= ( params->fMtu - sizeof ( packet_header_t ) ) )
  292. return 1;
  293. //else, get the number of needed packets (simply slice the biiig buffer)
  294. int npckt = data_size / ( params->fMtu - sizeof ( packet_header_t ) );
  295. if ( data_size % ( params->fMtu - sizeof ( packet_header_t ) ) )
  296. return ++npckt;
  297. return npckt;
  298. }
  299. EXPORT int SetRxTimeout ( JackNetSocket* socket, session_params_t* params )
  300. {
  301. //time in ms : 1,25 * 'one packet time'
  302. float time = 1250 * ( static_cast<float> ( params->fFramesPerPacket ) / static_cast<float> ( params->fSampleRate ) );
  303. int ms = ( int ) time;
  304. return ( socket->SetTimeOut ( ms ) == SOCKET_ERROR ) ? SOCKET_ERROR : ms;
  305. }
  306. // Packet *******************************************************************************************************
  307. EXPORT bool IsNextPacket ( packet_header_t* previous, packet_header_t* next, uint subcycles )
  308. {
  309. //ignore first cycle
  310. if ( previous->fCycle <= 1 )
  311. return true;
  312. //same PcktID (cycle), next SubPcktID (subcycle)
  313. if ( ( previous->fSubCycle < ( subcycles - 1 ) ) && ( next->fCycle == previous->fCycle ) && ( next->fSubCycle == ( previous->fSubCycle + 1 ) ) )
  314. return true;
  315. //next PcktID (cycle), SubPcktID reset to 1 (first subcyle)
  316. if ( ( next->fCycle == ( previous->fCycle + 1 ) ) && ( previous->fSubCycle == ( subcycles - 1 ) ) && ( next->fSubCycle == 0 ) )
  317. return true;
  318. //else, next is'nt next, return false
  319. return false;
  320. }
  321. }