xref: /openbmc/qemu/audio/audio_win_int.c (revision 14a650ec)
1 /* public domain */
2 
3 #include "qemu-common.h"
4 
5 #define AUDIO_CAP "win-int"
6 #include <windows.h>
7 #include <mmsystem.h>
8 
9 #include "audio.h"
10 #include "audio_int.h"
11 #include "audio_win_int.h"
12 
13 int waveformat_from_audio_settings (WAVEFORMATEX *wfx,
14                                     struct audsettings *as)
15 {
16     memset (wfx, 0, sizeof (*wfx));
17 
18     wfx->wFormatTag = WAVE_FORMAT_PCM;
19     wfx->nChannels = as->nchannels;
20     wfx->nSamplesPerSec = as->freq;
21     wfx->nAvgBytesPerSec = as->freq << (as->nchannels == 2);
22     wfx->nBlockAlign = 1 << (as->nchannels == 2);
23     wfx->cbSize = 0;
24 
25     switch (as->fmt) {
26     case AUD_FMT_S8:
27     case AUD_FMT_U8:
28         wfx->wBitsPerSample = 8;
29         break;
30 
31     case AUD_FMT_S16:
32     case AUD_FMT_U16:
33         wfx->wBitsPerSample = 16;
34         wfx->nAvgBytesPerSec <<= 1;
35         wfx->nBlockAlign <<= 1;
36         break;
37 
38     case AUD_FMT_S32:
39     case AUD_FMT_U32:
40         wfx->wBitsPerSample = 32;
41         wfx->nAvgBytesPerSec <<= 2;
42         wfx->nBlockAlign <<= 2;
43         break;
44 
45     default:
46         dolog ("Internal logic error: Bad audio format %d\n", as->freq);
47         return -1;
48     }
49 
50     return 0;
51 }
52 
53 int waveformat_to_audio_settings (WAVEFORMATEX *wfx,
54                                   struct audsettings *as)
55 {
56     if (wfx->wFormatTag != WAVE_FORMAT_PCM) {
57         dolog ("Invalid wave format, tag is not PCM, but %d\n",
58                wfx->wFormatTag);
59         return -1;
60     }
61 
62     if (!wfx->nSamplesPerSec) {
63         dolog ("Invalid wave format, frequency is zero\n");
64         return -1;
65     }
66     as->freq = wfx->nSamplesPerSec;
67 
68     switch (wfx->nChannels) {
69     case 1:
70         as->nchannels = 1;
71         break;
72 
73     case 2:
74         as->nchannels = 2;
75         break;
76 
77     default:
78         dolog (
79             "Invalid wave format, number of channels is not 1 or 2, but %d\n",
80             wfx->nChannels
81             );
82         return -1;
83     }
84 
85     switch (wfx->wBitsPerSample) {
86     case 8:
87         as->fmt = AUD_FMT_U8;
88         break;
89 
90     case 16:
91         as->fmt = AUD_FMT_S16;
92         break;
93 
94     case 32:
95         as->fmt = AUD_FMT_S32;
96         break;
97 
98     default:
99         dolog ("Invalid wave format, bits per sample is not "
100                "8, 16 or 32, but %d\n",
101                wfx->wBitsPerSample);
102         return -1;
103     }
104 
105     return 0;
106 }
107 
108