Using math.h for sin function and getting errors don't know why

drhunn
../NoteConverter.c: In function ‘main’:
../NoteConverter.c:154:9: warning: variable ‘position’ set but not used [-Wunused-but-set-variable]
Finished building: ../NoteConverter.c

Building target: NoteConverter
Invoking: GCC C Linker
gcc  -o "NoteConverter"  ./NoteConverter.o   
../NoteConverter.c:21: error: undefined reference to 'sin'
collect2: error: ld returned 1 exit status
make: *** [NoteConverter] Error 1

The following is code

/**
 * Frequency octave finder and play note
 */

 #include <stdio.h>
 #include <string.h>
 #include <math.h>

 #define DURATION 5
 #define SAMPLERATE 48000
 #define AMPLITUDE 1

 //A*sin(2*pi*n*f/R)

double yCord(int n, double freq)
{
    double y= sin(2*M_PI*freq*n/48000);
    return y;
}


int xCord(int n)
{
    double x = n/SAMPLERATE;
    return x;
}

// finds the freq from switch
int noteBaseFeq(char *note) {
    int baseNote = 0;
    int baseFreq = 0;

    const char *notes[] = { "C ", "C#", "Db", "D ", "D#", "Eb", "E ", 
        "F ", "F#", "Gb", "G ", "G#", "Ab", "A ", "A#", "Bb", "B " };

    int i;
    for( i = 0; i < 17; i++) {
        if (strcmp(note,notes[i]) == 0) {
            baseNote = i;
            break;
        }
    }

    switch (baseNote+1) {
        case 1: // c
            baseFreq = 26163;
            break;
        case 2: // c#
        case 3: // Db
            baseFreq = 27718;
            break;
        case 4: // D
            baseFreq = 29366;
            break;
        case 5: //D#
        case 6: //Eb
            baseFreq = 31113;
            break;
        case 7: //E
            baseFreq = 32963;
            break;
        case 8: //F
            baseFreq = 34923;
            break;
        case 9: //F#
        case 10: //Gb
            baseFreq = 36999;
            break;
        case 11: //G
            baseFreq = 39200;
            break;
        case 12: //G#
        case 13: //Ab
            baseFreq = 41530;
            break;
        case 14: //A
            baseFreq = 44000;
            break;
        case 15: //A#
        case 16: //Bb
            baseFreq = 46616;
            break;
        case 17: //B
            baseFreq = 49388;
            break;
    }
    return baseFreq;
}

// finds the octive based on A 4
int octaveModifier(int oct)
{
    if (oct == 4) {
        return oct;
    } else if (oct < 4) {
        oct = 4 - oct;
    } else if (oct > 4) {
        oct = oct - 4;
    }

    return oct;
}

// allows for concatenation
void append(char* s, char c)
{
    int len=strlen(s);
    s[len]=c;
    s[len+1]='\0';
}

int main() 
{
    char note;
    char modifier;
    char inputNote[256] = "";
    long long intFreq = 0;
    double freq = 0.0;
    int oct = 0;
    int modOct = 0;

    printf("Please enter an note sharp\\flat(\"b\" for flat) and octave:  \n");
    scanf("%c%c%d", &note, &modifier, &modOct);// gets input from user
    append(inputNote, note);
    append(inputNote, modifier);

    oct = octaveModifier(modOct);
    intFreq = noteBaseFeq(inputNote);

    int i;
    if(modOct > 4) {
        for(i = 0; i < oct; i++) {
            intFreq = intFreq * 2;
            freq = intFreq / 100.0;
        }
    } else if(modOct < 4) {
        for( i = 0; i < oct; i++) {
            intFreq = intFreq / 2;
            freq = intFreq / 100.0;
        }
    } else {
        freq = intFreq / 100.0;
    }

    double time = 0;
    double position = 0;
    for (i = 0; time < DURATION; i++) {
        time = xCord(i);
        position = yCord(i, freq);
    }

    return 0;
}
Jonathan Leffler

