Guest User

Untitled

a guest
Jun 18th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.81 KB | None | 0 0
  1. // test2.cpp : main project file.
  2.  
  3. #include "stdafx.h"
  4. #include "stdio.h"
  5. #include <windows.h>
  6. #include <mmsystem.h>
  7.  
  8. using namespace System;
  9.  
  10. const char *byte_to_binary
  11. (
  12.     int x
  13. )
  14. {
  15.     static char b[17];
  16.     b[0] = '\0';
  17.  
  18.     int z;
  19.     for (z = 65536; z > 0; z >>= 1)
  20.     {
  21.         strcat(b, ((x & z) == z) ? "1" : "0");
  22.     }
  23.  
  24.     return b;
  25. }
  26.  
  27. static HWAVEIN      hWaveIn;
  28. static WAVEHDR      WaveInHdr;
  29. static MMRESULT result;
  30. static const int NUMPTS = 44100 * 1;   // seconds
  31. static int sampleRate = 44100;
  32. static short int waveIn[NUMPTS];   // 'short int' is a 16-bit type; I request 16-bit samples below
  33.                                  // for 8-bit capture, you'd use 'unsigned char' or 'BYTE' 8-bit types
  34.  
  35.  
  36.  
  37.  void CALLBACK waveInProc(
  38.   HWAVEIN hwi,
  39.   UINT uMsg,
  40.   DWORD_PTR dwInstance,
  41.   DWORD_PTR dwParam1,
  42.   DWORD_PTR dwParam2
  43. ){
  44.     if(uMsg == MM_WIM_DATA){
  45.  
  46.  
  47.        
  48.         result = waveInAddBuffer(hWaveIn, &WaveInHdr, sizeof(WaveInHdr));
  49.          if (result)
  50.          {
  51.           printf("Failed to read block from device\n" );
  52.           return ;
  53.          }
  54.  
  55.          long int sum = 0 ;
  56.  
  57.          for(int i = 0; i < NUMPTS; i++ ){
  58.              for(int b = 0; b < 1; b++){ // брать последние x бит
  59.  
  60.                 if( waveIn[i] & (1 << b) ){
  61.                     sum++;
  62.                 }else{
  63.                     sum--;
  64.                 }
  65.  
  66.              }
  67.          }
  68.  
  69.             printf(" %d\n", sum);
  70.  
  71.  
  72.     }
  73. }
  74.  
  75.  
  76.  
  77. int main(array<System::String ^> ^args)
  78. {
  79.  
  80.  
  81.  
  82.      
  83.      
  84.  
  85.      // Specify recording parameters
  86.      WAVEFORMATEX pFormat;
  87.      pFormat.wFormatTag=WAVE_FORMAT_PCM;     // simple, uncompressed format
  88.      pFormat.nChannels=1;                    //  1=mono, 2=stereo
  89.      pFormat.nSamplesPerSec=sampleRate;      // 44100
  90.      pFormat.nAvgBytesPerSec=sampleRate*2;   // = nSamplesPerSec * n.Channels * wBitsPerSample/8
  91.      pFormat.nBlockAlign=2;                  // = n.Channels * wBitsPerSample/8
  92.      pFormat.wBitsPerSample=16;              //  16 for high quality, 8 for telephone-grade
  93.      pFormat.cbSize=0;
  94.  
  95.      result = waveInOpen(&hWaveIn, WAVE_MAPPER,&pFormat,
  96.                 (DWORD) waveInProc, 0L, WAVE_FORMAT_DIRECT | CALLBACK_FUNCTION );
  97.      if (result)
  98.      {
  99.       printf("Failed to open waveform input device.");
  100.       return 0;
  101.      }
  102.  
  103.      // Set up and prepare header for input
  104.      WaveInHdr.lpData = (LPSTR)waveIn;
  105.      WaveInHdr.dwBufferLength = NUMPTS*2;
  106.      WaveInHdr.dwBytesRecorded=0;
  107.      WaveInHdr.dwUser = 0L;
  108.      WaveInHdr.dwFlags = 0L;
  109.      WaveInHdr.dwLoops = 0L;
  110.      waveInPrepareHeader(hWaveIn, &WaveInHdr, sizeof(WaveInHdr));
  111.  
  112.  
  113.      // Insert a wave input buffer
  114.      result = waveInAddBuffer(hWaveIn, &WaveInHdr, sizeof(WaveInHdr));
  115.      if (result)
  116.      {
  117.       printf("Failed to read block from device" );
  118.       return 0;
  119.      }
  120.  
  121.      // Commence sampling input
  122.      result = waveInStart(hWaveIn);
  123.      if (result)
  124.      {
  125.       printf("Failed to start recording");
  126.       return 0;
  127.      }
  128.  
  129.  
  130.      while(1){Sleep(99999999);}
  131.  
  132.  
  133.      waveInClose(hWaveIn);
  134.  
  135.  
  136.  
  137.     return 0;
  138. }
Add Comment
Please, Sign In to add comment