GWのガイガーカウンタ自作チャレンジ(2台目) ネクスト(14) web公開の暫定ソフト2
このサイトの情報を参考に、少々、エンハンス、自作ガイガーカウンタを接続しているマイク入力の音量を、プログラムから設定しるようにした。ハード構成が代わると、少々、修正が必要出る事を、注意しておく。現在、長時間動作テスト中。
修正後のソースコードは、、、、
修正後のソースコードは、、、、
// gm_cnt_01.cpp : コンソール アプリケーション用のエントリ ポイントの定義
//
#include "stdafx.h"
#include <windows.h>
#include <mmsystem.h>
#include "gm_cnt_01.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// 唯一のアプリケーション オブジェクト
// WinApp theApp;
using namespace std;
WAVEFORMATEX wfe;
WAVEHDR whdr;
short *bWave;
HWAVEOUT hWaveOut;
HWAVEIN hWaveIn;
UINT numDevs;
int MyMain(int argc, TCHAR* argv[], TCHAR* envp[]);
void MyWaveInProc ( HWAVEIN hwi, UINT uMSG, DWORD dwInstance, DWORD dwParam1, DWORD dwParam2 ) {
int ret;
if ( uMSG != 958 ) fprintf( stderr , "CALLBACKE! (%d) @ %d\n", uMSG, __LINE__ );
if ( uMSG == WIM_OPEN ) {
// fprintf( stderr, "WIM_OPEN(%d)\n", __LINE__ );
}
if ( uMSG == WIM_DATA ) {
ret = waveInStop( hWaveIn );
if ( ret != MMSYSERR_NOERROR ) {
fprintf( stderr, "ERROR(%d)\n", __LINE__ );
return ;
}
ret = waveInClose( hWaveIn );
if ( ret != MMSYSERR_NOERROR ) {
fprintf( stderr, "ERROR(%d)\n", __LINE__ );
return ;
}
fprintf( stderr, "WIM_DATA(%d)\n", __LINE__ );
}
}
#define MAX_NUM_DEVS 10
HMIXER hMixer[MAX_NUM_DEVS];
int isOpenedMixer[MAX_NUM_DEVS];
MIXERLINE mixerLine[MAX_NUM_DEVS];
UINT idMic;
int MyMain(int argc, TCHAR* argv[], TCHAR* envp[] )
{
int ret = 0;
int i;
{
int j;
numDevs = mixerGetNumDevs();
if ( numDevs == 0 ) {
fprintf( stderr, "ERROR(%d)\n", __LINE__ );
return -1;
}
fprintf( stderr, "numDevs = %d\n", numDevs );
if ( numDevs > MAX_NUM_DEVS ) numDevs = MAX_NUM_DEVS;
for (i=0, j=0; i<numDevs; i++ ){
ret = mixerOpen( &(hMixer[i]), (UINT)i, 0, 0, MIXER_OBJECTF_MIXER );
if ( ret != MMSYSERR_NOERROR ) {
fprintf( stderr, "ERROR(%d) can't open mixar[%d]\n", __LINE__,
i);
isOpenedMixer[i] = 0;
} else {
j++;
isOpenedMixer[i] = 1;
}
}
if ( j < 1 ) {
fprintf( stderr, "ERROR(%d) cna't open all mixers\n", __LINE__ );
return -1;
} else {
fprintf( stderr, "%d opendMixars\n", j );
}
for (i=0, idMic = -1; i < numDevs; i++ ){
mixerLine[i].cbStruct = sizeof( mixerLine );
mixerLine[i].dwComponentType = MIXERLINE_COMPONENTTYPE_DST_WAVEIN;
ret = mixerGetLineInfo( (HMIXEROBJ)hMixer[i], &(mixerLine[i]),
MIXER_OBJECTF_HMIXER | MIXER_GETLINEINFOF_COMPONENTTYPE );
if ( ret == MMSYSERR_NOERROR ) {
// fprintf( stderr, "found Mic[%d]\n", i );
// fprintf( stderr,
// "Name: %s, %s, %x\n",
// mixerLine[i].szName,
// mixerLine[i].szShortName
// );
if ( (mixerLine[i].szName)[0] != (char)'R' ){
idMic = i;
}
}
}
fprintf( stderr, "Mic ID = %d\n", idMic );
MIXERCONTROL mixerCtrl;
MIXERLINECONTROLS mixerLineCtrls;
mixerLineCtrls.cbStruct = sizeof ( mixerLineCtrls );
mixerLineCtrls.dwLineID = mixerLine[idMic].dwLineID;
mixerLineCtrls.dwControlType = MIXERCONTROL_CONTROLTYPE_VOLUME;
mixerLineCtrls.cControls = 1;
mixerLineCtrls.cbmxctrl = sizeof ( mixerCtrl );
mixerLineCtrls.pamxctrl = &mixerCtrl;
ret = mixerGetLineControls(
(HMIXEROBJ)hMixer[idMic], &mixerLineCtrls,
MIXER_OBJECTF_HMIXER | MIXER_GETLINECONTROLSF_ONEBYTYPE );
if ( ret != MMSYSERR_NOERROR ) {
fprintf( stderr, "ERROR(%d)\n", __LINE__ );
}
{
MIXERCONTROLDETAILS_UNSIGNED micVolume;
MIXERCONTROLDETAILS mixerDts;
mixerDts.cbStruct = sizeof( mixerDts );
mixerDts.dwControlID = mixerCtrl.dwControlID;
mixerDts.cChannels = 1;
mixerDts.cMultipleItems = 0;
mixerDts.cbDetails = sizeof ( MIXERCONTROLDETAILS_UNSIGNED );
mixerDts.paDetails = &micVolume;
ret = mixerGetControlDetails( (HMIXEROBJ)hMixer[idMic],
&mixerDts,
MIXER_GETCONTROLDETAILSF_VALUE );
if (ret != MMSYSERR_NOERROR ){
fprintf( stderr, "ERROR(%d)\n", __LINE__ );
return -1;
}
fprintf( stderr, "Vol: %d, Max: %d\n", micVolume.dwValue,
mixerCtrl.Bounds.dwMaximum );
// micVolume.dwValue = 24348; // needed value for GM counter
micVolume.dwValue = 22000; // needed value for GM counter 音量設定値
mixerDts.cbStruct = sizeof( mixerDts );
mixerDts.dwControlID = mixerCtrl.dwControlID;
mixerDts.cChannels = 1;
mixerDts.cMultipleItems = 0;
mixerDts.cbDetails = sizeof ( MIXERCONTROLDETAILS_UNSIGNED );
mixerDts.paDetails = &micVolume;
ret = mixerSetControlDetails( (HMIXEROBJ)hMixer[idMic],
&mixerDts,
MIXER_SETCONTROLDETAILSF_VALUE );
if (ret != MMSYSERR_NOERROR ){
fprintf( stderr, "ERROR(%d)\n", __LINE__ );
return -1;
}
fprintf( stderr, "Changed to Vol: %d, Max: %d\n", micVolume.dwValue,
mixerCtrl.Bounds.dwMaximum );
}
for (i=0; i<numDevs; i++ ){
ret = mixerClose( hMixer[i] );
}
}
wfe.wFormatTag = WAVE_FORMAT_PCM;
wfe.nChannels = 1;
wfe.nSamplesPerSec = 32 * 1000 ;
wfe.nAvgBytesPerSec = wfe.nSamplesPerSec * 2 ;
wfe.wBitsPerSample = 16 ;
wfe.nBlockAlign = wfe.nChannels * wfe.wBitsPerSample / 16;
whdr.dwBufferLength = wfe.nAvgBytesPerSec * 60 ; // 60 sec
bWave = (short *)malloc( whdr.dwBufferLength );
if ( bWave == NULL ) {
fprintf( stderr, "ERROR(%d)\n", __LINE__ );
return -1 ;
}
whdr.lpData = (char *)bWave;
whdr.dwLoops = 0; // 1;
whdr.dwBytesRecorded = 0;
whdr.dwFlags = 0;
whdr.lpNext = NULL;
whdr.dwUser = 0;
whdr.reserved = 0;
ret = waveInOpen( &hWaveIn, WAVE_MAPPER, &wfe, (unsigned long)MyWaveInProc, 0, CALLBACK_NULL );
if ( ret != MMSYSERR_NOERROR ) {
fprintf( stderr, "ERROR(%d)\n", __LINE__ );
return -1;
}
ret = waveInPrepareHeader( hWaveIn, &whdr, sizeof(WAVEHDR) );
if ( ret != MMSYSERR_NOERROR ) {
fprintf( stderr, "ERROR(%d)\n", __LINE__ );
return -1;
}
ret = waveInAddBuffer( hWaveIn, &whdr, sizeof(WAVEHDR) );
if ( ret != MMSYSERR_NOERROR ) {
fprintf( stderr, "ERROR(%d)\n", __LINE__ );
return -1;
}
ret = waveInStart( hWaveIn );
if ( ret != MMSYSERR_NOERROR ) {
fprintf( stderr, "ERROR(%d)\n", __LINE__ );
return -1;
}
Sleep( 70 * 1000 ); // wait 60 sec
// if ( hWaveIn != -1 )
{
ret = waveInStop( hWaveIn );
if ( ret != MMSYSERR_NOERROR ) {
fprintf( stderr, "ERROR(%d)\n", __LINE__ );
return -1;
}
ret = waveInClose( hWaveIn );
if ( ret != MMSYSERR_NOERROR ) {
fprintf( stderr, "ERROR(%d)\n", __LINE__ );
return -1;
}
}
short max, min, cur, prv, thr;
int cnt;
max = min = 0;
prv = cur = 0;
thr = 27000;
cnt = 0;
SYSTEMTIME mytm;
GetLocalTime( &mytm );
for (i=0; i<whdr.dwBufferLength/2; i++ ){
cur = bWave[i];
if ( max < cur ) max = cur;
if ( min > cur ) min = cur;
if ( prv < thr ) {
if ( cur >= thr ) cnt++;
}
prv = cur;
}
fprintf( stdout, "%04d/%02d/%02d %02d:%02d:%02d, %d, psV/h, %d, %d, %d\n",
mytm.wYear, mytm.wMonth, mytm.wDay, mytm.wHour, mytm.wMinute, mytm.wSecond,
(cnt * 1000) / (3100) , cnt , max, min );
free( bWave );
return ret;
}
//
#include "stdafx.h"
#include <windows.h>
#include <mmsystem.h>
#include "gm_cnt_01.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// 唯一のアプリケーション オブジェクト
// WinApp theApp;
using namespace std;
WAVEFORMATEX wfe;
WAVEHDR whdr;
short *bWave;
HWAVEOUT hWaveOut;
HWAVEIN hWaveIn;
UINT numDevs;
int MyMain(int argc, TCHAR* argv[], TCHAR* envp[]);
void MyWaveInProc ( HWAVEIN hwi, UINT uMSG, DWORD dwInstance, DWORD dwParam1, DWORD dwParam2 ) {
int ret;
if ( uMSG != 958 ) fprintf( stderr , "CALLBACKE! (%d) @ %d\n", uMSG, __LINE__ );
if ( uMSG == WIM_OPEN ) {
// fprintf( stderr, "WIM_OPEN(%d)\n", __LINE__ );
}
if ( uMSG == WIM_DATA ) {
ret = waveInStop( hWaveIn );
if ( ret != MMSYSERR_NOERROR ) {
fprintf( stderr, "ERROR(%d)\n", __LINE__ );
return ;
}
ret = waveInClose( hWaveIn );
if ( ret != MMSYSERR_NOERROR ) {
fprintf( stderr, "ERROR(%d)\n", __LINE__ );
return ;
}
fprintf( stderr, "WIM_DATA(%d)\n", __LINE__ );
}
}
#define MAX_NUM_DEVS 10
HMIXER hMixer[MAX_NUM_DEVS];
int isOpenedMixer[MAX_NUM_DEVS];
MIXERLINE mixerLine[MAX_NUM_DEVS];
UINT idMic;
int MyMain(int argc, TCHAR* argv[], TCHAR* envp[] )
{
int ret = 0;
int i;
{
int j;
numDevs = mixerGetNumDevs();
if ( numDevs == 0 ) {
fprintf( stderr, "ERROR(%d)\n", __LINE__ );
return -1;
}
fprintf( stderr, "numDevs = %d\n", numDevs );
if ( numDevs > MAX_NUM_DEVS ) numDevs = MAX_NUM_DEVS;
for (i=0, j=0; i<numDevs; i++ ){
ret = mixerOpen( &(hMixer[i]), (UINT)i, 0, 0, MIXER_OBJECTF_MIXER );
if ( ret != MMSYSERR_NOERROR ) {
fprintf( stderr, "ERROR(%d) can't open mixar[%d]\n", __LINE__,
i);
isOpenedMixer[i] = 0;
} else {
j++;
isOpenedMixer[i] = 1;
}
}
if ( j < 1 ) {
fprintf( stderr, "ERROR(%d) cna't open all mixers\n", __LINE__ );
return -1;
} else {
fprintf( stderr, "%d opendMixars\n", j );
}
for (i=0, idMic = -1; i < numDevs; i++ ){
mixerLine[i].cbStruct = sizeof( mixerLine );
mixerLine[i].dwComponentType = MIXERLINE_COMPONENTTYPE_DST_WAVEIN;
ret = mixerGetLineInfo( (HMIXEROBJ)hMixer[i], &(mixerLine[i]),
MIXER_OBJECTF_HMIXER | MIXER_GETLINEINFOF_COMPONENTTYPE );
if ( ret == MMSYSERR_NOERROR ) {
// fprintf( stderr, "found Mic[%d]\n", i );
// fprintf( stderr,
// "Name: %s, %s, %x\n",
// mixerLine[i].szName,
// mixerLine[i].szShortName
// );
if ( (mixerLine[i].szName)[0] != (char)'R' ){
idMic = i;
}
}
}
fprintf( stderr, "Mic ID = %d\n", idMic );
MIXERCONTROL mixerCtrl;
MIXERLINECONTROLS mixerLineCtrls;
mixerLineCtrls.cbStruct = sizeof ( mixerLineCtrls );
mixerLineCtrls.dwLineID = mixerLine[idMic].dwLineID;
mixerLineCtrls.dwControlType = MIXERCONTROL_CONTROLTYPE_VOLUME;
mixerLineCtrls.cControls = 1;
mixerLineCtrls.cbmxctrl = sizeof ( mixerCtrl );
mixerLineCtrls.pamxctrl = &mixerCtrl;
ret = mixerGetLineControls(
(HMIXEROBJ)hMixer[idMic], &mixerLineCtrls,
MIXER_OBJECTF_HMIXER | MIXER_GETLINECONTROLSF_ONEBYTYPE );
if ( ret != MMSYSERR_NOERROR ) {
fprintf( stderr, "ERROR(%d)\n", __LINE__ );
}
{
MIXERCONTROLDETAILS_UNSIGNED micVolume;
MIXERCONTROLDETAILS mixerDts;
mixerDts.cbStruct = sizeof( mixerDts );
mixerDts.dwControlID = mixerCtrl.dwControlID;
mixerDts.cChannels = 1;
mixerDts.cMultipleItems = 0;
mixerDts.cbDetails = sizeof ( MIXERCONTROLDETAILS_UNSIGNED );
mixerDts.paDetails = &micVolume;
ret = mixerGetControlDetails( (HMIXEROBJ)hMixer[idMic],
&mixerDts,
MIXER_GETCONTROLDETAILSF_VALUE );
if (ret != MMSYSERR_NOERROR ){
fprintf( stderr, "ERROR(%d)\n", __LINE__ );
return -1;
}
fprintf( stderr, "Vol: %d, Max: %d\n", micVolume.dwValue,
mixerCtrl.Bounds.dwMaximum );
// micVolume.dwValue = 24348; // needed value for GM counter
micVolume.dwValue = 22000; // needed value for GM counter 音量設定値
mixerDts.cbStruct = sizeof( mixerDts );
mixerDts.dwControlID = mixerCtrl.dwControlID;
mixerDts.cChannels = 1;
mixerDts.cMultipleItems = 0;
mixerDts.cbDetails = sizeof ( MIXERCONTROLDETAILS_UNSIGNED );
mixerDts.paDetails = &micVolume;
ret = mixerSetControlDetails( (HMIXEROBJ)hMixer[idMic],
&mixerDts,
MIXER_SETCONTROLDETAILSF_VALUE );
if (ret != MMSYSERR_NOERROR ){
fprintf( stderr, "ERROR(%d)\n", __LINE__ );
return -1;
}
fprintf( stderr, "Changed to Vol: %d, Max: %d\n", micVolume.dwValue,
mixerCtrl.Bounds.dwMaximum );
}
for (i=0; i<numDevs; i++ ){
ret = mixerClose( hMixer[i] );
}
}
wfe.wFormatTag = WAVE_FORMAT_PCM;
wfe.nChannels = 1;
wfe.nSamplesPerSec = 32 * 1000 ;
wfe.nAvgBytesPerSec = wfe.nSamplesPerSec * 2 ;
wfe.wBitsPerSample = 16 ;
wfe.nBlockAlign = wfe.nChannels * wfe.wBitsPerSample / 16;
whdr.dwBufferLength = wfe.nAvgBytesPerSec * 60 ; // 60 sec
bWave = (short *)malloc( whdr.dwBufferLength );
if ( bWave == NULL ) {
fprintf( stderr, "ERROR(%d)\n", __LINE__ );
return -1 ;
}
whdr.lpData = (char *)bWave;
whdr.dwLoops = 0; // 1;
whdr.dwBytesRecorded = 0;
whdr.dwFlags = 0;
whdr.lpNext = NULL;
whdr.dwUser = 0;
whdr.reserved = 0;
ret = waveInOpen( &hWaveIn, WAVE_MAPPER, &wfe, (unsigned long)MyWaveInProc, 0, CALLBACK_NULL );
if ( ret != MMSYSERR_NOERROR ) {
fprintf( stderr, "ERROR(%d)\n", __LINE__ );
return -1;
}
ret = waveInPrepareHeader( hWaveIn, &whdr, sizeof(WAVEHDR) );
if ( ret != MMSYSERR_NOERROR ) {
fprintf( stderr, "ERROR(%d)\n", __LINE__ );
return -1;
}
ret = waveInAddBuffer( hWaveIn, &whdr, sizeof(WAVEHDR) );
if ( ret != MMSYSERR_NOERROR ) {
fprintf( stderr, "ERROR(%d)\n", __LINE__ );
return -1;
}
ret = waveInStart( hWaveIn );
if ( ret != MMSYSERR_NOERROR ) {
fprintf( stderr, "ERROR(%d)\n", __LINE__ );
return -1;
}
Sleep( 70 * 1000 ); // wait 60 sec
// if ( hWaveIn != -1 )
{
ret = waveInStop( hWaveIn );
if ( ret != MMSYSERR_NOERROR ) {
fprintf( stderr, "ERROR(%d)\n", __LINE__ );
return -1;
}
ret = waveInClose( hWaveIn );
if ( ret != MMSYSERR_NOERROR ) {
fprintf( stderr, "ERROR(%d)\n", __LINE__ );
return -1;
}
}
short max, min, cur, prv, thr;
int cnt;
max = min = 0;
prv = cur = 0;
thr = 27000;
cnt = 0;
SYSTEMTIME mytm;
GetLocalTime( &mytm );
for (i=0; i<whdr.dwBufferLength/2; i++ ){
cur = bWave[i];
if ( max < cur ) max = cur;
if ( min > cur ) min = cur;
if ( prv < thr ) {
if ( cur >= thr ) cnt++;
}
prv = cur;
}
fprintf( stdout, "%04d/%02d/%02d %02d:%02d:%02d, %d, psV/h, %d, %d, %d\n",
mytm.wYear, mytm.wMonth, mytm.wDay, mytm.wHour, mytm.wMinute, mytm.wSecond,
(cnt * 1000) / (3100) , cnt , max, min );
free( bWave );
return ret;
}
トラックバック(0)
このブログ記事を参照しているブログ一覧: GWのガイガーカウンタ自作チャレンジ(2台目) ネクスト(14) web公開の暫定ソフト2
このブログ記事に対するトラックバックURL: http://the.nerd.jp/blogs/cgi-bin/mt-tb.cgi/4615