OpenShot Audio Library | OpenShotAudio  0.6.0
juce_CoreAudioTimeConversions_mac.h
1 /*
2  ==============================================================================
3 
4  This file is part of the JUCE library.
5  Copyright (c) 2022 - Raw Material Software Limited
6 
7  JUCE is an open source library subject to commercial or open-source
8  licensing.
9 
10  The code included in this file is provided under the terms of the ISC license
11  http://www.isc.org/downloads/software-support-policy/isc-license. Permission
12  To use, copy, modify, and/or distribute this software for any purpose with or
13  without fee is hereby granted provided that the above copyright notice and
14  this permission notice appear in all copies.
15 
16  JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
17  EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
18  DISCLAIMED.
19 
20  ==============================================================================
21 */
22 
23 
24 // This file will be included directly by macOS/iOS-specific .cpps
25 #pragma once
26 
27 #if ! DOXYGEN
28 
29 #include <mach/mach_time.h>
30 
31 namespace juce
32 {
33 
34 struct CoreAudioTimeConversions
35 {
36 public:
37  CoreAudioTimeConversions()
38  {
39  mach_timebase_info_data_t info{};
40  mach_timebase_info (&info);
41  numerator = info.numer;
42  denominator = info.denom;
43  }
44 
45  uint64_t hostTimeToNanos (uint64_t hostTime) const
46  {
47  return multiplyByRatio (hostTime, numerator, denominator);
48  }
49 
50  uint64_t nanosToHostTime (uint64_t nanos) const
51  {
52  return multiplyByRatio (nanos, denominator, numerator);
53  }
54 
55 private:
56  // Adapted from CAHostTimeBase.h in the Core Audio Utility Classes
57  static uint64_t multiplyByRatio (uint64_t toMultiply, uint64_t numerator, uint64_t denominator)
58  {
59  #if defined (__SIZEOF_INT128__)
60  unsigned __int128
61  #else
62  long double
63  #endif
64  result = toMultiply;
65 
66  if (numerator != denominator)
67  {
68  result *= numerator;
69  result /= denominator;
70  }
71 
72  return (uint64_t) result;
73  }
74 
75  uint64_t numerator = 0, denominator = 0;
76 };
77 
78 } // namespace juce
79 
80 #endif