mixer: fix lowering hw volume while muted
[mplayer.git] / osdep / timer-darwin.c
blob19fcdd946a7ffaef456104362773e371870d9026
1 /*
2 * Precise timer routines using Mach timing
4 * Copyright (c) 2003-2004, Dan Villiom Podlaski Christiansen
6 * Permission is hereby granted, free of charge, to any person
7 * obtaining a copy of this software and associated documentation
8 * files (the "Software"), to deal in the Software without
9 * restriction, including without limitation the rights to use, copy,
10 * modify, merge, publish, distribute, sublicense, and/or sell copies
11 * of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
18 #include <unistd.h>
19 #include <stdint.h>
20 #include <stdlib.h>
21 #include <time.h>
22 #include <math.h>
23 #include <sys/time.h>
24 #include <mach/mach_time.h>
26 #include "config.h"
27 #include "mp_msg.h"
28 #include "timer.h"
30 /* global variables */
31 static double timebase_ratio;
33 const char timer_name[] = "Darwin accurate";
37 /* the core sleep function, uses floats and is used in MPlayer G2 */
38 static float sleep_accurate(float time_frame)
40 uint64_t deadline = time_frame / timebase_ratio + mach_absolute_time();
42 mach_wait_until(deadline);
44 return (mach_absolute_time() - deadline) * timebase_ratio;
47 /* wrapper for MPlayer G1 */
48 int usec_sleep(int usec_delay)
50 return sleep_accurate(usec_delay / 1e6) * 1e6;
54 /* current time in microseconds */
55 unsigned int GetTimer(void)
57 return (unsigned int)(uint64_t)(mach_absolute_time() * timebase_ratio * 1e6);
60 /* current time in milliseconds */
61 unsigned int GetTimerMS(void)
63 return (unsigned int)(uint64_t)(mach_absolute_time() * timebase_ratio * 1e3);
66 /* initialize timer, must be called at least once at start */
67 void InitTimer(void)
69 struct mach_timebase_info timebase;
71 mach_timebase_info(&timebase);
72 timebase_ratio = (double)timebase.numer / (double)timebase.denom
73 * (double)1e-9;
76 #if 0
77 #include <stdio.h>
79 int main(void) {
80 int i,j, r, c = 200;
81 long long t = 0;
83 InitTimer();
85 for (i = 0; i < c; i++) {
86 const int delay = rand() / (RAND_MAX / 1e5);
87 j = GetTimer();
88 #if 1
89 r = usec_sleep(delay);
90 #else
91 r = sleep_accurate(delay / 1e6) * 1e6;
92 #endif
93 j = (GetTimer() - j) - delay;
94 printf("sleep time:%8i %5i (%i)\n", delay, j, j - r);
95 t += j - r;
97 fprintf(stderr, "average error:\t%lli\n", t / c);
99 return 0;
101 #endif