Tone generator

Robert Olson olson at mcs.anl.gov
Tue Sep 21 15:46:28 CDT 2004


Eric was looking for a sample of libcommon-based data sending code. I put 
toegether a simple network tone generator for the swof stuff, and it's 
enclosed here. I've dropped the swof-alib code into FL CVS if you want to 
see what's going on there. the RTPSession object is just a very simple 
wrapper around a libcommon session object.

--bob

#include "alib.h"
#include <math.h>

int main(int argc, char **argv)
{
     RTPSession s("192.168.1.13", 10000, 10000);
     //RTPSession s("192.168.0.10", 10000, 10000);
     //RTPSession s("224.2.1.100", 10000, 10000);


     int pt = 112;
     int n = 320;
     int tick = 0;

     short buf[n];

     float gain = 0.1;

     float sampleFreq = 16000.0;
     float sineFreq = 440.0;
     float twopi = M_PI * 2.0;

     if (argc > 1)
     {
	gain = atof(argv[1]);
     }
     if (argc > 2)
     {
	sineFreq = atof(argv[2]);
     }

     unsigned long ts = 0;

     struct timeval tv;
     ::gettimeofday(&tv, 0);

     double start = (double) tv.tv_sec  + (double) tv.tv_usec / 1e6;
     double next = start + 0.020;

     struct rtp *sess = s.get_session();
     int m = 1;

     printf("Running\n");
     while (1)
     {
	for (int i = 0; i < n; i++)
	{
	    float t = (float) tick / sampleFreq;
	    float y = sin(twopi * sineFreq * t);
	    // y = drand48();
	    buf[i] = htons((short) (gain * y * 32768.0));
	    tick++;
	}

	s.probe(20);

	::gettimeofday(&tv, 0);
	double now = (double) tv.tv_sec  + (double) tv.tv_usec / 1e6;
	double wait = next - now;
	int sleept = (int) (wait * 1e6);
	//printf("next=%f now=%f wait=%f\n", next, now, wait);
	if (sleept > 100)
	{
	    //printf("sleep %d\n", sleept);
	    usleep(sleept);
	}
	next += 0.020;

	rtp_send_data(sess, ts, pt, m, 0, 0, (char *) buf, sizeof(buf), 0, 0, 0);
	rtp_send_ctrl(sess, ts, 0);
	m = 0;

	ts += n;
     }
}




More information about the ag-dev mailing list