Jump to content

conversion from c to c++

- - - - -

  • Please log in to reply
3 replies to this topic

#1
ogopa

ogopa

    Learning Programmer

  • Members
  • PipPipPip
  • 32 posts
Hey guys, I'm currently trying to convert a code from c to c++. I have corrected a few things, but I am now stuck. I tried compiling and I am getting errors. Btw I am using g++ compiler in terminal in linux.
the errors I am getting are:
audio_backup.cpp:13:24: warning: deprecated conversion from string constant to ‘char*’
audio_backup.cpp: In function ‘int generate_sine(const snd_pcm_channel_area_t*, snd_pcm_uframes_t, int, double*)’:
audio_backup.cpp:51:76: error: invalid operands of types ‘unsigned int’ and ‘const char [2]’ to binary ‘operator<<’

I'm not sure what this means. Can any one help me please?
line 13:
 static char *device = "plughw:0,0";                     /* playback device */

line 51:
cout<<"areas[%i].first == %i, aborting...", chn <<","<< areas[chn].first;



#include <stdio.h>

 #include <stdlib.h>

 #include <string.h>

 #include <sched.h>

 #include <errno.h>

 #include <getopt.h>

 #include <alsa/asoundlib.h>

 #include <sys/time.h>

 #include <cmath>

#include <iostream>

