diff options
| author | pepper <peppersclothescult@gmail.com> | 2015-01-10 21:32:32 -0800 |
|---|---|---|
| committer | pepper <peppersclothescult@gmail.com> | 2015-01-10 21:32:32 -0800 |
| commit | d53fa8a169832563c62262078b8d2ffe5cab8473 (patch) | |
| tree | b911d06d357d009c976709780f10e92ce915228a /source/time | |
first
Diffstat (limited to 'source/time')
| -rw-r--r-- | source/time/AudioClock.c | 72 | ||||
| -rw-r--r-- | source/time/AudioClock.h | 82 | ||||
| -rw-r--r-- | source/time/TaskTimer.c | 164 | ||||
| -rw-r--r-- | source/time/TaskTimer.h | 107 |
4 files changed, 425 insertions, 0 deletions
diff --git a/source/time/AudioClock.c b/source/time/AudioClock.c new file mode 100644 index 0000000..022abb2 --- /dev/null +++ b/source/time/AudioClock.c @@ -0,0 +1,72 @@ +// +// AudioClock.c - MrsWatson +// Created by Nik Reiman on 1/5/12. +// Copyright (c) 2012 Teragon Audio. All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// * Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. +// + +#include <stdio.h> +#include <stdlib.h> + +#include "time/AudioClock.h" + +AudioClock audioClockInstance = NULL; + +void initAudioClock(void) +{ + audioClockInstance = (AudioClock)malloc(sizeof(AudioClockMembers)); + audioClockInstance->currentFrame = 0; + audioClockInstance->transportChanged = false; + audioClockInstance->isPlaying = false; +} + +AudioClock getAudioClock(void) +{ + return audioClockInstance; +} + +void advanceAudioClock(AudioClock self, const unsigned long blocksize) +{ + if (self->currentFrame == 0 || !self->isPlaying) { + self->transportChanged = true; + self->isPlaying = true; + } else { + self->transportChanged = false; + } + + self->currentFrame += blocksize; +} + +void audioClockStop(AudioClock self) +{ + self->isPlaying = false; + self->transportChanged = true; +} + +void freeAudioClock(AudioClock self) +{ + if (self != NULL) { + free(self); + self = NULL; + } +} diff --git a/source/time/AudioClock.h b/source/time/AudioClock.h new file mode 100644 index 0000000..a0e5f2d --- /dev/null +++ b/source/time/AudioClock.h @@ -0,0 +1,82 @@ +// +// AudioClock.h - MrsWatson +// Created by Nik Reiman on 1/5/12. +// Copyright (c) 2012 Teragon Audio. All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// * Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. +// + +#ifndef MrsWatson_AudioClock_h +#define MrsWatson_AudioClock_h + +#include "base/Types.h" + +/** + * The AudioClock class keeps track of the sequence time and delivers the + * position in a variety of formats. Unlike most other classes, this one + * maintains a singleton instance because it must be accessed from C++ + * callbacks where it is difficult to pass a void* pointer. + */ + +typedef struct { + boolByte transportChanged; + boolByte isPlaying; + unsigned long currentFrame; +} AudioClockMembers; +typedef AudioClockMembers *AudioClock; +extern AudioClock audioClockInstance; + +/** + * Initialize the global audio clock instance. Should be called fairly + * early in the program initialization, as other components may depend + * on knowing the current position. + */ +void initAudioClock(void); + +/** + * Get a reference to the global audio clock instance. + * @return Reference to global audio clock, or NULL if the global instance has + * not yet been initialized. + */ +AudioClock getAudioClock(void); + +/** + * Advanced the global audio clock by a given number of samples. This should be + * called after processing each block. + * @param self + * @param blocksize Block size in sample frames. + */ +void advanceAudioClock(AudioClock self, const unsigned long blocksize); + +/** + * Indicate that playback is stopped. + * @param self + */ +void audioClockStop(AudioClock self); + +/** + * Free an audio clock instance and its associated resources. + * @param self + */ +void freeAudioClock(AudioClock self); + +#endif diff --git a/source/time/TaskTimer.c b/source/time/TaskTimer.c new file mode 100644 index 0000000..9add3b1 --- /dev/null +++ b/source/time/TaskTimer.c @@ -0,0 +1,164 @@ +// +// TaskTimer.c - MrsWatson +// Created by Nik Reiman on 1/5/12. +// Copyright (c) 2012 Teragon Audio. All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// * Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. +// + +#include <stdio.h> +#include <stdlib.h> + +#if UNIX +#include <time.h> +#endif + +#include "time/TaskTimer.h" + +TaskTimer newTaskTimer(const CharString component, const char *subcomponent) +{ + const char *componentCString = component != NULL ? component->data : NULL; + return newTaskTimerWithCString(componentCString, subcomponent); +} + +TaskTimer newTaskTimerWithCString(const char *component, const char *subcomponent) +{ + TaskTimer taskTimer = (TaskTimer)malloc(sizeof(TaskTimerMembers)); +#if WINDOWS + LARGE_INTEGER queryFrequency; +#endif + + taskTimer->component = newCharStringWithCString(component); + taskTimer->subcomponent = newCharStringWithCString(subcomponent); + taskTimer->enabled = true; + taskTimer->_running = false; + taskTimer->totalTaskTime = 0.0; + +#if WINDOWS + QueryPerformanceFrequency(&queryFrequency); + taskTimer->counterFrequency = (double)(queryFrequency.QuadPart) / 1000.0; +#endif + + return taskTimer; +} + +void taskTimerStart(TaskTimer self) +{ + if (self->_running) { + taskTimerStop(self); + } + +#if WINDOWS + QueryPerformanceCounter(&(self->startTime)); +#elif UNIX + gettimeofday(&self->startTime, NULL); +#endif + self->_running = true; +} + +double taskTimerStop(TaskTimer self) +{ + double elapsedTimeInMs = 0.0; +#if UNIX + double elapsedFullSeconds; + double elapsedMicroseconds; + struct timeval currentTime; +#elif WINDOWS + LONGLONG elapsedTimeInClocks; + LARGE_INTEGER stopTime; +#endif + + if (!self->_running) { + return 0.0; + } + +#if UNIX + + if (gettimeofday(¤tTime, NULL) == 0) { + if (currentTime.tv_sec == self->startTime.tv_sec) { + elapsedTimeInMs = (double)(currentTime.tv_usec - self->startTime.tv_usec) / 1000.0; + } else { + elapsedFullSeconds = (double)(currentTime.tv_sec - self->startTime.tv_sec - 1); + elapsedMicroseconds = (double)(currentTime.tv_usec + (1000000l - self->startTime.tv_usec)); + elapsedTimeInMs = (elapsedFullSeconds * 1000.0) + (elapsedMicroseconds / 1000.0); + } + + self->totalTaskTime += elapsedTimeInMs; + } + +#elif WINDOWS + QueryPerformanceCounter(&stopTime); + elapsedTimeInClocks = stopTime.QuadPart - self->startTime.QuadPart; + elapsedTimeInMs = (double)(elapsedTimeInClocks) / self->counterFrequency; + self->totalTaskTime += elapsedTimeInMs; +#endif + + self->_running = false; + return elapsedTimeInMs; +} + +CharString taskTimerHumanReadbleString(TaskTimer self) +{ + int hours, minutes, seconds; + CharString outString = newCharStringWithCapacity(kCharStringLengthShort); + + if (self->totalTaskTime < 1000) { + snprintf(outString->data, outString->capacity, "%dms", (int)self->totalTaskTime); + } else if (self->totalTaskTime < 60 * 1000) { + seconds = (int)(self->totalTaskTime / 1000.0); + snprintf(outString->data, outString->capacity, "%dsec", seconds); + } else { + seconds = (int)(self->totalTaskTime / 1000.0) % 60; + minutes = (int)(self->totalTaskTime / (1000.0 * 60.0)); + + if (minutes > 60) { + hours = minutes / 60; + minutes = (minutes % 60); + snprintf(outString->data, outString->capacity, "%d:%d:%dsec", hours, minutes, seconds); + } else { + snprintf(outString->data, outString->capacity, "%d:%dsec", minutes, seconds); + } + } + + return outString; +} + +void taskTimerSleep(const double milliseconds) +{ +#if UNIX + struct timespec sleepTime; + sleepTime.tv_sec = 0; + sleepTime.tv_nsec = (long)(1000000.0 * milliseconds); + nanosleep(&sleepTime, NULL); +#elif WINDOWS + Sleep((DWORD)milliseconds); +#endif +} + +void freeTaskTimer(TaskTimer self) +{ + if (self != NULL) { + freeCharString(self->component); + freeCharString(self->subcomponent); + free(self); + } +} diff --git a/source/time/TaskTimer.h b/source/time/TaskTimer.h new file mode 100644 index 0000000..43dac8c --- /dev/null +++ b/source/time/TaskTimer.h @@ -0,0 +1,107 @@ +// +// TaskTimer.h - MrsWatson +// Created by Nik Reiman on 1/5/12. +// Copyright (c) 2012 Teragon Audio. All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// * Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. +// + +#ifndef MrsWatson_TaskTimer_h +#define MrsWatson_TaskTimer_h + +#include "base/CharString.h" + +#if UNIX +#include <sys/time.h> +#endif + +typedef struct { + CharString component; + CharString subcomponent; + boolByte enabled; + boolByte _running; + double totalTaskTime; + +#if WINDOWS + LARGE_INTEGER startTime; + double counterFrequency; +#elif UNIX + struct timeval startTime; +#endif +} TaskTimerMembers; +typedef TaskTimerMembers *TaskTimer; + +/** + * Create a new task timer. + * @param component Component name which this timer belongs to. NULL or empty + * string may be passed for this argument. + * @param subcomponent Subcomponent which this timer is measured. NULL or empty + * string may be passed for this argument. + * @return Initialized instance + */ +TaskTimer newTaskTimer(const CharString component, const char *subcomponent); + +/** + * Create a new task timer. + * @param component Component name which this timer belongs to. NULL or empty + * string may be passed for this argument. + * @param subcomponent Subcomponent which this timer is measured. NULL or empty + * string may be passed for this argument. + * @return Initialized instance + */ +TaskTimer newTaskTimerWithCString(const char *component, const char *subcomponent); + +/** + * Start the timer. Timers may be stopped and started multiple times. + * @param self + */ +void taskTimerStart(TaskTimer self); + +/** + * Stop the timer. Timers may be stopped and started multiple times. + * @param self + * @return Time used since last call to taskTimerStart() + */ +double taskTimerStop(TaskTimer self); + +/** + * Get the string representation of the total accumulated time for this timer. + * @param self + * @return Formatted string, which the caller must free themselves when finished + */ +CharString taskTimerHumanReadbleString(TaskTimer self); + +/** + * Suspend execution for a given amount of milliseconds. Depending on the host + * operating system, the amount of time actually slept may differ slightly from + * the requested amount. + * @param milliseconds Number of milliseconds to sleep + */ +void taskTimerSleep(const double milliseconds); + +/** + * Free a task timer and its associated resources + * @param self + */ +void freeTaskTimer(TaskTimer self); + +#endif |
