Add audio_utils.h header in PortaudioBackend for de/interleaving audio data
[ardour.git] / libs / backends / portaudio / win_utils.cc
1 /*
2  * Copyright (C) 2015 Tim Mayberry <mojofunk@gmail.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18
19 #include "win_utils.h"
20
21 #include <windows.h>
22 #include <mmsystem.h>
23
24 #include "pbd/compose.h"
25
26 #include "debug.h"
27
28 namespace {
29
30 LARGE_INTEGER
31 get_frequency ()
32 {
33         LARGE_INTEGER freq;
34         QueryPerformanceFrequency(&freq);
35         return freq;
36 }
37
38
39 UINT&
40 old_timer_resolution ()
41 {
42         static UINT timer_res_ms = 0;
43         return timer_res_ms;
44 }
45
46 } // anon namespace
47
48 namespace utils {
49
50 bool
51 set_min_timer_resolution ()
52 {
53         TIMECAPS caps;
54
55         if (timeGetDevCaps (&caps, sizeof(TIMECAPS)) != TIMERR_NOERROR) {
56                 DEBUG_TIMING ("Could not get timer device capabilities.\n");
57                 return false;
58         } else {
59                 old_timer_resolution () = caps.wPeriodMin;
60                 if (timeBeginPeriod (caps.wPeriodMin) != TIMERR_NOERROR) {
61                         DEBUG_TIMING (string_compose (
62                             "Could not set minimum timer resolution to %1(ms)\n", caps.wPeriodMin));
63                         return false;
64                 }
65         }
66
67         DEBUG_TIMING (string_compose ("Multimedia timer resolution set to %1(ms)\n",
68                                       caps.wPeriodMin));
69
70         return true;
71 }
72
73 bool
74 reset_timer_resolution ()
75 {
76         if (old_timer_resolution ()) {
77                 if (timeEndPeriod (old_timer_resolution ()) != TIMERR_NOERROR) {
78                         DEBUG_TIMING ("Could not reset timer resolution.\n");
79                         return false;
80                 }
81         }
82
83         DEBUG_TIMING (string_compose ("Multimedia timer resolution set to %1(ms)\n",
84                                       old_timer_resolution ()));
85
86         return true;
87 }
88
89 uint64_t get_microseconds ()
90 {
91         static LARGE_INTEGER frequency = get_frequency ();
92         LARGE_INTEGER current_val;
93
94         QueryPerformanceCounter (&current_val);
95
96         return (uint64_t)(((double)current_val.QuadPart) /
97                           ((double)frequency.QuadPart) * 1000000.0);
98 }
99
100 } // namespace utils