Could anyone explain the problem im having on line 28,

(value = amp*sin(2*pi*freq*t);)

i keep getting this error message

28  11  [Error] cannot convert 'double' to 'float*' in assignment



#include <cstdlib>
#include <iostream>
#include <math.h>
#include "dp_lib.h"

using namespace std;

int main()
{
    double pi=3.141592653589793238462643383279502884197169399375;
    float* value;
    unsigned long length = 44100; // sample rate
    int freq = 1000;  //frequency
    int t = 1;
    int duration;

    int amp = 1;  // size of the wave

    value = (float* ) allocate_memory(length);

    FILE* fp = fopen("myaudio.wav", "wb"); // open file 


// loop to generate audio samples
    for(int n = 0; n < length; n++) //where n is the step |start of for loop
    {
    int t = n/freq;           //t/freq  
    value = amp*sin(2*pi*freq*t);
    write_wav_file(data, length, fs, fp);
    cout << n << "\t" << value <<"\n"; 
    }     // | end of for loop

    fclose(fp);         // close file
    free(fp);         // release memory

  cout << "\n\n";
  system ("pause");
}

It is illegal to convert from type double to a pointer to type float. You may want to declare value as float value; (rather than float *value).

Do note, however, that double to float is a lossy transformation. Is there any reason that you dont make value a double as well?

[edit]
Since it looks like you want to assign to an array, you would allocate your data (preferably to type double *) and then access individual members of the array as value[n] = amp * sin (2 * pi * freq * t);

Edited 2 Years Ago by L7Sqr

On line 32 you have value = amp*sin(2*pi*freq*t). value is a float * so assigning amp*sin(2*pi*freq*t) to value makes no sense because value is a pointer which is an integer number. Now if you want to store the return into the float that value points to you need to do *value = amp*sin(2*pi*freq*t)

However as well as that problem, which you can solve as suggested above. You will find that value is constant for long period of the loop.

That is because you have done

 int t = n/freq;            
 value[n] = amp*sin(2*pi*freq*t);

n is an integer and freq is an integer, and using integer division, t start as zero for the first 1000 items, then it is 1 for the next 1000 and so on.

Why you divide t by freq and then multiple by freq ? Just do
value[n] = amp*sin(2*pi*n);.

The next problem is that is hightly unlikely to be what you really wanted.. I am guessing that you actually need this:

   value[n]=amp*sin((2.0*pi*n)/freq);

If you are actually trying to round the number to create this effect, then I suggest (a) a comment and (b) stepping through the loop in steps of freq.

This article has been dead for over six months. Start a new discussion instead.