Thread: Simple C library for playing .wav files

  1. #1
    Registered User
    Join Date
    Jul 2011
    Location
    Croatia
    Posts
    16

    Simple C library for playing .wav files

    Hi, can anyone suggest me simple c library for playing .wav files. I am coding on Ubuntu-linux using KDevelop. I would also need a link or help how to implement that library in KDevelop.I found something: RBPlayer but i don't know which library this header #include <rb-player.h> belongs.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    I'm not 100% sure on what your question is. Are you asking what library you need to link with? It's a total guess, but try librb (pass -lrb to gcc). Also, you can just look in your /usr/lib directory for libr* and see what comes up. It may be librbplayer or something. You might be better off checking on a Rhythmbox specific forum for this. Also, you can download the source and see if you can tell what libraries it builds and installs, since that will probably be the ones you will need to link against. Sorry I can't be of more help.

  3. #3
    Registered User
    Join Date
    Jul 2011
    Location
    Croatia
    Posts
    16
    I found this lib in /usr/lib directory librhythmbox-core.so.0.0.0
    I google that lib and found Ubuntu -- Filelist of package rhythmbox/hardy/i386 but i don't know how that can help me.

  4. #4
    Registered User
    Join Date
    Jul 2011
    Location
    Croatia
    Posts
    16
    I could use some other library for playing audio .wav files, but i am not so skillfull in searching for wright one.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    So rhythmbox is a media organizer and player, a bit like iTunes, that runs on X Windows. That means your computer will have to be running that application for your program to control it. I sounds like librhythmbox-core is a good library to try linking against. It looks like that link just shows a list of all the files that are installed when you install the rhythmbox package, including the library you found. I don't know much about media or audio programming, so I'm can't recommend too much. I searched for "linux libraries for playing .wav", and got a number of hits, including this stack overflow article that seems to have some decent suggestions.

  6. #6
    Registered User
    Join Date
    Jul 2011
    Location
    Croatia
    Posts
    16
    Quote Originally Posted by anduril462 View Post
    So rhythmbox is a media organizer and player, a bit like iTunes, that runs on X Windows. That means your computer will have to be running that application for your program to control it. I sounds like librhythmbox-core is a good library to try linking against. It looks like that link just shows a list of all the files that are installed when you install the rhythmbox package, including the library you found. I don't know much about media or audio programming, so I'm can't recommend too much. I searched for "linux libraries for playing .wav", and got a number of hits, including this stack overflow article that seems to have some decent suggestions.
    Thank you, your link is very good. I found the answers i need.

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    A .wav file is actually just raw PCM data with a header on it. I've used alsa-lib* for sound effects in a game before, via .wav files. It is pretty simple to use and works consistently. Also: it is used by default in 99% of linux PC installs, so is widely available and (presumably...) well maintained.

    * that page isn't loading for me right now tho according to Google it should still be current.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    Registered User
    Join Date
    Jul 2011
    Location
    Croatia
    Posts
    16
    Quote Originally Posted by MK27 View Post
    A .wav file is actually just raw PCM data with a header on it. I've used alsa-lib* for sound effects in a game before, via .wav files. It is pretty simple to use and works consistently. Also: it is used by default in 99% of linux PC installs, so is widely available and (presumably...) well maintained.

    * that page isn't loading for me right now tho according to Google it should still be current.
    The link is still down. I also checked your links in signature - lot's of good resources.

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by xyman View Post
    The link is still down.
    Today as well. There must be some serious server problem there, since that is the site linked from their wikipedia page, which notes that alsa has been in development for more than a decade and the last stable release was 5 months ago. If they had changed the domain, they would probably be smart enough to update the wikipedia page.

    The alsa-devel package available from your linux distro (which doesn't require the alsa site to be up) should contain some "doxygen" style html docs, including some examples (they install into /usr/share/doc/alsa-lib-devel/doxygen/html or something close to that). I'll be honest and say I would not want to try to learn from scratch with just that tho, and I don't see much in the way of 3rd party tutorials online (whereas I did learn from their own site).

    If it is any help, here's the sound module from that game. There were three sound effects, "thunk", "pip", and "bounce", which are loaded into memory from .wav files in the init_alsa() function by calling the load_pcm() function. Nb. there are some globals referenced that are not declared in this file, those begin with Capitals (but they are not very relevant anyway). The sounds were played via play_snd(), which is a pthread function so the game wouldn't pause to make noise (but again, that's not so relevant to understanding the alsa part).

    Code:
    /* sound.c */
    #include <pthread.h>
    #include <stdio.h>
    #include <errno.h>
    #include <alsa/asoundlib.h>
    
    int init_alsa() {
    	unsigned int rate = 16000;
    	int err;
    	snd_pcm_hw_params_t *params;
    
    	if ((err=snd_pcm_open(&PCM,"plughw:0,0",SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
    		fprintf(stderr, "Can't use sound: %s\n", snd_strerror(err));
    		return err;
    	}
    
    	snd_pcm_hw_params_alloca(&params);
    	snd_pcm_hw_params_any(PCM, params);
    	snd_pcm_hw_params_set_access(PCM, params, SND_PCM_ACCESS_RW_INTERLEAVED);
    	snd_pcm_hw_params_set_format(PCM, params, SND_PCM_FORMAT_S16_LE);
    	snd_pcm_hw_params_set_channels(PCM, params, 1);
    	snd_pcm_hw_params_set_rate_near(PCM, params, &rate, NULL);
    
    	if ((err = snd_pcm_hw_params(PCM, params)) < 0) {
    		fprintf(stderr, "Can't set parameters for sound: %s\n", snd_strerror(err));
    		return err;
    	}
    
    	Sfx[0] = load_pcm("sounds/thunk.raw", &Sfx_L[0]);
    	Sfx[1] = load_pcm("sounds/pip.raw", &Sfx_L[1]);
    	Sfx[2] = load_pcm("sounds/bounce.raw", &Sfx_L[2]);
    
    	return 1;
    }
    
    
    char *load_pcm (char *file, int *len) {
    	size_t rv;
    	struct stat info;
    	FILE *fp = fopen(file,"rb");
    	char *data;
    
    	if (!fp) {
    		fprintf(stderr, "load_pcm() -- Couldn't open %s: ", file);
    		perror("fopen() ");
    		return NULL;
    	}
    
    	stat(file,&info);
    	data = malloc(info.st_size);
    	if ((rv=fread(data,1,info.st_size,fp)) != info.st_size) 
    		fprintf(stderr, "load_pcm -- Read short on %s: %d/%d\n",file,(int)rv,(int)info.st_size);
    	fclose(fp);
    	*len = info.st_size;
    	return data;
    }
    
    
    void *play_snd(void *p) {
    	int rem, i, n = (long)p;
    	char *dp = Sfx[n];
    
    	if (!Config.sound) pthread_exit(0);
    
    	pthread_mutex_lock(&SoundLock);
    	for (i=0;i<Sfx_L[n];i+=256) {
    		rem = (Sfx_L[n]-i)/2;
    		if (rem>128) rem = 128;
    		if (snd_pcm_writei(PCM,dp,128) ==-EPIPE) snd_pcm_prepare(PCM);
    		dp+=256;
    	}
    	pthread_mutex_unlock(&SoundLock);
    
    	pthread_exit(0);
    }
    All the function calls are either standard C library or alsa (except for some pthreads in play_snd()). I was just looking at the html doxygen docs; they have a search bar and all of the alsa functions and constants are in it, altho the docs are typically "doxygen minimal", in other words, pretty sketchy. But you should be able to do what you want I hope -- good luck!
    Last edited by MK27; 07-08-2011 at 09:58 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  10. #10
    Registered User
    Join Date
    Jul 2011
    Location
    Croatia
    Posts
    16
    Thanks, I'll give it a try.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple library for playing .wav files
    By xyman in forum C Programming
    Replies: 1
    Last Post: 07-06-2011, 08:04 AM
  2. Playing MP3 files in C++
    By peyman_k in forum C++ Programming
    Replies: 6
    Last Post: 07-29-2008, 05:25 PM
  3. Playing wma files
    By maxorator in forum C++ Programming
    Replies: 4
    Last Post: 10-02-2005, 02:23 AM
  4. playing .mmf files
    By Ruski in forum Windows Programming
    Replies: 2
    Last Post: 06-17-2005, 09:49 PM
  5. Playing .wav files
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 05-17-2002, 09:53 PM