Introduction

Audio files can be easily played in the c programme with the help of library called “fmode”. the library is a very high level lib and available for Linux,Win,Mac,Arm platforms.Here giving a small example how to play songs with c language.

Installing fmode library

  • Download lib from here and extract.
  • open terminal as root then copy all the .so files to /usr/local/lib from fmodstudioapi10904linux/api/lowlevel/lib/x86_64
    cd fmodstudioapi10904linux/api/lowlevel/lib/
    
    cp x86_64/* /usr/local/lib
  • create a folder fmodex at  /usr/local/include/
     mkdir /usr/local/include/fmodex
  • copy .h files from fmodstudioapi10904linux/api/lowlevel/inc to this folder
    cd fmodstudioapi10904linux/api/lowlevel/
    
    cp inc/* /usr/local/include/fmodex/
  • update libraries
    ldconfig

now the lib is ready to use.

Code snippet

#include <stdio.h>
#include <string.h>
#include <fmodex/fmod.h>

void ERRCHEC(int res,char *where)
{
 if(res!=0)
 {
 printf("error occured at %s\n",where);
 return;
 }
}

int main()
{
 FMOD_SOUND *sound;
 FMOD_SYSTEM *system;
 FMOD_CHANNEL *channel=0;
 FMOD_RESULT result;
 unsigned int version;
 void *extradriverdata = 0;

result = FMOD_System_Create(&system);
 ERRCHEC(result,"sys_create");

result = FMOD_System_GetVersion(system,&version);
 ERRCHEC(result,"get version");

if (version < FMOD_VERSION)
 {
 printf("fatal err\n");
 }

result = FMOD_System_Init(system,32,FMOD_INIT_NORMAL,extradriverdata);
 ERRCHEC(result,"sys_init");

result=FMOD_System_CreateSound(system,"/home/user/001/eclipse_workspace/config/Debug/test.mp3",FMOD_DEFAULT,0,&sound);
 ERRCHEC(result,"create sound");

result = FMOD_Sound_SetMode(sound,FMOD_LOOP_OFF);
 ERRCHEC(result,"setmode");
 printf("Press 'Q' to exit\n");
result=FMOD_System_PlaySound(system,sound,0,false,&channel);
 ERRCHEC(result,"playsound");
 char ch;
 while((ch=getchar())!='Q');
 printf("exiting...\n");
 return 0;
}

use -lfmode if your combiling from terminal.
use fmode in libraries in the project settings in eclipse.
make sure that you have a mp3  called test.mp3 at location specified in the code.
For more reading and library API visit here

 

 

 

 

Advertisements