using namespace std;


 static char *device = "plughw:0,0";                     /* playback device */

 static snd_pcm_format_t format = SND_PCM_FORMAT_S16;    /* sample format */

 static unsigned int rate = 96000;                       /* stream rate */

 static unsigned int channels = 128;                       /* count of channels */

 static unsigned int buffer_time = 500000;               /* ring buffer length in us */

 static unsigned int period_time = 100000;               /* period time in us */

 static double freq = 440;                               /* sinusoidal wave frequency in Hz */

 static int verbose = 0;                                 /* verbose flag */

 static int resample = 1;                                /* enable alsa-lib resampling */

 static int period_event = 0; 				/* produce poll event after each period */

 static snd_pcm_sframes_t buffer_size;

 static snd_pcm_sframes_t period_size;

 static snd_output_t *output = NULL;

 static float amplitude = 1;

 static int generate_sine(const snd_pcm_channel_area_t *areas, 

                           snd_pcm_uframes_t offset,

                           int count, double *_phase)

 {

         static double max_phase = 2. * M_PI;

         double phase = *_phase;

         double step = max_phase*freq/(double)rate;

         unsigned char *samples[channels];

         int steps[channels];

         unsigned int chn;

         int format_bits = snd_pcm_format_width(format);

         unsigned int maxval = (1 << (format_bits - 1)) - 1;

         int bps = format_bits / 8;  /* bytes per sample */

         int phys_bps = snd_pcm_format_physical_width(format) / 8;

         int big_endian = snd_pcm_format_big_endian(format) == 1;

         int to_unsigned = snd_pcm_format_unsigned(format) == 1;

         int is_float = (format == SND_PCM_FORMAT_FLOAT_LE ||

                         format == SND_PCM_FORMAT_FLOAT_BE);

	 float amplitude_scale = amplitude/8.56;

	 

	

	 /* verify and prepare the contents of areas */

         for (chn = 0; chn < channels; chn++) {

                 if ((areas[chn].first % 8) != 0) {

                         cout<<"areas[%i].first == %i, aborting...", chn <<","<< areas[chn].first;

                         exit(EXIT_FAILURE);

                 }

                 samples[chn] = /*(signed short *)*/(((unsigned char *)areas[chn].addr) + (areas[chn].first / 8));

                 if ((areas[chn].step % 16) != 0) {

                         cout<<"areas[%i].step == %i, aborting...  "<<","<< chn areas[chn].step;

                         exit(EXIT_FAILURE);

                 }

                 steps[chn] = areas[chn].step / 8;

                 samples[chn] += offset * steps[chn];

         }

         /* fill the channel areas */

         while (count-- > 0) {

                 union {

                         float f;

                         int i;

                 } fval;

                 int res, i;

                 if (is_float) {

                         fval.f = amplitude_scale * sin(phase) * maxval;

                         res = fval.i;

                 } else

                         res = amplitude_scale * sin(phase) * maxval;

                 if (to_unsigned)

                         res ^= 1U << (format_bits - 1);

                 for (chn = 0; chn < channels; chn++) {

                         /* Generate data in native endian format */

                         if (big_endian) {

                                 for (i = 0; i < bps; i++)

                                         *(samples[chn] + phys_bps - 1 - i) = (res >> i * 8) & 0xff;

                         } else {

                                 for (i = 0; i < bps; i++)

                                         *(samples[chn] + i) = (res >>  i * 8) & 0xff;

                         }

                         samples[chn] += steps[chn];

                 }

                 phase += step;

                 if (phase >= max_phase)

                         phase -= max_phase;

         }

         *_phase = phase;

 }

 

 static int set_hwparams(snd_pcm_t *handle,

                         snd_pcm_hw_params_t *params,

                         snd_pcm_access_t access)

 {

         unsigned int rrate;

         snd_pcm_uframes_t size;

         int err, dir;

 

         /* choose all parameters */

         err = snd_pcm_hw_params_any(handle, params);

         if (err < 0) {

                 cout<<"Broken configuration for playback: no configurations available: %s  "<<","<< snd_strerror(err);

                 return err;

         }

         /* set hardware resampling */

         err = snd_pcm_hw_params_set_rate_resample(handle, params, resample);

         if (err < 0) {

                 cout<<"Resampling setup failed for playback: %s  "<<","<< snd_strerror(err);

                 return err;

         }

         /* set the interleaved read/write format */

         err = snd_pcm_hw_params_set_access(handle, params, access);

         if (err < 0) {

                 cout<<"Access type not available for playback: %s  "<<","<< snd_strerror(err);

                 return err;

         }

         /* set the sample format */

         err = snd_pcm_hw_params_set_format(handle, params, format);

         if (err < 0) {

                 cout<<"Sample format not available for playback: %s  "<<","<< snd_strerror(err);

                 return err;

         }

         /* set the count of channels */

         err = snd_pcm_hw_params_set_channels(handle, params, channels);

         if (err < 0) {

                 cout<<"Channels count (%i) not available for playbacks: %s  ", channels<<","<< snd_strerror(err);

                 return err;

         }

         /* set the stream rate */

         rrate = rate;

         err = snd_pcm_hw_params_set_rate_near(handle, params, &rrate, 0);

         if (err < 0) {

                 cout<<"Rate %iHz not available for playback: %s  ", rate<<","<< snd_strerror(err);

                 return err;

         }

         if (rrate != rate) {

                 cout<<"Rate doesn't match (requested %iHz, get %iHz)  "<<","<< rate<<","<< err;

                 return -EINVAL;

         }

         /* set the buffer time */

         err = snd_pcm_hw_params_set_buffer_time_near(handle, params, &buffer_time, &dir);

         if (err < 0) {

                 cout<<"Unable to set buffer time %i for playback: %s  ", buffer_time<<","<< snd_strerror(err);

                 return err;

         }

         err = snd_pcm_hw_params_get_buffer_size(params, &size);

         if (err < 0) {

                 cout<<"Unable to get buffer size for playback: %s  "<<","<< snd_strerror(err);

                 return err;

         }

         buffer_size = size;

         /* set the period time */

         err = snd_pcm_hw_params_set_period_time_near(handle, params, &period_time, &dir);

         if (err < 0) {

                 cout<<"Unable to set period time %i for playback: %s  "<<","<< period_time, snd_strerror(err);

                 return err;

         }

         err = snd_pcm_hw_params_get_period_size(params, &size, &dir);

         if (err < 0) {

                 cout<<"Unable to get period size for playback: %s  "<<","<< snd_strerror(err);

                 return err;

         }

         period_size = size;

         /* write the parameters to device */

         err = snd_pcm_hw_params(handle, params);

         if (err < 0) {

                 cout<<"Unable to set hw params for playback: %s  "<<","<< snd_strerror(err);

                 return err;

         }

         return 0;

 }

Edited by ogopa, 26 July 2011 - 06:00 AM.


#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
What's the code? Without seeing it, it will be very hard for anyone to help you.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
ogopa

ogopa

    Learning Programmer

  • Members
  • PipPipPip
  • 32 posts
Just edited the post and in cluded the code:)

#4
ogopa

ogopa

    Learning Programmer

  • Members
  • PipPipPip
  • 32 posts
I fixed these errors now actually. thanks for the interest




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users