Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <alsa/asoundlib.h>
- #include <iostream>
- #define GET_BIT(X,N) ( ( (X) >> (N) ) & 1 )
- void ad7771_data_to_voltage(double ref, uint32_t *raw_code, double *voltage) {
- if((*raw_code <= 0x7FFFFF) && (*raw_code >= 0x00)) {
- *voltage = (double)(*raw_code * (ref/16777216.0));
- }
- else {
- *voltage = (double)((*raw_code * (ref/16777216.0))-ref);
- }
- }
- timespec diff(timespec start, timespec end){
- timespec temp;
- if ((end.tv_nsec-start.tv_nsec)<0) {
- temp.tv_sec = end.tv_sec-start.tv_sec-1;
- temp.tv_nsec = 1000000000+end.tv_nsec-start.tv_nsec;
- } else {
- temp.tv_sec = end.tv_sec-start.tv_sec;
- temp.tv_nsec = end.tv_nsec-start.tv_nsec;
- }
- return temp;
- }
- int main (int argc, char *argv[]) {
- double ref = 2.5;
- uint32_t raw_code = 0;
- double voltage = -1;
- int i;
- int err;
- snd_pcm_t *capture_handle;
- snd_pcm_hw_params_t *hw_params;
- //const int datarate=128000;
- if ((err = snd_pcm_open (&capture_handle, "hw", SND_PCM_STREAM_CAPTURE, 0)) < 0) {
- fprintf (stderr, "cannot open audio device %s (%s)\n", argv[1], snd_strerror (err));
- exit (1);
- }
- if ((err = snd_pcm_hw_params_malloc (&hw_params)) < 0) {
- fprintf (stderr, "cannot allocate hardware parameter structure (%s)\n", snd_strerror (err));
- exit (1);
- }
- fprintf (stderr,"1 \n");
- if ((err = snd_pcm_hw_params_any (capture_handle, hw_params)) < 0) {
- fprintf (stderr, "cannot initialize hardware parameter structure (%s)\n", snd_strerror (err));
- exit (1);
- }
- fprintf (stderr,"2 \n");
- if ((err = snd_pcm_hw_params_set_access (capture_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
- fprintf (stderr, "cannot set access type (%s)\n", snd_strerror (err));
- exit (1);
- }
- fprintf (stderr,"3 \n");
- if ((err = snd_pcm_hw_params_set_format (capture_handle, hw_params, SND_PCM_FORMAT_S32)) < 0) {
- fprintf (stderr, "cannot set sample format (%s)\n", snd_strerror (err));
- exit (1);
- }
- fprintf (stderr,"4 \n");
- unsigned int rate = 192000;
- if ((err = snd_pcm_hw_params_set_rate_near (capture_handle, hw_params, &rate, 0)) < 0) {
- fprintf (stderr, "cannot set sample rate (%s)\n", snd_strerror (err));
- exit (1);
- }
- fprintf (stderr,"5 \n"); fprintf (stderr,"Rate: %d\n",rate);
- if ((err = snd_pcm_hw_params_set_channels (capture_handle, hw_params, 8)) < 0) {
- fprintf (stderr, "cannot set channel count (%s)\n", snd_strerror (err));
- exit (1);
- }
- fprintf (stderr,"6 \n");
- if ((err = snd_pcm_hw_params (capture_handle, hw_params)) < 0) {
- fprintf (stderr, "cannot set parameters (%s)\n", snd_strerror (err));
- exit (1);
- }
- fprintf (stderr,"7 \n");
- snd_pcm_hw_params_free (hw_params);
- fprintf (stderr,"8 \n");
- if ((err = snd_pcm_prepare (capture_handle)) < 0) {
- fprintf (stderr, "cannot prepare audio interface for use (%s)\n", snd_strerror (err));
- exit (1);
- }
- fprintf (stderr,"9 \n");
- timespec time1_, time2_;
- uint32_t ad7771_butch_size=32*32;
- uint8_t buf[32*1024];
- uint32_t counbter=0;
- for (int i1 = 0; i1 < 10; ++i1) {
- clock_gettime(CLOCK_REALTIME, &time1_);
- std::cout<< "loop ns: " << diff(time2_,time1_).tv_nsec << std::endl;
- clock_gettime(CLOCK_REALTIME, &time2_);
- if ((err = snd_pcm_readi (capture_handle, &buf, ad7771_butch_size)) != ad7771_butch_size) {
- fprintf (stderr, "read from audio interface failed (%s)\n", snd_strerror (err));
- exit (1);
- }
- //counbter+=ad7771_butch_size;
- }
- fprintf (stderr,"10 \n");
- snd_pcm_close (capture_handle);
- exit (0);
- }
- root@BeagleBone:/home/debian# g++ -oalsatut1 alsatut1.cpp -lasound && ./alsatut1
- 1
- 2
- 3
- 4
- 5
- Rate: 192000
- 6
- 7
- 8
- 9
- loop ns: 2113288995
- loop ns: 9338154
- loop ns: 6528263
- loop ns: 7033311
- loop ns: 6846199
- loop ns: 7037977
- loop ns: 6838533
- loop ns: 7022313
- loop ns: 6833200
- loop ns: 6833283
- 10
- root@BeagleBone:/home/debian#
Advertisement
Add Comment
Please, Sign In to add comment