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.

175 lines
5.3KB

  1. #include "types.h"
  2. #include "JackConstants.h"
  3. #include "JackMidiPort.h"
  4. #include <string>
  5. #include <algorithm>
  6. #include <cmath>
  7. #include <cstdlib>
  8. #include <cstdio>
  9. #include <iostream>
  10. #include <unistd.h>
  11. #include <sys/types.h>
  12. #include <sys/socket.h>
  13. #include <netdb.h>
  14. #include <netinet/in.h>
  15. #include <arpa/inet.h>
  16. #include <errno.h>
  17. namespace Jack
  18. {
  19. typedef struct _session_params session_params_t;
  20. typedef struct _packet_header packet_header_t;
  21. typedef struct _midi_portbuf_desc midi_portbuf_desc_t;
  22. typedef struct sockaddr socket_address_t;
  23. typedef struct in_addr address_t;
  24. typedef jack_default_audio_sample_t sample_t;
  25. //session params ******************************************************************************
  26. struct _session_params
  27. {
  28. char fPacketType[7]; //packet type ('param')
  29. char fProtocolVersion; //version
  30. int fPacketID; //indicates the packet type
  31. char fMasterNetName[256]; //master hostname (network)
  32. char fSlaveNetName[256]; //slave hostname (network)
  33. size_t fMtu; //connection mtu
  34. size_t fID; //slave's ID
  35. int fSendAudioChannels; //number of master->slave channels
  36. int fReturnAudioChannels; //number of slave->master channels
  37. int fSendMidiChannels; //number of master->slave midi channels
  38. int fReturnMidiChannels; //number of slave->master midi channels
  39. size_t fSampleRate; //session sample rate
  40. size_t fPeriodSize; //period size
  41. size_t fFramesPerPacket; //complete frames per packet
  42. size_t fBitdepth; //samples bitdepth (unused)
  43. char fName[JACK_CLIENT_NAME_SIZE]; //slave's name
  44. };
  45. //net status **********************************************************************************
  46. enum _net_status
  47. {
  48. SOCKET_ERROR,
  49. CONNECT_ERROR,
  50. RECV_ERROR,
  51. CONNECTED,
  52. ROLLING
  53. };
  54. typedef enum _net_status net_status_t;
  55. //sync packet type ****************************************************************************
  56. enum _sync_packet_type
  57. {
  58. INVALID, //...
  59. SLAVE_AVAILABLE, //a slave is available
  60. SLAVE_SETUP, //slave configuration
  61. START_MASTER, //slave is ready, start master
  62. START_SLAVE, //master is ready, activate slave
  63. KILL_MASTER //master must stop
  64. };
  65. typedef enum _sync_packet_type sync_packet_type_t;
  66. //packet header *******************************************************************************
  67. struct _packet_header
  68. {
  69. char fPacketType[7]; //packet type ( 'headr' )
  70. char fDataType; //a for audio, m for midi
  71. char fDataStream; //s for send, r for return
  72. size_t fID; //to identify the slave
  73. size_t fBitdepth; //bitdepth of the data samples
  74. size_t fMidiDataSize; //size of midi data (if packet is 'midi typed') in bytes
  75. size_t fNMidiPckt; //number of midi packets of the cycle
  76. size_t fCycle; //process cycle counter
  77. size_t fSubCycle; //midi/audio subcycle counter
  78. char fIsLastPckt; //is it the last packet of a given cycle ('y' or 'n')
  79. char fFree[13]; //unused
  80. };
  81. //midi data ***********************************************************************************
  82. class NetMidiBuffer
  83. {
  84. private:
  85. int fNPorts;
  86. size_t fMaxBufsize;
  87. int fMaxPcktSize;
  88. //data
  89. char* fBuffer;
  90. char* fNetBuffer;
  91. public:
  92. NetMidiBuffer ( session_params_t* params, size_t nports, char* net_buffer );
  93. ~NetMidiBuffer();
  94. JackMidiBuffer** fPortBuffer;
  95. void Reset();
  96. size_t GetSize();
  97. //utility
  98. void DisplayEvents();
  99. //jack<->buffer
  100. int RenderFromJackPorts();
  101. int RenderToJackPorts();
  102. //network<->buffer
  103. int RenderFromNetwork ( size_t subcycle, size_t copy_size );
  104. int RenderToNetwork ( size_t subcycle, size_t copy_size );
  105. };
  106. // audio data *********************************************************************************
  107. class NetAudioBuffer
  108. {
  109. private:
  110. int fNPorts;
  111. jack_nframes_t fPeriodSize;
  112. jack_nframes_t fSubPeriodSize;
  113. size_t fSubPeriodBytesSize;
  114. char* fNetBuffer;
  115. public:
  116. NetAudioBuffer ( session_params_t* params, size_t nports, char* net_buffer );
  117. ~NetAudioBuffer();
  118. sample_t** fPortBuffer;
  119. size_t GetSize();
  120. //jack<->buffer
  121. void RenderFromJackPorts ( size_t subcycle );
  122. void RenderToJackPorts ( size_t subcycle );
  123. };
  124. //utility *************************************************************************************
  125. //n<-->h functions
  126. void SessionParamsHToN ( session_params_t* params );
  127. void SessionParamsNToH ( session_params_t* params );
  128. void PacketHeaderHToN ( packet_header_t* header );
  129. void PacketHeaderNToH ( packet_header_t* header );
  130. //display session parameters
  131. void SessionParamsDisplay ( session_params_t* params );
  132. //display packet header
  133. void PacketHeaderDisplay ( packet_header_t* header );
  134. //get the packet type from a sesion parameters
  135. sync_packet_type_t GetPacketType ( session_params_t* params );
  136. //set the packet type in a session parameters
  137. int SetPacketType ( session_params_t* params, sync_packet_type_t packet_type );
  138. //step of network initialization
  139. size_t SetFramesPerPacket ( session_params_t* params );
  140. //get the midi packet number for a given cycle
  141. size_t GetNMidiPckt ( session_params_t* params, size_t data_size );
  142. //set the recv timeout on a socket
  143. int SetRxTimeout ( int* sockfd, session_params_t* params );
  144. //check if 'next' packet is really the next after 'previous'
  145. bool IsNextPacket ( packet_header_t* previous, packet_header_t* next, size_t subcycles );
  146. }