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.

178 lines
5.6KB

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