You probably need to link with -lm to get the maths library linked. Some platforms require that; others do not.

Fixing the set-but-unused variable is easy; remove its declaration and the line where it is set (or use the result — print it, perhaps?).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Getting null pointer, don't know why

From Dev

Getting exception ClassNotFoundException and don't know why

From Dev

Sin function without using math.h library

From Dev

I keep getting a segmentation fault and I don't know why

From Dev

getting NullPointerException and don't know why. war game

From Dev

I keep getting this error message and I don't know why

From Dev

Don't know why I am getting StopIteration error

From Dev

PHP syntax error in function, don't know why

From Dev

File open prompt Function code not working - don't know why

From Dev

My prime number function is broken but I don't know why

From Dev

SQL Server transactions and transaction isolation - getting errors that I don't know how to fix

From Dev

Trying to compile X Window System and getting errors that I don't know

From Dev

Getting an "ArgumentOutOfRangeException" error but don't know how?

From Dev

Getting an "ArgumentOutOfRangeException" error but don't know how?

From Dev

Using fractional in type definition for function to do math computations-getting errors

From Dev

I'm getting an ArrayIndexOutOfBounds Exception and I don't know why. Can someone shed some light?

From Dev

Getting a stackoverflowexception and don't know why. can anyone help please?

From Dev

I'm getting the error Unexpected symbol 'void' in Unity after copying a code and I don't know why

From Dev

I don't know why I'm getting this error in full join in MySql

From Dev

Why is there no clamp function in math.h

From Dev

It's my first time using android studio and getting error? Don't know what seems to be wrong with it?

From Dev

é instead of "é", definitely don't know why

From Dev

I don't know why this canvas is null

From Dev

resizeBy not working and don't know why

From Dev

Don't know why Polymer is not working

From Dev

Get 404 error and don't know why

From Dev

Sound is gone, I don't know why

From Dev

é instead of "é", definitely don't know why

From Dev

title is indented and i don't know why

Related Related

  1. 1

    Getting null pointer, don't know why

  2. 2

    Getting exception ClassNotFoundException and don't know why

  3. 3

    Sin function without using math.h library

  4. 4

    I keep getting a segmentation fault and I don't know why

  5. 5

    getting NullPointerException and don't know why. war game

  6. 6

    I keep getting this error message and I don't know why

  7. 7

    Don't know why I am getting StopIteration error

  8. 8

    PHP syntax error in function, don't know why

  9. 9

    File open prompt Function code not working - don't know why

  10. 10

    My prime number function is broken but I don't know why

  11. 11

    SQL Server transactions and transaction isolation - getting errors that I don't know how to fix

  12. 12

    Trying to compile X Window System and getting errors that I don't know

  13. 13

    Getting an "ArgumentOutOfRangeException" error but don't know how?

  14. 14

    Getting an "ArgumentOutOfRangeException" error but don't know how?

  15. 15

    Using fractional in type definition for function to do math computations-getting errors

  16. 16

    I'm getting an ArrayIndexOutOfBounds Exception and I don't know why. Can someone shed some light?

  17. 17

    Getting a stackoverflowexception and don't know why. can anyone help please?

  18. 18

    I'm getting the error Unexpected symbol 'void' in Unity after copying a code and I don't know why

  19. 19

    I don't know why I'm getting this error in full join in MySql

  20. 20

    Why is there no clamp function in math.h

  21. 21

    It's my first time using android studio and getting error? Don't know what seems to be wrong with it?

  22. 22

    é instead of "é", definitely don't know why

  23. 23

    I don't know why this canvas is null

  24. 24

    resizeBy not working and don't know why

  25. 25

    Don't know why Polymer is not working

  26. 26

    Get 404 error and don't know why

  27. 27

    Sound is gone, I don't know why

  28. 28

    é instead of "é", definitely don't know why

  29. 29

    title is indented and i don't know why

HotTag

Archive