C Board  

Go Back   C Board > Platform Specific Boards > Linux Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 08-05-2009, 10:45 AM   #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.
Fujitaka is offline   Reply With Quote
Old 08-05-2009, 12:08 PM   #2
Jaxom's & Imriel's Dad
 
Kennedy's Avatar
 
Join Date: Aug 2006
Location: Alabama
Posts: 877
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 ...".
Kennedy is offline   Reply With Quote
Old 08-05-2009, 12:34 PM   #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.
Fujitaka is offline   Reply With Quote
Old 08-05-2009, 05:06 PM   #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.
Fujitaka is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How can I compile C and C++ in Linux operating system? Please help. MarkSquall C Programming 8 03-10-2008 12:08 PM
installing linux for the first time Micko Tech Board 9 12-06-2004 05:15 AM
Linux: starting a prog autamatically w/KDE MathFan Tech Board 5 06-19-2004 05:59 PM
Linux for Windows! Strut Linux Programming 2 12-25-2002 11:36 AM
Linux? Windows Xp? VooDoo Linux Programming 15 07-31-2002 08:18 AM


All times are GMT -6. The time now is 07:40 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22