Main Page   Compound List   File List   Compound Members   File Members  

OSC-timetag.c

Go to the documentation of this file.
00001 /*
00002 Copyright (c) 1998.  The Regents of the University of California (Regents).
00003 All Rights Reserved.
00004 
00005 Permission to use, copy, modify, and distribute this software and its
00006 documentation for educational, research, and not-for-profit purposes, without
00007 fee and without a signed licensing agreement, is hereby granted, provided that
00008 the above copyright notice, this paragraph and the following two paragraphs
00009 appear in all copies, modifications, and distributions.  Contact The Office of
00010 Technology Licensing, UC Berkeley, 2150 Shattuck Avenue, Suite 510, Berkeley,
00011 CA 94720-1620, (510) 643-7201, for commercial licensing opportunities.
00012 
00013 Written by Matt Wright, The Center for New Music and Audio Technologies,
00014 University of California, Berkeley.
00015 
00016      IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
00017      SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS,
00018      ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
00019      REGENTS HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00020 
00021      REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
00022      LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00023      FOR A PARTICULAR PURPOSE. THE SOFTWARE AND ACCOMPANYING
00024      DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS".
00025      REGENTS HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
00026      ENHANCEMENTS, OR MODIFICATIONS.
00027 
00028 The OpenSound Control WWW page is 
00029     http://www.cnmat.berkeley.edu/OpenSoundControl
00030 */
00031 
00032 /*
00033 
00034  OSC_timeTag.c: library for manipulating OSC time tags
00035  Matt Wright, 5/29/97
00036 
00037  Version 0.2 (9/11/98): cleaned up so no explicit type names in the .c file.
00038 
00039 */
00040 
00041 #include "OSC-timetag.h"
00042 
00043 
00044 #ifdef HAS8BYTEINT
00045 #define TWO_TO_THE_32_FLOAT 4294967296.0f
00046 
00047 OSCTimeTag OSCTT_Immediately(void) {
00048     return (OSCTimeTag) 1;
00049 }
00050 
00051 OSCTimeTag OSCTT_BiggestPossibleTimeTag(void) {
00052     return (OSCTimeTag) 0xffffffffffffffff;
00053 }
00054 
00055 OSCTimeTag OSCTT_PlusSeconds(OSCTimeTag original, float secondsOffset) {
00056     int64 offset = (int64) (secondsOffset * TWO_TO_THE_32_FLOAT);
00057 
00058 /*    printf("* OSCTT_PlusSeconds %llx plus %f seconds (i.e., %lld offset) is %llx\n", original,
00059               secondsOffset, offset, original + offset);  */
00060 
00061     return original + offset;
00062 }
00063 
00064 int OSCTT_Compare(OSCTimeTag left, OSCTimeTag right) {
00065 #if 0
00066     printf("***** OSCTT_Compare(%llx, %llx): %d\n", left, right,
00067            (left<right) ? -1 : ((left == right) ? 0 : 1));
00068 #endif
00069     if (left < right) {
00070         return -1;
00071     } else if (left == right) {
00072         return 0;
00073     } else {
00074         return 1;
00075     }
00076 }
00077 
00078 #ifdef __sgi
00079 #include <sys/time.h>
00080 
00081 #define SECONDS_FROM_1900_to_1970 2208988800 /* 17 leap years */
00082 #define TWO_TO_THE_32_OVER_ONE_MILLION 4295
00083 
00084 
00085 OSCTimeTag OSCTT_CurrentTime(void) {
00086     uint64 result;
00087     uint32 usecOffset;
00088     struct timeval tv;
00089     struct timezone tz;
00090 
00091     BSDgettimeofday(&tv, &tz);
00092 
00093     /* First get the seconds right */
00094     result = (unsigned) SECONDS_FROM_1900_to_1970 + 
00095              (unsigned) tv.tv_sec - 
00096              (unsigned) 60 * tz.tz_minuteswest +
00097              (unsigned) (tz.tz_dsttime ? 3600 : 0);
00098 
00099 #if 0
00100     /* No timezone, no DST version ... */
00101     result = (unsigned) SECONDS_FROM_1900_to_1970 + 
00102              (unsigned) tv.tv_sec;
00103 #endif
00104 
00105 
00106     /* make seconds the high-order 32 bits */
00107     result = result << 32;
00108         
00109     /* Now get the fractional part. */
00110     usecOffset = (unsigned) tv.tv_usec * (unsigned) TWO_TO_THE_32_OVER_ONE_MILLION;
00111     /* printf("** %ld microsec is offset %x\n", tv.tv_usec, usecOffset); */
00112 
00113     result += usecOffset;
00114 
00115 /*    printf("* OSCTT_CurrentTime is %llx\n", result); */
00116     return result;
00117 }
00118 
00119 #else /* __sgi */
00120 
00121 /* Instead of asking your operating system what time it is, it might be
00122    clever to find out the current time at the instant your application 
00123    starts audio processing, and then keep track of the number of samples
00124    output to know how much time has passed. */
00125 
00126 /* Loser version for systems that have no ability to tell the current time: */
00127 OSCTimeTag OSCTT_CurrentTime(void) {
00128     return (OSCTimeTag) 1;
00129 }
00130 
00131 #endif /* __sgi */
00132 
00133 
00134 #else /* Not HAS8BYTEINT */
00135 
00136 OSCTimeTag OSCTT_CurrentTime(void) {
00137     OSCTimeTag result;
00138     result.seconds = 0;
00139     result.fraction = 1;
00140     return result;
00141 }
00142 
00143 OSCTimeTag OSCTT_BiggestPossibleTimeTag(void) {
00144     OSCTimeTag result;
00145     result.seconds = 0xffffffff;
00146     result.fraction = 0xffffffff;
00147     return result;
00148 }
00149 
00150 OSCTimeTag OSCTT_Immediately(void) {
00151     OSCTimeTag result;
00152     result.seconds = 0;
00153     result.fraction = 1;
00154     return result;
00155 }
00156 
00157 OSCTimeTag OSCTT_PlusSeconds(OSCTimeTag original, float secondsOffset) {
00158     OSCTimeTag result;
00159     result.seconds = 0;
00160     result.fraction = 1;
00161     return result;
00162 }
00163 
00164 int OSCTT_Compare(OSCTimeTag left, OSCTimeTag right) {
00165     /* Untested! */
00166     int highResult = left.seconds - right.seconds;
00167 
00168     if (highResult != 0) return highResult;
00169 
00170     return left.fraction - right.fraction;
00171 }
00172 
00173 
00174 #endif /* HAS8BYTEINT */
00175 

Generated at Mon Oct 14 00:10:51 2002 for avrlib by doxygen1.2.8.1 written by Dimitri van Heesch, © 1997-2001