Thread: Starting Linux Audio Programming OSS

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    10

    Starting Linux Audio Programming OSS

    I want to start a small project so I can learn off of it atm. A small linux audio player that will play flac files. Maby you people can point me to some books, resources that can get me started on Linux Audio Programming in C.

    I looked at the libflac C api. Compiled their example code. I looked at OSS's C api some. Just confused on how the process works.

    For example. Do I use OSS to open a device. Then somehow tell oss to use the libflac to read in the flac audio file. Then feed it to the device through OSS so I can hear it though my headset. Confused atm. Thanks.

  2. #2
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Bad idea. OSS is legacy and is marked for deletion. What you want to use is ALSA. The ALSA lib is available for download (but it is a beast -- and they REQUIRE you to use their lib to access their drivers -- I'm working on getting around that, though). It does, however, do what you want it to do.

    BTW, for what it is worth, ALSA used to stand for "Another Linux Sound Architecture", however, they changed the name of if a few years back to "Advanced ...".

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    10
    Ah. So my first step is to not use OSS, and go with ALSA. I might even look into SDL Audio as well. Since I have programmed with SDL before.

    I will go take a look at the ALSA libs then. Hopefully I won't get to confused at that.
    Last edited by Fujitaka; 08-05-2009 at 12:37 PM.

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    10
    Ok. I managed to compile the example program on ALSA. Been studying it for awhile. Looking at the ALSA API too.

    Code:
    #include <stdio.h>
    	#include <stdlib.h>
    	#include <alsa/asoundlib.h>
    	      
    	main (int argc, char *argv[])
    	{
    		int i;
    		int err;
    		short buf[128];
    		snd_pcm_t *playback_handle;
    		snd_pcm_hw_params_t *hw_params;
    	
    		if ((err = snd_pcm_open (&playback_handle, argv[1], SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
    			fprintf (stderr, "cannot open audio device %s (%s)\n", 
    				 argv[1],
    				 snd_strerror (err));
    			exit (1);
    		}
    		   
    		if ((err = snd_pcm_hw_params_malloc (&hw_params)) < 0) {
    			fprintf (stderr, "cannot allocate hardware parameter structure (%s)\n",
    				 snd_strerror (err));
    			exit (1);
    		}
    				 
    		if ((err = snd_pcm_hw_params_any (playback_handle, hw_params)) < 0) {
    			fprintf (stderr, "cannot initialize hardware parameter structure (%s)\n",
    				 snd_strerror (err));
    			exit (1);
    		}
    	
    		if ((err = snd_pcm_hw_params_set_access (playback_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
    			fprintf (stderr, "cannot set access type (%s)\n",
    				 snd_strerror (err));
    			exit (1);
    		}
    	
    		if ((err = snd_pcm_hw_params_set_format (playback_handle, hw_params, SND_PCM_FORMAT_S16_LE)) < 0) {
    			fprintf (stderr, "cannot set sample format (%s)\n",
    				 snd_strerror (err));
    			exit (1);
    		}
    	
    		if ((err = snd_pcm_hw_params_set_rate_resample (playback_handle, hw_params, 44100, 0)) < 0) {
    			fprintf (stderr, "cannot set sample rate (%s)\n",
    				 snd_strerror (err));
    			exit (1);
    		}
    	
    		if ((err = snd_pcm_hw_params_set_channels (playback_handle, hw_params, 2)) < 0) {
    			fprintf (stderr, "cannot set channel count (%s)\n",
    				 snd_strerror (err));
    			exit (1);
    		}
    	
    		if ((err = snd_pcm_hw_params (playback_handle, hw_params)) < 0) {
    			fprintf (stderr, "cannot set parameters (%s)\n",
    				 snd_strerror (err));
    			exit (1);
    		}
    	
    		snd_pcm_hw_params_free (hw_params);
    	
    		if ((err = snd_pcm_prepare (playback_handle)) < 0) {
    			fprintf (stderr, "cannot prepare audio interface for use (%s)\n",
    				 snd_strerror (err));
    			exit (1);
    		}
    	
    		for (i = 0; i < 10; ++i) {
    			if ((err = snd_pcm_writei (playback_handle, buf, 128)) != 128) {
    				fprintf (stderr, "write to audio interface failed (%s)\n",
    					 snd_strerror (err));
    				exit (1);
    			}
    		}
    	
    		snd_pcm_close (playback_handle);
    		exit (0);
    	}
    So far I kinda understand what is going on here. Maby someone here can help me understand this next part. How exactly do I feed a decoded FLAC stream to ALSA. I take it when I want to write the decoded streams to ALSA I hae to use snd_pcm_writei function. Not sure.

    I studied the FLAC C decode example code, and api for a few days.

    SourceForge.net Repository - [flac] View of /flac/examples/c/decode/file/main.c

    but still not sure how ALSA and libflac can be linked together to make it work.

    Been searching on google for some help, but not finding much on programming ALSA.
    Last edited by Fujitaka; 08-05-2009 at 05:11 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 03-10-2008, 12:08 PM
  2. installing linux for the first time
    By Micko in forum Tech Board
    Replies: 9
    Last Post: 12-06-2004, 05:15 AM
  3. Linux: starting a prog autamatically w/KDE
    By MathFan in forum Tech Board
    Replies: 5
    Last Post: 06-19-2004, 05:59 PM
  4. Linux for Windows!
    By Strut in forum Linux Programming
    Replies: 2
    Last Post: 12-25-2002, 11:36 AM
  5. Linux? Windows Xp?
    By VooDoo in forum Linux Programming
    Replies: 15
    Last Post: 07-31-2002, 08:18 AM