#include <iostream.h>
#include <fstream.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <time.h>
#include <sys/time.h>
#include <sys/soundcard.h>
#define BUF_SIZE 4096
#define PROG 2000
#define FREQ 277
char *device=/dev/dsp;
int audio_fd;
short audio_buffer[BUF_SIZE];
int format,channels,speed;
time_t now;
struct tm* GMT;
struct timeval tv;
double t;
int czas;
char tmp[200];
int len;
int koniec=0;
double tau;
int stan,starystan;
int Maxstat;
int Od;
int start=0;
int main(int argc, char** argv)
{
tau=5.0;
if (argc>1)
tau=atof(argv[1]);
//cerr<<Stała integracji: <<tau<< s<<endl;
starystan=1;
Maxstat=200;
int Od=2;
if ((audio_fd=open(device,O_RDONLY,0))==-1)
cerr<<Urządzenie
<<device<<
nie daje się otworzyć.<<endl;
return -2;
}
if (ioctl(audio_fd,SNDCTL_DSP_RESET,0)==-1)
//cerr<<Nie daje sie zresetowac DSP<<endl;
return -1;
popHtmlLanguage
else
// cerr<<Zresetowano DSP.<<endl;
}
format=AFMT_S16_LE;
if (ioctl(audio_fd,SNDCTL_DSP_SETFMT,&format)==-1)
{
//cerr<<Nie daje sie ustawic formatu danych<<endl;
return -1;
}
else if (format==AFMT_S16_LE)
{
//cerr<<Format danych ustawiony na 16 bitow.<<endl;
}
else
{
//cout<<Blad formatu danych<<endl;
return -1;
}
channels=2;
if (ioctl(audio_fd,SNDCTL_DSP_CHANNELS,&channels)==-1)
{
//cerr<<Nie daje sie ustawic stereo<<endl;
return -1;
}
else
{
//cerr<<Ustawiono kanalow <<channels<<endl;
}
speed=48000;
if (ioctl(audio_fd,SNDCTL_DSP_SPEED,&speed)==-1)
{
//cerr<<Nie daje sie ustawic predkosci 44100 Hz<<endl;
return -1;
}
else
{
//cerr<<Ustawiono częstość próbkowania <<speed<< próbek/s/kanał<<endl;
}
int i=0,k,l=0;
double K0=0.0,K180=0.0;
double l0=0.0,l180=0.0;
double D=0.0;
stan=1;
while (1)
{
if ((len=read(audio_fd,audio_buffer,2*BUF_SIZE))==-1)
{
//cerr<</dev/dsp nie da sie czytac.<<endl;
close(audio_fd);
return -1;
}
if (len!=(2*BUF_SIZE))
break;
for (k=0;k<BUF_SIZE/2;k+=2)
{
if (audio_buffer[k]>+PROG)
stan=1;
else if (audio_buffer[k]<-PROG)
stan=-1;
if (stan!=starystan)
{
l++;
if (i<=Od)
i++;
else if (starystan==1)
start=1;
if (start&&(stan==-1))
{
K0/=l0;
K180/=l180;
D+=K0-K180;
if ((double)l >= (FREQ*tau))
{
D/=(double)l;
gettimeofday(&tv,NULL);
now=tv.tv_sec;
GMT=gmtime(&now);
t=(double)GMT->tm_yday+((double)GMT->tm_hour*3600.0
+(double)GMT->tm_min*60.0
+(double)GMT->tm_sec+(double)tv.tv_usec/1.0e6-tau/2.0)/86400.0;
sprintf(tmp,dane/Slonce_%i_%i.dat,
GMT->tm_year+1900,GMT->tm_yday);
ofstream dane(tmp,ios::out|ios::app);
dane.precision(8);
dane<<t<< <<D<<endl;
dane.close();
D=0.0;
l=0;
}
K0=0.0;
K180=0.0;
l0=0.0;
l180=0.0;
}
starystan=stan;
}
if (stan==-1)
{
K0+=(double)audio_buffer[k+1];
l0+=1.0;
}
else if (stan==1)
{
K180+=(double)audio_buffer[k+1];
l180+=1.0;
}
}
if (koniec)
break;
}
close(audio_fd);
return 0;