Originally Posted by
xyman
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(¶ms);
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!