home *** CD-ROM | disk | FTP | other *** search
/ Over Load / MAXIWIN7.ISO / programs / display / samples / vumeter.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-14  |  2.6 KB  |  107 lines

  1. /**********************************************************/
  2. /*/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\*/
  3. /*
  4. CAUTION: Compile using LARGE memory model!!!!!
  5. */
  6. /*/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\*/
  7. /**********************************************************/
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <conio.h>
  12.  
  13. #include "xsnd.h"
  14.  
  15. /**********************************************************/
  16. /* basic block length (samples per block):
  17. too sort ==> too much interrupts per second. */
  18. #define N 200
  19.  
  20. /* sampling rate */
  21. /*#define SRATE 8000*/
  22. #define SRATE 7500  /* shareware version only supports this */
  23.  
  24. /**********************************************************/
  25. /* in this global variable we store the audio level of the
  26. last audio frame */
  27.  
  28. volatile UINT32 level = 0;
  29.  
  30. /**********************************************************/
  31. /* we ignore argument {last}, as this is an end-less program */
  32.  
  33. void myfunc( BOOL last )
  34. {
  35.     INT16 * p;  /* far pointer to 16 bit integer */
  36.     int i;
  37.  
  38.     /* get the recorded block. Can't be NULL, because this callback
  39.     is called when a block is filled. */
  40.     p = xsnd16_getblk();
  41.  
  42.     /* compute audio level. OK, this is not a good algorithm, but
  43.     computing dB needs math.h, and I don't want to :) */
  44.     level = 0;
  45.     for (i=0; i<N; i++)
  46.         level += abs(p[i]);
  47.     level = ((level/N)*80)/(32000);
  48.  
  49.     /* after processing the block, put it again to the recording queue */
  50.     xsnd16_addblk();
  51. }
  52.  
  53. /**********************************************************/
  54.  
  55. int main( void )
  56. {
  57.     int err;
  58.     int i;
  59.  
  60.  
  61.     printf("This programs implements a simple audio vumeter.\n"
  62.         "Audio is recorded and a text-bar displays it's level.\n"
  63.         "Press ENTER to continue...");
  64.     fgetc(stdin);
  65.  
  66.     /* sane state for SB16 */
  67.     xsnd16_initialize();
  68.  
  69.     /* allocate SB16 resources */
  70.     err=xsnd16_open(XSND_RECORD,  /* record */
  71.             SRATE,       /* sampling rate */
  72.             XSND_MONO,   /* mono recording */
  73.             N,          /* block length */
  74.             4,          /* number of blocks */
  75.             myfunc);    /* no user callback */
  76.  
  77.     if (err) {    /* if error */
  78.         printf("can't open sb16!\n");
  79.         return 1;
  80.     }
  81.  
  82.     /* first we add as many free blocks to the recording queue as
  83.     possible. */
  84.     while (!xsnd16_addblk());
  85.  
  86.     xsnd16_start();  /* start recording */
  87.  
  88.     printf("press any key to stop...\n");
  89.  
  90.     do {
  91.         for (i=0; i<79; i++)
  92.             if (i<level)
  93.                 printf("*");
  94.             else
  95.                 printf(" ");
  96.  
  97.         printf("\r");
  98.     } while (!kbhit());
  99.  
  100.     /* free resources */
  101.     xsnd16_close();
  102.  
  103.     return 0;
  104. }
  105.  
  106. /**********************************************************/
  107.