Commit | Line | Data |
---|---|---|
ef79bbde P |
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 | ||
28 | static snd_pcm_t *handle = NULL; | |
29 | static snd_pcm_uframes_t buffer_size; | |
30 | ||
31 | // SETUP SOUND | |
32 | void 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 | ||
97ea4077 | 43 | pchannels=2; |
ef79bbde P |
44 | |
45 | pspeed = 44100; | |
46 | format = SND_PCM_FORMAT_S16; | |
47 | ||
48 | if ((err = snd_pcm_open(&handle, "default", | |
49 | SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK)) < 0) | |
50 | { | |
51 | printf("Audio open error: %s\n", snd_strerror(err)); | |
52 | return; | |
53 | } | |
54 | ||
55 | if((err = snd_pcm_nonblock(handle, 0))<0) | |
56 | { | |
57 | printf("Can't set blocking moded: %s\n", snd_strerror(err)); | |
58 | return; | |
59 | } | |
60 | ||
61 | snd_pcm_hw_params_alloca(&hwparams); | |
62 | ||
63 | if((err=snd_pcm_hw_params_any(handle, hwparams))<0) | |
64 | { | |
65 | printf("Broken configuration for this PCM: %s\n", snd_strerror(err)); | |
66 | return; | |
67 | } | |
68 | ||
69 | if((err=snd_pcm_hw_params_set_access(handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED))<0) | |
70 | { | |
71 | printf("Access type not available: %s\n", snd_strerror(err)); | |
72 | return; | |
73 | } | |
74 | ||
75 | if((err=snd_pcm_hw_params_set_format(handle, hwparams, format))<0) | |
76 | { | |
77 | printf("Sample format not available: %s\n", snd_strerror(err)); | |
78 | return; | |
79 | } | |
80 | ||
81 | if((err=snd_pcm_hw_params_set_channels(handle, hwparams, pchannels))<0) | |
82 | { | |
83 | printf("Channels count not available: %s\n", snd_strerror(err)); | |
84 | return; | |
85 | } | |
86 | ||
87 | if((err=snd_pcm_hw_params_set_rate_near(handle, hwparams, &pspeed, 0))<0) | |
88 | { | |
89 | printf("Rate not available: %s\n", snd_strerror(err)); | |
90 | return; | |
91 | } | |
92 | ||
93 | if((err=snd_pcm_hw_params_set_buffer_time_near(handle, hwparams, &buffer_time, 0))<0) | |
94 | { | |
95 | printf("Buffer time error: %s\n", snd_strerror(err)); | |
96 | return; | |
97 | } | |
98 | ||
99 | if((err=snd_pcm_hw_params_set_period_time_near(handle, hwparams, &period_time, 0))<0) | |
100 | { | |
101 | printf("Period time error: %s\n", snd_strerror(err)); | |
102 | return; | |
103 | } | |
104 | ||
105 | if((err=snd_pcm_hw_params(handle, hwparams))<0) | |
106 | { | |
107 | printf("Unable to install hw params: %s\n", snd_strerror(err)); | |
108 | return; | |
109 | } | |
110 | ||
111 | snd_pcm_status_alloca(&status); | |
112 | if((err=snd_pcm_status(handle, status))<0) | |
113 | { | |
114 | printf("Unable to get status: %s\n", snd_strerror(err)); | |
115 | return; | |
116 | } | |
117 | ||
118 | buffer_size = snd_pcm_status_get_avail(status); | |
119 | } | |
120 | ||
121 | // REMOVE SOUND | |
122 | void RemoveSound(void) | |
123 | { | |
124 | if(handle != NULL) | |
125 | { | |
126 | snd_pcm_drop(handle); | |
127 | snd_pcm_close(handle); | |
128 | handle = NULL; | |
129 | } | |
130 | } | |
131 | ||
132 | // GET BYTES BUFFERED | |
133 | unsigned long SoundGetBytesBuffered(void) | |
134 | { | |
135 | unsigned long l; | |
136 | ||
137 | if (handle == NULL) // failed to open? | |
138 | return SOUNDSIZE; | |
139 | l = snd_pcm_avail(handle); | |
140 | if (l < 0) return 0; | |
141 | if (l < buffer_size / 2) // can we write in at least the half of fragments? | |
142 | l = SOUNDSIZE; // -> no? wait | |
143 | else l = 0; // -> else go on | |
144 | ||
145 | return l; | |
146 | } | |
147 | ||
148 | // FEED SOUND DATA | |
149 | void SoundFeedStreamData(unsigned char* pSound,long lBytes) | |
150 | { | |
151 | if (handle == NULL) return; | |
152 | ||
153 | if (snd_pcm_state(handle) == SND_PCM_STATE_XRUN) | |
154 | snd_pcm_prepare(handle); | |
97ea4077 | 155 | snd_pcm_writei(handle,pSound, lBytes / 4); |
ef79bbde | 156 | } |