audio/sdl: be more anal about errors
[qemu.git] / audio / audio_win_int.c
blob58690524c474ca69c2557f52279e7e167bcf9b95
1 /* public domain */
3 #include "qemu-common.h"
4 #include "audio.h"
6 #define AUDIO_CAP "win-int"
7 #include <windows.h>
8 #include <mmsystem.h>
10 #include "audio.h"
11 #include "audio_int.h"
12 #include "audio_win_int.h"
14 int waveformat_from_audio_settings (WAVEFORMATEX *wfx,
15 struct audsettings *as)
17 memset (wfx, 0, sizeof (*wfx));
19 wfx->wFormatTag = WAVE_FORMAT_PCM;
20 wfx->nChannels = as->nchannels;
21 wfx->nSamplesPerSec = as->freq;
22 wfx->nAvgBytesPerSec = as->freq << (as->nchannels == 2);
23 wfx->nBlockAlign = 1 << (as->nchannels == 2);
24 wfx->cbSize = 0;
26 switch (as->fmt) {
27 case AUD_FMT_S8:
28 case AUD_FMT_U8:
29 wfx->wBitsPerSample = 8;
30 break;
32 case AUD_FMT_S16:
33 case AUD_FMT_U16:
34 wfx->wBitsPerSample = 16;
35 wfx->nAvgBytesPerSec <<= 1;
36 wfx->nBlockAlign <<= 1;
37 break;
39 case AUD_FMT_S32:
40 case AUD_FMT_U32:
41 wfx->wBitsPerSample = 32;
42 wfx->nAvgBytesPerSec <<= 2;
43 wfx->nBlockAlign <<= 2;
44 break;
46 default:
47 dolog ("Internal logic error: Bad audio format %d\n", as->freq);
48 return -1;
51 return 0;
54 int waveformat_to_audio_settings (WAVEFORMATEX *wfx,
55 struct audsettings *as)
57 if (wfx->wFormatTag != WAVE_FORMAT_PCM) {
58 dolog ("Invalid wave format, tag is not PCM, but %d\n",
59 wfx->wFormatTag);
60 return -1;
63 if (!wfx->nSamplesPerSec) {
64 dolog ("Invalid wave format, frequency is zero\n");
65 return -1;
67 as->freq = wfx->nSamplesPerSec;
69 switch (wfx->nChannels) {
70 case 1:
71 as->nchannels = 1;
72 break;
74 case 2:
75 as->nchannels = 2;
76 break;
78 default:
79 dolog (
80 "Invalid wave format, number of channels is not 1 or 2, but %d\n",
81 wfx->nChannels
83 return -1;
86 switch (wfx->wBitsPerSample) {
87 case 8:
88 as->fmt = AUD_FMT_U8;
89 break;
91 case 16:
92 as->fmt = AUD_FMT_S16;
93 break;
95 case 32:
96 as->fmt = AUD_FMT_S32;
97 break;
99 default:
100 dolog ("Invalid wave format, bits per sample is not "
101 "8, 16 or 32, but %d\n",
102 wfx->wBitsPerSample);
103 return -1;
106 return 0;