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.

331 lines
12KB

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