home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: Multimed / Multimed.zip / rxwavsrc.zip / RxWavPeak.c < prev    next >
C/C++ Source or Header  |  2000-03-06  |  4KB  |  152 lines

  1. /*  RxWav
  2.    Copyright (C) 1999  Giorgio Vicario
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2 of the License, or
  7.    (at your option) any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA     */
  17.  
  18. #define INCL_REXXSAA
  19. #include <os2emx.h>
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <regexp.h>
  24. #include <math.h>
  25. #include <float.h>
  26. #include "RxWav.h"
  27.  
  28. /***********************************************************************
  29. Peak
  30. Vuole il puntatore ad un campione in una traccia gia' allocata e la
  31. durata della traccia in campioni;
  32. restituisce il valore di picco massimo
  33. ***********************************************************************/
  34. ULONG
  35. WavPeak (PCSZ name, LONG argc, const RXSTRING * argv,
  36.      PCSZ queuename, PRXSTRING retstr)
  37. {
  38.   PSHORT pCh = NULL;
  39.   ULONG nCamp, i;
  40.   SHORT c;
  41.   double max;
  42.   APIRET rc;
  43.  
  44.   if (argc != 2)
  45.     {
  46.       SendMsg (FUNC_PEAK, ERR_NUMERO_PARAMETRI);
  47.       return INVALID_ROUTINE;
  48.     }
  49.  
  50.   if (!sscanf (argv[0].strptr, "%d", &pCh))
  51.     {
  52.       SendMsg (FUNC_PEAK, ERR_PUNTATORE_ERRATO);
  53.       return INVALID_ROUTINE;
  54.     }
  55.  
  56.   nCamp = atol (argv[1].strptr);
  57.   if (nCamp < 1)
  58.     {
  59.       SendMsg (FUNC_PEAK, ERR_VALORE_INVALIDO);
  60.       return INVALID_ROUTINE;
  61.     }
  62.  
  63.   pCh = AllineaCh (pCh, nCamp + 2, FUNC_PEAK);
  64.   if (!pCh)
  65.     return INVALID_ROUTINE;
  66.  
  67.   c = 0;
  68.   max = 0;
  69.   for (i = nCamp; i != 0; i--)
  70.     {
  71.       if (abs (*pCh) > c)
  72.     c = abs (*pCh);
  73.       pCh++;
  74.     }
  75.  
  76.   max = (double) c / MAX_CAMPIONE;
  77.  
  78.   sprintf (retstr->strptr, "%f", max);
  79.   retstr->strlength = strlen (retstr->strptr);
  80.   return VALID_ROUTINE;
  81. }
  82.  
  83.  
  84. /***********************************************************************
  85. Avg
  86. Vuole il puntatore ad un campione in una traccia gia' allocata e la
  87. durata della traccia in campioni;
  88. restituisce il valore medio segnato e quello assoluto
  89. ***********************************************************************/
  90. ULONG
  91. WavAvg (PCSZ name, LONG argc, const RXSTRING * argv,
  92.     PCSZ queuename, PRXSTRING retstr)
  93. {
  94.   PSHORT pCh, pTemp;
  95.   SHORT d;
  96.   ULONG nCamp, i;
  97.   double avg, avg2, scostamento;
  98.   APIRET rc;
  99.  
  100.   if ((argc < 2) | (argc > 3))
  101.     {
  102.       SendMsg (FUNC_AVG, ERR_NUMERO_PARAMETRI);
  103.       return INVALID_ROUTINE;
  104.     }
  105.  
  106.   if (!sscanf (argv[0].strptr, "%d", &pCh))
  107.     {
  108.       SendMsg (FUNC_AVG, ERR_PUNTATORE_ERRATO);
  109.       return INVALID_ROUTINE;
  110.     }
  111.  
  112.   nCamp = atol (argv[1].strptr);
  113.   if (nCamp < 1)
  114.     {
  115.       SendMsg (FUNC_AVG, ERR_VALORE_INVALIDO);
  116.       return INVALID_ROUTINE;
  117.     }
  118.  
  119.   pCh = AllineaCh (pCh, nCamp, FUNC_AVG);
  120.   if (!pCh)
  121.     return INVALID_ROUTINE;
  122.  
  123.   pTemp = pCh;
  124.  
  125.   avg = 0;
  126.   for (i = nCamp; i != 0; i--)
  127.     {
  128.       avg = avg + *pTemp;
  129.       avg2 = avg2 + abs (*pTemp);
  130.       pTemp++;
  131.     }
  132.  
  133.   avg = (avg / (double) nCamp) / (double) MAX_CAMPIONE;
  134.   avg2 = (avg2 / (double) nCamp) / (double) MAX_CAMPIONE;
  135.  
  136.   if (argc == 3)
  137.     {
  138.       scostamento = atof (argv[2].strptr);
  139.       d = (scostamento - avg) * MAX_CAMPIONE;
  140.       for (i = nCamp; i != 0; i--)
  141.     {
  142.       *pCh = *pCh + d;
  143.       pCh++;
  144.     }
  145.     }
  146.  
  147.  
  148.   sprintf (retstr->strptr, "%f %f", avg, avg2);
  149.   retstr->strlength = strlen (retstr->strptr);
  150.   return VALID_ROUTINE;
  151. }
  152.