some debug code improvements
[pcsx_rearmed.git] / plugins / dfsound / alsa.c
... / ...
CommitLineData
1/***************************************************************************
2 alsa.c - description
3 -------------------
4 begin : Sat Mar 01 2003
5 copyright : (C) 2002 by Pete Bernert
6 email : BlackDove@addcom.de
7 ***************************************************************************/
8/***************************************************************************
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. See also the license.txt file for *
14 * additional informations. *
15 * *
16 ***************************************************************************/
17
18#include "stdafx.h"
19
20#define _IN_OSS
21
22#include "externals.h"
23
24#define ALSA_PCM_NEW_HW_PARAMS_API
25#define ALSA_PCM_NEW_SW_PARAMS_API
26#include <alsa/asoundlib.h>
27
28static snd_pcm_t *handle = NULL;
29static snd_pcm_uframes_t buffer_size;
30
31// SETUP SOUND
32void SetupSound(void)
33{
34 snd_pcm_hw_params_t *hwparams;
35 snd_pcm_status_t *status;
36 unsigned int pspeed;
37 int pchannels;
38 int format;
39 unsigned int buffer_time = 100000;
40 unsigned int period_time = buffer_time / 4;
41 int err;
42
43 if (iDisStereo) pchannels = 1;
44 else pchannels=2;
45
46 pspeed = 44100;
47 format = SND_PCM_FORMAT_S16;
48
49 if ((err = snd_pcm_open(&handle, "default",
50 SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK)) < 0)
51 {
52 printf("Audio open error: %s\n", snd_strerror(err));
53 return;
54 }
55
56 if((err = snd_pcm_nonblock(handle, 0))<0)
57 {
58 printf("Can't set blocking moded: %s\n", snd_strerror(err));
59 return;
60 }
61
62 snd_pcm_hw_params_alloca(&hwparams);
63
64 if((err=snd_pcm_hw_params_any(handle, hwparams))<0)
65 {
66 printf("Broken configuration for this PCM: %s\n", snd_strerror(err));
67 return;
68 }
69
70 if((err=snd_pcm_hw_params_set_access(handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED))<0)
71 {
72 printf("Access type not available: %s\n", snd_strerror(err));
73 return;
74 }
75
76 if((err=snd_pcm_hw_params_set_format(handle, hwparams, format))<0)
77 {
78 printf("Sample format not available: %s\n", snd_strerror(err));
79 return;
80 }
81
82 if((err=snd_pcm_hw_params_set_channels(handle, hwparams, pchannels))<0)
83 {
84 printf("Channels count not available: %s\n", snd_strerror(err));
85 return;
86 }
87
88 if((err=snd_pcm_hw_params_set_rate_near(handle, hwparams, &pspeed, 0))<0)
89 {
90 printf("Rate not available: %s\n", snd_strerror(err));
91 return;
92 }
93
94 if((err=snd_pcm_hw_params_set_buffer_time_near(handle, hwparams, &buffer_time, 0))<0)
95 {
96 printf("Buffer time error: %s\n", snd_strerror(err));
97 return;
98 }
99
100 if((err=snd_pcm_hw_params_set_period_time_near(handle, hwparams, &period_time, 0))<0)
101 {
102 printf("Period time error: %s\n", snd_strerror(err));
103 return;
104 }
105
106 if((err=snd_pcm_hw_params(handle, hwparams))<0)
107 {
108 printf("Unable to install hw params: %s\n", snd_strerror(err));
109 return;
110 }
111
112 snd_pcm_status_alloca(&status);
113 if((err=snd_pcm_status(handle, status))<0)
114 {
115 printf("Unable to get status: %s\n", snd_strerror(err));
116 return;
117 }
118
119 buffer_size = snd_pcm_status_get_avail(status);
120}
121
122// REMOVE SOUND
123void RemoveSound(void)
124{
125 if(handle != NULL)
126 {
127 snd_pcm_drop(handle);
128 snd_pcm_close(handle);
129 handle = NULL;
130 }
131}
132
133// GET BYTES BUFFERED
134unsigned long SoundGetBytesBuffered(void)
135{
136 unsigned long l;
137
138 if (handle == NULL) // failed to open?
139 return SOUNDSIZE;
140 l = snd_pcm_avail(handle);
141 if (l < 0) return 0;
142 if (l < buffer_size / 2) // can we write in at least the half of fragments?
143 l = SOUNDSIZE; // -> no? wait
144 else l = 0; // -> else go on
145
146 return l;
147}
148
149// FEED SOUND DATA
150void SoundFeedStreamData(unsigned char* pSound,long lBytes)
151{
152 if (handle == NULL) return;
153
154 if (snd_pcm_state(handle) == SND_PCM_STATE_XRUN)
155 snd_pcm_prepare(handle);
156 snd_pcm_writei(handle,pSound,
157 iDisStereo ? lBytes / 2 : lBytes / 4);
158}