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.

151 lines
4.2KB

  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 "Peak_Server.H"
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <sys/socket.h>
  22. /* Peak Server
  23. The Peak Server streams peak data to any Timeline Editors or other
  24. clients that ask for it.
  25. Peak request looks like (in ASCII)
  26. > read_peaks "foo.wav" fpp start end
  27. Where "foo.wav" is the base name. (actual filenames may differ if
  28. the channels of the source are 'broken out')
  29. Response looks like (in binary)
  30. > (int)channels (int)length (float)min max min max min max
  31. > min max min max
  32. Were length specifies the number of Peaks (min/max pairs) (for each
  33. channel) The first channel is transmitted first, and any others
  34. follow.
  35. */
  36. #include "Audio_File.H"
  37. #include "Peaks.H"
  38. typedef unsigned long tick_t;
  39. #define PEAK_PORT 6100
  40. void
  41. Peak_Server::handle_new ( int s )
  42. {
  43. printf( "new connection\n" );
  44. }
  45. void
  46. Peak_Server::handle_hang_up ( int s )
  47. {
  48. printf( "hangup\n" );
  49. }
  50. void
  51. Peak_Server::handle_request ( int s, const char *buf, int l )
  52. {
  53. printf( "request: %s", buf );
  54. char source[512];
  55. float fpp;
  56. tick_t start, end;
  57. enum { GET_INFO, READ_PEAKS } request;
  58. if ( 1 == sscanf( buf, "get_info \"%[^\"]\"", source ) )
  59. {
  60. request = GET_INFO;
  61. }
  62. else if ( 4 == sscanf( buf, "read_peaks \"%[^\"]\" %f %lu %lu", source, &fpp, &start, &end ) )
  63. {
  64. request = READ_PEAKS;
  65. }
  66. else
  67. {
  68. const char *err = "error: malformed request\n";
  69. fprintf( stderr, err );
  70. send( s, err, strlen( err ), 0 );
  71. return;
  72. }
  73. Audio_File *af = Audio_File::from_file( source );
  74. if ( ! af )
  75. {
  76. const char *err = "error: could not open source\n";
  77. send( s, err, strlen( err ), 0 );
  78. return;
  79. }
  80. switch ( request )
  81. {
  82. case GET_INFO:
  83. {
  84. char buf[128];
  85. snprintf( buf, sizeof( buf ), "length=%lu channels=%d\n", af->length(), af->channels() );
  86. send( s, buf, strlen( buf ), 0 );
  87. break;
  88. }
  89. case READ_PEAKS:
  90. {
  91. int data[2];
  92. int peaks;
  93. data[0] = af->channels();
  94. data[1] = peaks = (end - start) / fpp;
  95. send( s, &data, sizeof( data ), 0 );
  96. const Peaks *pk = af->peaks();
  97. int npeaks = pk->fill_buffer( fpp, start, end );
  98. int channels = af->channels();
  99. Peak *pbuf = new Peak[ npeaks * channels ];
  100. /* deinterlace */
  101. int k = 0;
  102. for ( int i = 0; i < channels; i++ )
  103. for ( int j = i; j < npeaks * channels; j += channels )
  104. pbuf[ k++ ] = pk->peakbuf()[ j ];
  105. /* transmit */
  106. send( s, pbuf, sizeof( Peak ) * npeaks * channels, 0 );
  107. delete pbuf;
  108. break;
  109. }
  110. }
  111. // delete af;
  112. }