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

  1. /* RxWav
  2.    Copyright (C) 1999 2000  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. Oscillatore sinusoidale:
  30. Vuole il puntatore ad una traccie gia' allocate, la durata in campioni,
  31. la frequenza in hertz e l'ampiezza da 0 a 1
  32. ***********************************************************************/
  33. ULONG
  34. WavSinOsc (PCSZ name, LONG argc, const RXSTRING * argv,
  35.        PCSZ queuename, PRXSTRING retstr)
  36. {
  37.   PSHORT pCh = NULL;
  38.   ULONG nCamp, nCampAggiunti;
  39.   double f, a, j;
  40.   double k;
  41.   INT i, dither;
  42.   APIRET rc;
  43.  
  44.   if ((argc < 4) | (argc > 5))
  45.     {
  46.       SendMsg (FUNC_SIN_OSC, ERR_NUMERO_PARAMETRI);
  47.       return INVALID_ROUTINE;
  48.     }
  49.  
  50.   if (!sscanf (argv[0].strptr, "%d", &pCh))
  51.     {
  52.       SendMsg (FUNC_SIN_OSC, ERR_PUNTATORE_ERRATO);
  53.       return INVALID_ROUTINE;
  54.     }
  55.  
  56.   nCamp = atol (argv[1].strptr);
  57.   if (!nCamp)
  58.     {
  59.       SendMsg (FUNC_SIN_OSC, ERR_NUMERO_CAMPIONI);
  60.       return INVALID_ROUTINE;
  61.     }
  62.  
  63.   f = atof (argv[2].strptr);
  64.  
  65.   if ((f < 1) || (f > (FreqCamp / 2)))
  66.     {
  67.       SendMsg (FUNC_SIN_OSC, ERR_FREQUENZA_OSCILLATORE);
  68.       return INVALID_ROUTINE;
  69.     }
  70.  
  71.   a = atof (argv[3].strptr);
  72.   if ((a < 0) || (a > 1))
  73.     {
  74.       SendMsg (FUNC_SIN_OSC, ERR_AMPIEZZA_OSCILLATORE);
  75.       return INVALID_ROUTINE;
  76.     }
  77.  
  78.   if (argc > 4)
  79.     {
  80.       if (!strncmp (argv[4].strptr, "D", 1))
  81.     dither = TRUE;
  82.       else
  83.     dither = FALSE;
  84.     }
  85.  
  86.  
  87.  
  88.   pCh = AllineaCh (pCh, nCamp, FUNC_SIN_OSC);
  89.   if (!pCh)
  90.     return INVALID_ROUTINE;
  91.  
  92.   j = 0;
  93.   a = a * MAX_CAMPIONE;
  94.   k = ((float) M_PI * 2) / (FreqCamp / f);
  95.  
  96.   if (dither)
  97.     for (i = nCamp; i != 0; i--)
  98.       {
  99.     *pCh = sin (j) * a + rand () - rand ();
  100.     pCh++;
  101.     j = j + k;
  102.       }
  103.   else
  104.     for (i = nCamp; i != 0; i--)
  105.       {
  106.     *pCh = sin (j) * a;
  107.     pCh++;
  108.     j = j + k;
  109.       }
  110.  
  111.   nCampAggiunti = 0;
  112.   if (sin (j) > 0)
  113.     {
  114.       do
  115.     {
  116.       if (dither)
  117.         *pCh = sin (j) * a + rand () - rand ();
  118.       else
  119.         *pCh = sin (j) * a;
  120.       pCh++;
  121.       nCampAggiunti++;
  122.       j = j + k;
  123.     }
  124.       while (sin (j) > 0);
  125.     }
  126.   else
  127.     {
  128.       do
  129.     {
  130.       if (dither)
  131.         *pCh = sin (j) * a + rand () - rand ();
  132.       else
  133.         *pCh = sin (j) * a;
  134.       pCh++;
  135.       nCampAggiunti++;
  136.       j = j + k;
  137.     }
  138.       while (sin (j) < 0);
  139.     }
  140.  
  141.  
  142.   sprintf (retstr->strptr, "%i", ERR_OK);
  143.   retstr->strlength = strlen (retstr->strptr);
  144.   return VALID_ROUTINE;
  145. }
  146.  
  147.  
  148. /***********************************************************************
  149. Oscillatore sinusoidale di test:
  150. ***********************************************************************/
  151. ULONG
  152. WavTestOsc (PCSZ name, LONG argc, const RXSTRING * argv,
  153.         PCSZ queuename, PRXSTRING retstr)
  154. {
  155.   PSHORT pCh = NULL;
  156.   ULONG nCamp, nCampAggiunti;
  157.   double f, a, j;
  158.   double k;
  159.   INT i;
  160.   APIRET rc;
  161.  
  162.   if (argc != 3)
  163.     {
  164.       SendMsg (FUNC_TEST_OSC, ERR_NUMERO_PARAMETRI);
  165.       return INVALID_ROUTINE;
  166.     }
  167.  
  168.   if (!sscanf (argv[0].strptr, "%d", &pCh))
  169.     {
  170.       SendMsg (FUNC_TEST_OSC, ERR_PUNTATORE_ERRATO);
  171.       return INVALID_ROUTINE;
  172.     }
  173.  
  174.   nCamp = atol (argv[1].strptr);
  175.   if (!nCamp)
  176.     {
  177.       SendMsg (FUNC_TEST_OSC, ERR_NUMERO_CAMPIONI);
  178.       return INVALID_ROUTINE;
  179.     }
  180.  
  181.   a = atof (argv[2].strptr);
  182.   if ((a < 0) || (a > 1))
  183.     {
  184.       SendMsg (FUNC_TEST_OSC, ERR_AMPIEZZA_OSCILLATORE);
  185.       return INVALID_ROUTINE;
  186.     }
  187.  
  188.   pCh = AllineaCh (pCh, nCamp, FUNC_TEST_OSC);
  189.   if (!pCh)
  190.     return INVALID_ROUTINE;
  191.  
  192.   j = 0;
  193.   f = 20;
  194.   a = a * 32000;
  195.   k = ((float) M_PI * 2) / (FreqCamp / f);
  196.  
  197.   for (i = nCamp; i != 0; i--)
  198.     {
  199.       *pCh = sin (j) * a;
  200.       pCh++;
  201.       f = f * (float) (1 + ((float) 7 / (float) nCamp));
  202.       if (f > 20480)
  203.     i = 1;
  204.       k = ((float) M_PI * 2) / (FreqCamp / f);
  205.       j = j + k;
  206.     }
  207.  
  208.   nCampAggiunti = 0;
  209.   if (sin (j) > 0)
  210.     {
  211.       do
  212.     {
  213.       *pCh = sin (j) * a;
  214.       pCh++;
  215.       nCampAggiunti++;
  216.       j = j + k;
  217.     }
  218.       while (sin (j) > 0);
  219.     }
  220.   else
  221.     {
  222.       do
  223.     {
  224.       *pCh = sin (j) * a;
  225.       pCh++;
  226.       nCampAggiunti++;
  227.       j = j + k;
  228.     }
  229.       while (sin (j) < 0);
  230.     }
  231.  
  232.  
  233.   sprintf (retstr->strptr, "%i", ERR_OK);
  234.   retstr->strlength = strlen (retstr->strptr);
  235.   return VALID_ROUTINE;
  236. }
  237.  
  238.  
  239. /***********************************************************************
  240. Rumore bianco ?
  241. ***********************************************************************/
  242. ULONG
  243. WavWhiteNoiseOsc (PCSZ name, LONG argc, const RXSTRING * argv,
  244.           PCSZ queuename, PRXSTRING retstr)
  245. {
  246.   PSHORT pCh = NULL;
  247.   ULONG nCamp, nCampAggiunti;
  248.   double a, b, c;
  249.   INT i;
  250.   APIRET rc;
  251.  
  252.   if (argc != 3)
  253.     {
  254.       SendMsg (FUNC_WHITE_NOISE, ERR_NUMERO_PARAMETRI);
  255.       return INVALID_ROUTINE;
  256.     }
  257.  
  258.   if (!sscanf (argv[0].strptr, "%d", &pCh))
  259.     {
  260.       SendMsg (FUNC_WHITE_NOISE, ERR_PUNTATORE_ERRATO);
  261.       return INVALID_ROUTINE;
  262.     }
  263.  
  264.   nCamp = atol (argv[1].strptr);
  265.   if (!nCamp)
  266.     {
  267.       SendMsg (FUNC_WHITE_NOISE, ERR_NUMERO_CAMPIONI);
  268.       return INVALID_ROUTINE;
  269.     }
  270.  
  271.   a = atof (argv[2].strptr);
  272.   if ((a < 0) || (a > 1))
  273.     {
  274.       SendMsg (FUNC_WHITE_NOISE, ERR_AMPIEZZA_OSCILLATORE);
  275.       return INVALID_ROUTINE;
  276.     }
  277.  
  278.   pCh = AllineaCh (pCh, nCamp, FUNC_WHITE_NOISE);
  279.   if (!pCh)
  280.     return INVALID_ROUTINE;
  281.  
  282.   b = 0;
  283.   c = 0;
  284.  
  285.   for (i = nCamp; i != 0; i--)
  286.     {
  287.       b = rand () * a;
  288.       c = (c / 2) + b;
  289.       *pCh = b;
  290.       pCh++;
  291.     }
  292.  
  293.   sprintf (retstr->strptr, "%i", ERR_OK);
  294.   retstr->strlength = strlen (retstr->strptr);
  295.   return VALID_ROUTINE;
  296. }
  297.  
  298.  
  299. /***********************************************************************
  300. Rumore rosa ?
  301. ***********************************************************************/
  302. ULONG
  303. WavPinkNoiseOsc (PCSZ name, LONG argc, const RXSTRING * argv,
  304.          PCSZ queuename, PRXSTRING retstr)
  305. {
  306.   PSHORT pCh = NULL;
  307.   ULONG nCamp;
  308.   double a, w, p, b0, b1, b2, b3, b4, b5, b6;
  309.   double k0, k1, k2, k3, k4, k5, k6;
  310.   double h0, h1, h2, h3, h4, h5, h6;
  311.   INT i;
  312.   APIRET rc;
  313.  
  314.   if (argc != 3)
  315.     {
  316.       SendMsg (FUNC_PINK_NOISE, ERR_NUMERO_PARAMETRI);
  317.       return INVALID_ROUTINE;
  318.     }
  319.  
  320.   if (!sscanf (argv[0].strptr, "%d", &pCh))
  321.     {
  322.       SendMsg (FUNC_PINK_NOISE, ERR_PUNTATORE_ERRATO);
  323.       return INVALID_ROUTINE;
  324.     }
  325.  
  326.   nCamp = atol (argv[1].strptr);
  327.   if (!nCamp)
  328.     {
  329.       SendMsg (FUNC_PINK_NOISE, ERR_NUMERO_CAMPIONI);
  330.       return INVALID_ROUTINE;
  331.     }
  332.  
  333.   a = atof (argv[2].strptr);
  334.   if ((a < 0) || (a > 1))
  335.     {
  336.       SendMsg (FUNC_PINK_NOISE, ERR_AMPIEZZA_OSCILLATORE);
  337.       return INVALID_ROUTINE;
  338.     }
  339.  
  340.   pCh = AllineaCh (pCh, nCamp, FUNC_PINK_NOISE);
  341.   if (!pCh)
  342.     return INVALID_ROUTINE;
  343.  
  344.   b0 = 0;
  345.   b1 = 0;
  346.   b2 = 0;
  347.   b3 = 0;
  348.   b4 = 0;
  349.   b5 = 0;
  350.   b6 = 0;
  351.   k0 = 0.99886;
  352.   k1 = 0.99332;
  353.   k2 = 0.96900;
  354.   k3 = 0.86650;
  355.   k4 = 0.55000;
  356.   k5 = -0.7616;
  357.   k6 = 0.5362;
  358.   h0 = 0.0555179;
  359.   h1 = 0.0750759;
  360.   h2 = 0.1538520;
  361.   h3 = 0.3104856;
  362.   h4 = 0.5329522;
  363.   h5 = 0.0168980;
  364.   h6 = 0.115926;
  365.   a = a / 4;
  366.  
  367.   for (i = nCamp; i != 0; i--)
  368.     {
  369.       w = rand () * a;
  370.       b0 = k0 * b0 + w * h0;
  371.       b1 = k1 * b1 + w * h1;
  372.       b2 = k2 * b2 + w * h2;
  373.       b3 = k3 * b3 + w * h3;
  374.       b4 = k4 * b4 + w * h4;
  375.       b5 = k5 * b5 - w * h5;
  376.       p = b0 + b1 + b2 + b3 + b4 + b5 + b6 + w * k6;
  377.       b6 = w * h6;
  378.       *pCh++ = p;
  379.     }
  380.  
  381.   sprintf (retstr->strptr, "%i", ERR_OK);
  382.   retstr->strlength = strlen (retstr->strptr);
  383.   return VALID_ROUTINE;
  384. }
  385.  
  386.  
  387. /***********************************************************************
  388. Silenzia una traccia
  389. ***********************************************************************/
  390. ULONG
  391. WavMute (PCSZ name, LONG argc, const RXSTRING * argv,
  392.      PCSZ queuename, PRXSTRING retstr)
  393. {
  394.   PSHORT pCh = NULL;
  395.   ULONG nCamp;
  396.   double f, a, j;
  397.   double k;
  398.   INT i;
  399.   APIRET rc;
  400.  
  401.   if (argc != 2)
  402.     {
  403.       SendMsg (FUNC_MUTE, ERR_NUMERO_PARAMETRI);
  404.       return INVALID_ROUTINE;
  405.     }
  406.  
  407.   if (!sscanf (argv[0].strptr, "%d", &pCh))
  408.     {
  409.       SendMsg (FUNC_MUTE, ERR_PUNTATORE_ERRATO);
  410.       return INVALID_ROUTINE;
  411.     }
  412.  
  413.   nCamp = atol (argv[1].strptr);
  414.   if (!nCamp)
  415.     {
  416.       SendMsg (FUNC_MUTE, ERR_NUMERO_CAMPIONI);
  417.       return INVALID_ROUTINE;
  418.     }
  419.  
  420.   pCh = AllineaCh (pCh, nCamp, FUNC_MUTE);
  421.   if (!pCh)
  422.     return INVALID_ROUTINE;
  423.  
  424.   for (i = nCamp; i != 0; i--)
  425.     {
  426.       *pCh = 0;
  427.       pCh++;
  428.     }
  429.  
  430.   sprintf (retstr->strptr, "%i", ERR_OK);
  431.   retstr->strlength = strlen (retstr->strptr);
  432.   return VALID_ROUTINE;
  433. }
  434.