mirror of
https://github.com/moonlight-stream/moonlight-embedded.git
synced 2026-06-15 21:31:12 +00:00
Simplify ALSA backend code
This commit is contained in:
+2
-27
@@ -1,6 +1,3 @@
|
|||||||
/* Use the newer ALSA API */
|
|
||||||
#define ALSA_PCM_NEW_HW_PARAMS_API
|
|
||||||
|
|
||||||
/* All of the ALSA library API is defined
|
/* All of the ALSA library API is defined
|
||||||
* in this header */
|
* in this header */
|
||||||
#include <alsa/asoundlib.h>
|
#include <alsa/asoundlib.h>
|
||||||
@@ -9,42 +6,20 @@ snd_pcm_t *handle;
|
|||||||
|
|
||||||
int nv_alsa_init(unsigned int channelCount, unsigned int sampleRate, unsigned char* device) {
|
int nv_alsa_init(unsigned int channelCount, unsigned int sampleRate, unsigned char* device) {
|
||||||
int rc;
|
int rc;
|
||||||
snd_pcm_hw_params_t *params;
|
|
||||||
int dir;
|
|
||||||
|
|
||||||
/* Open PCM device for playback. */
|
/* Open PCM device for playback. */
|
||||||
if ((rc = snd_pcm_open(&handle, device, SND_PCM_STREAM_PLAYBACK, 0)) != 0)
|
if ((rc = snd_pcm_open(&handle, device, SND_PCM_STREAM_PLAYBACK, 0)) != 0)
|
||||||
return rc;
|
return rc;
|
||||||
|
|
||||||
snd_pcm_hw_params_alloca(¶ms);
|
if ((rc = snd_pcm_set_params(handle, SND_PCM_FORMAT_S16_LE, SND_PCM_ACCESS_RW_INTERLEAVED, channelCount, sampleRate, 1, 50000)) != 0) //50ms latency
|
||||||
snd_pcm_hw_params_any(handle, params);
|
|
||||||
|
|
||||||
if ((rc = snd_pcm_hw_params_set_access(handle, params, SND_PCM_ACCESS_RW_INTERLEAVED)) != 0)
|
|
||||||
return rc;
|
|
||||||
|
|
||||||
if ((rc = snd_pcm_hw_params_set_format(handle, params, SND_PCM_FORMAT_S16_LE)) != 0)
|
|
||||||
return rc;
|
|
||||||
|
|
||||||
if ((rc = snd_pcm_hw_params_set_channels(handle, params, channelCount)) != 0)
|
|
||||||
return rc;
|
|
||||||
|
|
||||||
if ((rc = snd_pcm_hw_params_set_rate_near(handle, params, &sampleRate, &dir)) != 0)
|
|
||||||
return rc;
|
|
||||||
|
|
||||||
snd_pcm_uframes_t frames = 32;
|
|
||||||
if ((rc = snd_pcm_hw_params_set_period_size_near(handle, params, &frames, &dir)) != 0)
|
|
||||||
return rc;
|
|
||||||
|
|
||||||
if ((rc = snd_pcm_hw_params(handle, params)) != 0)
|
|
||||||
return rc;
|
return rc;
|
||||||
}
|
}
|
||||||
|
|
||||||
int nv_alsa_play(const unsigned char* indata, int data_len) {
|
int nv_alsa_play(const unsigned char* indata, int data_len) {
|
||||||
int frames = data_len/4; /* 2 bytes/sample, 2 channels */
|
int frames = data_len/4; /* 2 bytes/sample, 2 channels */
|
||||||
int rc = snd_pcm_writei(handle, indata, frames);
|
int rc = snd_pcm_writei(handle, indata, frames);
|
||||||
if (rc == -EPIPE) {
|
if (rc == -EPIPE)
|
||||||
snd_pcm_prepare(handle);
|
snd_pcm_prepare(handle);
|
||||||
}
|
|
||||||
|
|
||||||
return rc;
|
return rc;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user