clinuxaudioportaudio

How do I include libsdn and portaudio in a C project?


The project

The "project" im currently working on is a tui wav player. I'm in the first stage where i want to read and play a wav song/sound.

Please consider that I started using C in 4 weeks ago, so I am a total newbie.

I' m not using any IDE, and cc to compile (cc main.c -o main -lm)

Things I did

I dowloaded portaudio via yay/pacman, I downloaded libsnd source from here, installed with the help of the documentation (./configure && make && make install).

The libsnd sources are in the same folder as my main.c code.

Output of ls -a

 .    c-econio  'Goran Bregović - Kalashnikov.mp3'   kalasnikow.wav      main.c       test.c
 ..   .git      'Goran Bregović - Kalashnikov.wav'   libsndfile-1.0.28   random.mp3

c-econio and libsndfile-1.0.28 are dir's (library's) that i want to use (econio will be used later on). As previously mentioned portaudio is installed via package manager.

I have no idea if #inlude <portauido> is working, cause i dont know how to test it without libsnd.

The main question

How do i include portaudio and libsnd into this code and how do i test it? Do i have to add something to the compiler (cc)? Im using linux.

The code

main.c

#include <dirent.h>
#include <stdio.h>
#include <string.h>
#include <portaudio.h>

int listwav() {
    int i = 1;
     DIR *d;
    struct dirent *entry;
    d = opendir("."); // Megnyitom az éppenleges DIR-t.
    if (d) {
        while ((entry = readdir(d)) != NULL) {  // Ameddig van file + mp3 kiiratás. 
            if (strlen(entry->d_name) > 4 && strcmp(entry->d_name + strlen(entry->d_name) - 4, ".wav") == 0) {
              printf("\t%d %s\n",i, entry->d_name);
              i++; 
            }
        }
        closedir(d);
    }
    return (0);
}

void play(){ // play audio "ffplay -v 0 -nodisp -autoexit"
  printf("What to play? \n");
  listwav();
  // system("playerctl play");
}

void stop(){
  printf("Stopped\n");
  // system("playerctl stop");
}

int chose(){
  int chosen = 0;    
  
  printf("Choose what to do:\n 1.Play\n 2.Stop\n 3.Exit\n Chosen: "); scanf("%d", &chosen);
  while (chosen != 3){
    switch (chosen) {
      case 1: play(); break;
      case 2: stop(); break;
    }
    return 0;
  }
}

 /// -----
 int main(){
   chose();

 }

EDIT: Unupdated version of the folder im using: https://github.com/Lolis4TheWin/mpc3


Solution

  • So I finally figured out!

    Download and make the librabry

    1. Download the source
    2. Read the installation manual inside the source file
    3. Do what it says (./configure; make ; make install)
    4. You can delete the source, the library is in your /usr/local/lib dir.

    Compile the code

    A) option

    gcc -lsndfile main.c -o main -lm && ./main is what i had to use when compiling.

    After -l the library name comes. -o stands for output file and -lm stands for some library to that i had to include cause of linux. (I think, not based information.)

    And I had to include the librars like this:

    #include <sndfile>
    #include <portaudio>
    

    B) option

    Is to link the library with path inside ""

    #include "usr/local/lib/libsndfila.a"