Assists music production by grouping standalone programs into sessions. Community version of "Non Session Manager".
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.

136 lines
3.8KB

  1. /*******************************************************************************/
  2. /* Copyright (C) 2008 Jonathan Moore Liles */
  3. /* */
  4. /* This program is free software; you can redistribute it and/or modify it */
  5. /* under the terms of the GNU General Public License as published by the */
  6. /* Free Software Foundation; either version 2 of the License, or (at your */
  7. /* option) any later version. */
  8. /* */
  9. /* This program is distributed in the hope that it will be useful, but WITHOUT */
  10. /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
  11. /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for */
  12. /* more details. */
  13. /* */
  14. /* You should have received a copy of the GNU General Public License along */
  15. /* with This program; see the file COPYING. If not,write to the Free Software */
  16. /* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  17. /*******************************************************************************/
  18. #include <netdb.h>
  19. #include <netinet/in.h>
  20. #include <unistd.h>
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <errno.h>
  25. #include <sys/socket.h>
  26. #include <sys/types.h>
  27. #include <sys/select.h>
  28. #include "Peak_Client.H"
  29. /* An interface to the peak server */
  30. #define PEAK_PORT 6111
  31. int
  32. connect_to_host ( const char *host, int port )
  33. {
  34. int s;
  35. struct sockaddr_in sa;
  36. struct hostent *hp;
  37. memset( &sa, 0, sizeof( sa ) );
  38. hp = gethostbyname( host );
  39. sa.sin_family = hp->h_addrtype;
  40. sa.sin_port = htons( port );
  41. sa.sin_addr.s_addr = INADDR_ANY;
  42. if ( ( s = socket( AF_INET, SOCK_STREAM, 0 ) ) < 0 )
  43. {
  44. perror( "socket()" );
  45. return -1;
  46. }
  47. if ( connect( s, (struct sockaddr *)&sa, sizeof( sa ) ) < 0 )
  48. {
  49. perror( "connect()" );
  50. close( s );
  51. return -1;
  52. }
  53. return s;
  54. }
  55. bool
  56. Peak_Client::connect ( const char *host )
  57. {
  58. if ( ( _socket = connect_to_host( host, PEAK_PORT ) ) < 0 )
  59. return false;
  60. return true;
  61. }
  62. bool
  63. Peak_Client::get_info ( const char *source, nframes_t *len, int *channels )
  64. {
  65. /* FIXME: needs to handle errors/reconnect */
  66. char buf[512];
  67. snprintf( buf, sizeof( buf ), "get_info \"%s\"\n", source );
  68. send( _socket, buf, strlen( buf ), 0 );
  69. recv( _socket, buf, sizeof( buf ), 0 );
  70. if ( sscanf( buf, "length=%lu channels=%d", len, channels ) != 2 )
  71. return false;
  72. return true;
  73. }
  74. bool
  75. Peak_Client::read_peaks ( const char *source, float fpp, nframes_t start, nframes_t end,
  76. int *peaks, Peak **pbuf, int *channels )
  77. {
  78. /* FIXME: needs to handle errors/reconnect */
  79. char buf[512];
  80. snprintf( buf, sizeof( buf ), "read_peaks \"%s\" %f %lu %lu\n", source, fpp, start, end );
  81. send( _socket, buf, strlen( buf ), 0 );
  82. if ( recv( _socket, buf, sizeof( int ) * 2, 0 ) != sizeof( int ) * 2 )
  83. {
  84. printf( "error!\n" );
  85. return false;
  86. }
  87. if ( ! strncmp( buf, "error", 5 ) )
  88. return false;
  89. *channels = ((int*)buf)[0];
  90. *peaks = ((int*)buf)[1];
  91. if ( ! ( *peaks && *channels ) )
  92. /* unknown error */;
  93. printf( "reading %d peaks for %d channels\n", *peaks, *channels );
  94. *pbuf = new Peak[ *peaks * *channels ];
  95. for ( int i = 0; i < *channels; ++i )
  96. recv( _socket, *pbuf + (i * *peaks), *peaks * sizeof( Peak ), MSG_WAITALL );
  97. printf( "done.\n" );
  98. return true;
  99. }