X-Git-Url: http://de.git.xonotic.org/?p=xonotic%2Fdarkplaces.git;a=blobdiff_plain;f=snd_alsa.c;h=59c85edfcacb9ac79ae3b6aa21e81c4d99b575fb;hp=96c8b2d1abeee45cb058ca48b16791ea6b967bfc;hb=65f6c04779aa58c69929e0d22967bf152dac17c1;hpb=d1a66b1504773e5da10463fd15d524697b0c2ba2 diff --git a/snd_alsa.c b/snd_alsa.c index 96c8b2d1..59c85edf 100644 --- a/snd_alsa.c +++ b/snd_alsa.c @@ -36,6 +36,7 @@ static snd_pcm_t* pcm_handle = NULL; static snd_pcm_sframes_t expected_delay = 0; static unsigned int alsasoundtime; +static snd_seq_t* seq_handle = NULL; /* ==================== @@ -47,14 +48,64 @@ May return a suggested format if the requested format isn't available */ qboolean SndSys_Init (const snd_format_t* requested, snd_format_t* suggested) { - const char* pcm_name; - int i, err; + const char* pcm_name, *seq_name; + int i, err, seq_client, seq_port; snd_pcm_hw_params_t* hw_params = NULL; snd_pcm_format_t snd_pcm_format; snd_pcm_uframes_t buffer_size; Con_Print ("SndSys_Init: using the ALSA module\n"); + seq_name = NULL; + i = COM_CheckParm ("-sndseqin"); // TODO turn this into a cvar, maybe + if (i != 0 && i < com_argc - 1) + seq_name = com_argv[i + 1]; + if(seq_name) + { + seq_client = atoi(seq_name); + seq_port = 0; + if(strchr(seq_name, ':')) + seq_port = atoi(strchr(seq_name, ':') + 1); + Con_Printf ("SndSys_Init: seq input port has been set to \"%d:%d\". Enabling sequencer input...\n", seq_client, seq_port); + err = snd_seq_open (&seq_handle, "default", SND_SEQ_OPEN_INPUT, 0); + if (err < 0) + { + Con_Print ("SndSys_Init: can't open seq device\n"); + goto seqdone; + } + err = snd_seq_set_client_name(seq_handle, gamename); + if (err < 0) + { + Con_Print ("SndSys_Init: can't set name of seq device\n"); + goto seqerror; + } + err = snd_seq_create_simple_port(seq_handle, gamename, SND_SEQ_PORT_CAP_WRITE | SND_SEQ_PORT_CAP_SUBS_WRITE, SND_SEQ_PORT_TYPE_MIDI_GENERIC | SND_SEQ_PORT_TYPE_APPLICATION); + if(err < 0) + { + Con_Print ("SndSys_Init: can't create seq port\n"); + goto seqerror; + } + err = snd_seq_connect_from(seq_handle, 0, seq_client, seq_port); + if(err < 0) + { + Con_Printf ("SndSys_Init: can't connect to seq port \"%d:%d\"\n", seq_client, seq_port); + goto seqerror; + } + err = snd_seq_nonblock(seq_handle, 1); + if(err < 0) + { + Con_Print ("SndSys_Init: can't make seq nonblocking\n"); + goto seqerror; + } + + goto seqdone; + +seqerror: + snd_seq_close(seq_handle); + seq_handle = NULL; + } + +seqdone: // Check the requested sound format if (requested->width < 1 || requested->width > 2) { @@ -73,16 +124,16 @@ qboolean SndSys_Init (const snd_format_t* requested, snd_format_t* suggested) Con_Printf ("SndSys_Init: suggesting sound width = %hu\n", suggested->width); } - + return false; } - + if (pcm_handle != NULL) { Con_Print ("SndSys_Init: WARNING: Init called before Shutdown!\n"); SndSys_Shutdown (); } - + // Determine the name of the PCM handle we'll use switch (requested->channels) { @@ -105,25 +156,25 @@ qboolean SndSys_Init (const snd_format_t* requested, snd_format_t* suggested) pcm_name = com_argv[i + 1]; // Open the audio device - Con_DPrintf ("SndSys_Init: PCM device is \"%s\"\n", pcm_name); + Con_Printf ("SndSys_Init: PCM device is \"%s\"\n", pcm_name); err = snd_pcm_open (&pcm_handle, pcm_name, SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK); - if (err != 0) + if (err < 0) { Con_Printf ("SndSys_Init: can't open audio device \"%s\" (%s)\n", pcm_name, snd_strerror (err)); return false; } - + // Allocate the hardware parameters err = snd_pcm_hw_params_malloc (&hw_params); - if (err != 0) + if (err < 0) { Con_Printf ("SndSys_Init: can't allocate hardware parameters (%s)\n", snd_strerror (err)); goto init_error; } err = snd_pcm_hw_params_any (pcm_handle, hw_params); - if (err != 0) + if (err < 0) { Con_Printf ("SndSys_Init: can't initialize hardware parameters (%s)\n", snd_strerror (err)); @@ -132,7 +183,7 @@ qboolean SndSys_Init (const snd_format_t* requested, snd_format_t* suggested) // Set the access type err = snd_pcm_hw_params_set_access (pcm_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED); - if (err != 0) + if (err < 0) { Con_Printf ("SndSys_Init: can't set access type (%s)\n", snd_strerror (err)); @@ -145,7 +196,7 @@ qboolean SndSys_Init (const snd_format_t* requested, snd_format_t* suggested) else snd_pcm_format = SND_PCM_FORMAT_S16; err = snd_pcm_hw_params_set_format (pcm_handle, hw_params, snd_pcm_format); - if (err != 0) + if (err < 0) { Con_Printf ("SndSys_Init: can't set sound width to %hu (%s)\n", requested->width, snd_strerror (err)); @@ -154,7 +205,7 @@ qboolean SndSys_Init (const snd_format_t* requested, snd_format_t* suggested) // Set the sound channels err = snd_pcm_hw_params_set_channels (pcm_handle, hw_params, requested->channels); - if (err != 0) + if (err < 0) { Con_Printf ("SndSys_Init: can't set sound channels to %hu (%s)\n", requested->channels, snd_strerror (err)); @@ -163,7 +214,7 @@ qboolean SndSys_Init (const snd_format_t* requested, snd_format_t* suggested) // Set the sound speed err = snd_pcm_hw_params_set_rate (pcm_handle, hw_params, requested->speed, 0); - if (err != 0) + if (err < 0) { Con_Printf ("SndSys_Init: can't set sound speed to %u (%s)\n", requested->speed, snd_strerror (err)); @@ -172,7 +223,7 @@ qboolean SndSys_Init (const snd_format_t* requested, snd_format_t* suggested) buffer_size = requested->speed / 5; err = snd_pcm_hw_params_set_buffer_size_near (pcm_handle, hw_params, &buffer_size); - if (err != 0) + if (err < 0) { Con_Printf ("SndSys_Init: can't set sound buffer size to %lu (%s)\n", buffer_size, snd_strerror (err)); @@ -181,7 +232,7 @@ qboolean SndSys_Init (const snd_format_t* requested, snd_format_t* suggested) buffer_size /= NB_PERIODS; err = snd_pcm_hw_params_set_period_size_near(pcm_handle, hw_params, &buffer_size, 0); - if (err != 0) + if (err < 0) { Con_Printf ("SndSys_Init: can't set sound period size to %lu (%s)\n", buffer_size, snd_strerror (err)); @@ -189,7 +240,7 @@ qboolean SndSys_Init (const snd_format_t* requested, snd_format_t* suggested) } err = snd_pcm_hw_params (pcm_handle, hw_params); - if (err != 0) + if (err < 0) { Con_Printf ("SndSys_Init: can't set hardware parameters (%s)\n", snd_strerror (err)); @@ -203,16 +254,19 @@ qboolean SndSys_Init (const snd_format_t* requested, snd_format_t* suggested) alsasoundtime = 0; if (snd_channellayout.integer == SND_CHANNELLAYOUT_AUTO) Cvar_SetValueQuick (&snd_channellayout, SND_CHANNELLAYOUT_ALSA); - + return true; // It's not very clean, but it avoids a lot of duplicated code. init_error: - + if (hw_params != NULL) snd_pcm_hw_params_free (hw_params); - SndSys_Shutdown (); + + snd_pcm_close(pcm_handle); + pcm_handle = NULL; + return false; } @@ -226,6 +280,12 @@ Stop the sound card, delete "snd_renderbuffer" and free its other resources */ void SndSys_Shutdown (void) { + if (seq_handle != NULL) + { + snd_seq_close(seq_handle); + seq_handle = NULL; + } + if (pcm_handle != NULL) { snd_pcm_close(pcm_handle); @@ -255,11 +315,11 @@ static qboolean SndSys_Recover (int err_num) // We can only do something on underrun ("broken pipe") errors if (err_num != -EPIPE) return false; - + err = snd_pcm_prepare (pcm_handle); - if (err != 0) + if (err < 0) { - Con_DPrintf ("SndSys_Recover: unable to recover (%s)\n", + Con_Printf ("SndSys_Recover: unable to recover (%s)\n", snd_strerror (err)); // TOCHECK: should we stop the playback ? @@ -300,7 +360,7 @@ static snd_pcm_sframes_t SndSys_Write (const unsigned char* buffer, unsigned int snd_renderbuffer->startframe += written; expected_delay += written; } - + return written; } @@ -317,7 +377,7 @@ void SndSys_Submit (void) unsigned int startoffset, factor; snd_pcm_uframes_t limit, nbframes; snd_pcm_sframes_t written; - + if (pcm_handle == NULL || snd_renderbuffer->startframe == snd_renderbuffer->endframe) return; @@ -332,7 +392,7 @@ void SndSys_Submit (void) written = SndSys_Write (&snd_renderbuffer->ring[startoffset * factor], limit); if (written < 0 || (snd_pcm_uframes_t)written != limit) return; - + nbframes -= limit; startoffset = 0; } @@ -359,7 +419,7 @@ unsigned int SndSys_GetSoundTime (void) return 0; err = snd_pcm_delay (pcm_handle, &delay); - if (err != 0) + if (err < 0) { if (developer.integer >= 1000 && vid_activewindow) Con_DPrintf ("SndSys_GetSoundTime: can't get playback delay (%s)\n", @@ -369,7 +429,7 @@ unsigned int SndSys_GetSoundTime (void) return 0; err = snd_pcm_delay (pcm_handle, &delay); - if (err != 0) + if (err < 0) { Con_DPrintf ("SndSys_GetSoundTime: can't get playback delay, again (%s)\n", snd_strerror (err)); @@ -386,9 +446,9 @@ unsigned int SndSys_GetSoundTime (void) else timediff = expected_delay - delay; expected_delay = delay; - + alsasoundtime += (unsigned int)timediff; - + return alsasoundtime; } @@ -418,3 +478,37 @@ void SndSys_UnlockRenderBuffer (void) { // Nothing to do } + +/* +==================== +SndSys_SendKeyEvents + +Send keyboard events originating from the sound system (e.g. MIDI) +==================== +*/ +void SndSys_SendKeyEvents(void) +{ + snd_seq_event_t *event; + if(!seq_handle) + return; + for(;;) + { + if(snd_seq_event_input(seq_handle, &event) <= 0) + break; + if(event) + { + switch(event->type) + { + case SND_SEQ_EVENT_NOTEON: + if(event->data.note.velocity) + { + Key_Event(K_MIDINOTE0 + event->data.note.note, 0, true); + break; + } + case SND_SEQ_EVENT_NOTEOFF: + Key_Event(K_MIDINOTE0 + event->data.note.note, 0, false); + break; + } + } + } +}