Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // test2.cpp : main project file.
- #include "stdafx.h"
- #include "stdio.h"
- #include <windows.h>
- #include <mmsystem.h>
- using namespace System;
- const char *byte_to_binary
- (
- int x
- )
- {
- static char b[17];
- b[0] = '\0';
- int z;
- for (z = 65536; z > 0; z >>= 1)
- {
- strcat(b, ((x & z) == z) ? "1" : "0");
- }
- return b;
- }
- static HWAVEIN hWaveIn;
- static WAVEHDR WaveInHdr;
- static MMRESULT result;
- static const int NUMPTS = 44100 * 1; // seconds
- static int sampleRate = 44100;
- static short int waveIn[NUMPTS]; // 'short int' is a 16-bit type; I request 16-bit samples below
- // for 8-bit capture, you'd use 'unsigned char' or 'BYTE' 8-bit types
- void CALLBACK waveInProc(
- HWAVEIN hwi,
- UINT uMsg,
- DWORD_PTR dwInstance,
- DWORD_PTR dwParam1,
- DWORD_PTR dwParam2
- ){
- if(uMsg == MM_WIM_DATA){
- result = waveInAddBuffer(hWaveIn, &WaveInHdr, sizeof(WaveInHdr));
- if (result)
- {
- printf("Failed to read block from device\n" );
- return ;
- }
- long int sum = 0 ;
- for(int i = 0; i < NUMPTS; i++ ){
- for(int b = 0; b < 1; b++){ // брать последние x бит
- if( waveIn[i] & (1 << b) ){
- sum++;
- }else{
- sum--;
- }
- }
- }
- printf(" %d\n", sum);
- }
- }
- int main(array<System::String ^> ^args)
- {
- // Specify recording parameters
- WAVEFORMATEX pFormat;
- pFormat.wFormatTag=WAVE_FORMAT_PCM; // simple, uncompressed format
- pFormat.nChannels=1; // 1=mono, 2=stereo
- pFormat.nSamplesPerSec=sampleRate; // 44100
- pFormat.nAvgBytesPerSec=sampleRate*2; // = nSamplesPerSec * n.Channels * wBitsPerSample/8
- pFormat.nBlockAlign=2; // = n.Channels * wBitsPerSample/8
- pFormat.wBitsPerSample=16; // 16 for high quality, 8 for telephone-grade
- pFormat.cbSize=0;
- result = waveInOpen(&hWaveIn, WAVE_MAPPER,&pFormat,
- (DWORD) waveInProc, 0L, WAVE_FORMAT_DIRECT | CALLBACK_FUNCTION );
- if (result)
- {
- printf("Failed to open waveform input device.");
- return 0;
- }
- // Set up and prepare header for input
- WaveInHdr.lpData = (LPSTR)waveIn;
- WaveInHdr.dwBufferLength = NUMPTS*2;
- WaveInHdr.dwBytesRecorded=0;
- WaveInHdr.dwUser = 0L;
- WaveInHdr.dwFlags = 0L;
- WaveInHdr.dwLoops = 0L;
- waveInPrepareHeader(hWaveIn, &WaveInHdr, sizeof(WaveInHdr));
- // Insert a wave input buffer
- result = waveInAddBuffer(hWaveIn, &WaveInHdr, sizeof(WaveInHdr));
- if (result)
- {
- printf("Failed to read block from device" );
- return 0;
- }
- // Commence sampling input
- result = waveInStart(hWaveIn);
- if (result)
- {
- printf("Failed to start recording");
- return 0;
- }
- while(1){Sleep(99999999);}
- waveInClose(hWaveIn);
- return 0;
- }
Add Comment
Please, Sign In to add comment