VirtualBox

source: vbox/trunk/src/VBox/ValidationKit/utils/audio/ntPlayToneWaveX.cpp@ 104620

Last change on this file since 104620 was 98103, checked in by vboxsync, 22 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.1 KB
Line 
1/* $Id: ntPlayToneWaveX.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * ????
4 */
5
6/*
7 * Copyright (C) 2012-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#include <iprt/win/windows.h>
42
43#include <iprt/getopt.h>
44#include <iprt/initterm.h>
45#include <iprt/message.h>
46#include <iprt/stream.h>
47#include <iprt/errcore.h>
48
49#define _USE_MATH_DEFINES
50#include <math.h>
51
52
53/*********************************************************************************************************************************
54* Global Variables *
55*********************************************************************************************************************************/
56uint32_t g_cSamplesPerSec = 44100;
57uint32_t g_cSamplesPerPeriod = 100; // 441.0Hz for 44.1kHz
58uint32_t g_cSamplesInBuffer = 4096;
59double g_rdSecDuration = 5.0;
60
61uint32_t g_cbSample; // assuming 16-bit stereo (for now)
62
63HWAVEOUT g_hWaveOut;
64HANDLE g_hWavEvent;
65
66
67int main(int argc, char **argv)
68{
69 int rc = RTR3InitExe(argc, &argv, 0);
70 if (RT_FAILURE(rc))
71 return RTMsgInitFailure(rc);
72
73 static const RTGETOPTDEF s_aOptions[] =
74 {
75 { "--samples-per-sec", 's', RTGETOPT_REQ_UINT32 },
76 { "--period-in-samples", 'p', RTGETOPT_REQ_UINT32 },
77 { "--bufsize-in-samples", 'b', RTGETOPT_REQ_UINT32 },
78 { "--total-duration-in-secs", 'd', RTGETOPT_REQ_UINT32 }
79 };
80
81 RTGETOPTSTATE State;
82 RTGetOptInit(&State, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 1, 0);
83 RTGETOPTUNION ValueUnion;
84 int chOpt;
85 while ((chOpt = RTGetOpt(&State, &ValueUnion)) != 0)
86 {
87 switch (chOpt)
88 {
89 case 's': g_cSamplesPerSec = ValueUnion.u32; break;
90 case 'p': g_cSamplesPerPeriod = ValueUnion.u32; break;
91 case 'b': g_cSamplesInBuffer = ValueUnion.u32; break;
92 case 'd': g_rdSecDuration = ValueUnion.u32; break;
93 case 'h':
94 RTPrintf("usage: ntPlayToneWaveX.exe\n"
95 "[-s|--samples-per-sec]\n"
96 "[-p|--period-in-samples]\n"
97 "[-b|--bufsize-in-samples]\n"
98 "[-d|--total-duration-in-secs]\n"
99 "\n"
100 "Plays sine tone using ancient waveX API\n");
101 return 0;
102
103 default:
104 return RTGetOptPrintError(chOpt, &ValueUnion);
105 }
106 }
107
108
109 WAVEFORMATEX waveFormatEx = { 0 };
110 MMRESULT mmresult;
111
112 waveFormatEx.wFormatTag = WAVE_FORMAT_PCM;
113 waveFormatEx.nChannels = 2;
114 waveFormatEx.nSamplesPerSec = g_cSamplesPerSec;
115 waveFormatEx.wBitsPerSample = 16;
116 waveFormatEx.nBlockAlign = g_cbSample = waveFormatEx.nChannels * waveFormatEx.wBitsPerSample / 8;
117 waveFormatEx.nAvgBytesPerSec = waveFormatEx.nBlockAlign * waveFormatEx.nSamplesPerSec;
118 waveFormatEx.cbSize = 0;
119
120 g_hWavEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
121 mmresult = waveOutOpen(&g_hWaveOut, WAVE_MAPPER, &waveFormatEx, (DWORD_PTR)g_hWavEvent, NULL, CALLBACK_EVENT);
122
123 if (mmresult != MMSYSERR_NOERROR)
124 {
125 RTMsgError("waveOutOpen failed with 0x%X\n", mmresult);
126 return -1;
127 }
128
129
130 uint32_t ui32SamplesToPlayTotal = (uint32_t)(g_rdSecDuration * g_cSamplesPerSec);
131 uint32_t ui32SamplesToPlay = ui32SamplesToPlayTotal;
132 uint32_t ui32SamplesPlayed = 0;
133 uint32_t ui32SamplesForWavBuf;
134
135 WAVEHDR waveHdr1 = {0}, waveHdr2 = {0}, *pWaveHdr, *pWaveHdrPlaying, *pWaveHdrWaiting;
136 uint32_t i, k;
137 DWORD res;
138
139 int16_t *i16Samples1 = (int16_t *)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, g_cSamplesInBuffer * g_cbSample);
140 int16_t *i16Samples2 = (int16_t *)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, g_cSamplesInBuffer * g_cbSample);
141
142 k = 0; // This is discrete time really!!!
143
144 for (i = 0; i < g_cSamplesInBuffer; i++, k++)
145 {
146 i16Samples1[2 * i] = (uint16_t)(10000.0 * sin(2.0 * M_PI * k / g_cSamplesPerPeriod));
147 i16Samples1[2 * i + 1] = i16Samples1[2 * i];
148 }
149
150 ui32SamplesForWavBuf = min(ui32SamplesToPlay, g_cSamplesInBuffer);
151
152 waveHdr1.lpData = (LPSTR)i16Samples1;
153 waveHdr1.dwBufferLength = ui32SamplesForWavBuf * g_cbSample;
154 waveHdr1.dwFlags = 0;
155 waveHdr1.dwLoops = 0;
156
157 ui32SamplesToPlay -= ui32SamplesForWavBuf;
158 ui32SamplesPlayed += ui32SamplesForWavBuf;
159
160 pWaveHdrPlaying = &waveHdr1;
161
162 mmresult = waveOutPrepareHeader(g_hWaveOut, pWaveHdrPlaying, sizeof(WAVEHDR));
163 mmresult = waveOutWrite(g_hWaveOut, pWaveHdrPlaying, sizeof(WAVEHDR));
164 //RTMsgInfo("waveOutWrite completes with %d\n", mmresult);
165
166 res = WaitForSingleObject(g_hWavEvent, INFINITE);
167 //RTMsgInfo("WaitForSingleObject completes with %d\n\n", res);
168
169 waveHdr2.lpData = (LPSTR)i16Samples2;
170 waveHdr2.dwBufferLength = 0;
171 waveHdr2.dwFlags = 0;
172 waveHdr2.dwLoops = 0;
173
174 pWaveHdrWaiting = &waveHdr2;
175
176 while (ui32SamplesToPlay > 0)
177 {
178 int16_t *i16Samples = (int16_t *)pWaveHdrWaiting->lpData;
179
180 for (i = 0; i < g_cSamplesInBuffer; i++, k++)
181 {
182 i16Samples[2 * i] = (uint16_t)(10000.0 * sin(2.0 * M_PI * k / g_cSamplesPerPeriod));
183 i16Samples[2 * i + 1] = i16Samples[2 * i];
184 }
185
186 ui32SamplesForWavBuf = min(ui32SamplesToPlay, g_cSamplesInBuffer);
187
188 pWaveHdrWaiting->dwBufferLength = ui32SamplesForWavBuf * g_cbSample;
189 pWaveHdrWaiting->dwFlags = 0;
190 pWaveHdrWaiting->dwLoops = 0;
191
192
193 ui32SamplesToPlay -= ui32SamplesForWavBuf;
194 ui32SamplesPlayed += ui32SamplesForWavBuf;
195
196 mmresult = waveOutPrepareHeader(g_hWaveOut, pWaveHdrWaiting, sizeof(WAVEHDR));
197 mmresult = waveOutWrite(g_hWaveOut, pWaveHdrWaiting, sizeof(WAVEHDR));
198 //RTMsgInfo("waveOutWrite completes with %d\n", mmresult);
199
200 res = WaitForSingleObject(g_hWavEvent, INFINITE);
201 //RTMsgInfo("WaitForSingleObject completes with %d\n\n", res);
202
203 mmresult = waveOutUnprepareHeader(g_hWaveOut, pWaveHdrPlaying, sizeof(WAVEHDR));
204 //RTMsgInfo("waveOutUnprepareHeader completes with %d\n", mmresult);
205
206 pWaveHdr = pWaveHdrWaiting;
207 pWaveHdrWaiting = pWaveHdrPlaying;
208 pWaveHdrPlaying = pWaveHdr;
209 }
210
211 while (mmresult = waveOutUnprepareHeader(g_hWaveOut, pWaveHdrPlaying, sizeof(WAVEHDR)))
212 {
213 //Expecting WAVERR_STILLPLAYING
214 //RTMsgInfo("waveOutUnprepareHeader failed with 0x%X\n", mmresult);
215 Sleep(100);
216 }
217
218 if (mmresult == MMSYSERR_NOERROR)
219 {
220 waveOutClose(g_hWaveOut);
221 }
222
223 HeapFree(GetProcessHeap(), 0, i16Samples1);
224 HeapFree(GetProcessHeap(), 0, i16Samples2);
225}
226
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette