Go to the documentation of this file.00001 #include "alutInternal.h"
00002
00003 #if HAVE_NANOSLEEP && HAVE_TIME_H
00004 #include <time.h>
00005 #include <errno.h>
00006 #elif HAVE_USLEEP && HAVE_UNISTD_H
00007 #include <unistd.h>
00008 #elif HAVE_SLEEP && HAVE_WINDOWS_H || WIN32
00009 #include <windows.h>
00010 #else
00011 #error No way to sleep on this platform
00012 #endif
00013
00014 ALboolean
00015 alutSleep (ALfloat duration)
00016 {
00017 if (duration < 0)
00018 {
00019 _alutSetError (ALUT_ERROR_INVALID_VALUE);
00020 return AL_FALSE;
00021 }
00022
00023 {
00024 ALuint seconds = (ALuint) duration;
00025 ALfloat rest = duration - (ALfloat) seconds;
00026
00027 #if HAVE_NANOSLEEP && HAVE_TIME_H
00028
00029 ALuint microSecs = (ALuint) (rest * 1000000);
00030 struct timespec t, remainingTime;
00031 t.tv_sec = (time_t) seconds;
00032 t.tv_nsec = ((long) microSecs) * 1000;
00033
00034
00035 while (nanosleep (&t, &remainingTime) < 0)
00036 {
00037 if (errno != EINTR)
00038 {
00039 return AL_FALSE;
00040 }
00041
00042 t.tv_sec = remainingTime.tv_sec;
00043 t.tv_nsec = remainingTime.tv_nsec;
00044 }
00045
00046 #elif HAVE_USLEEP && HAVE_UNISTD_H
00047
00048 while (seconds > 0)
00049 {
00050 usleep (1000000);
00051 seconds--;
00052 }
00053 usleep ((unsigned int) (rest * 1000000));
00054
00055 #elif HAVE_SLEEP && HAVE_WINDOWS_H
00056
00057 while (seconds > 0)
00058 {
00059 Sleep (1000);
00060 seconds--;
00061 }
00062 Sleep ((DWORD) (rest * 1000));
00063
00064 #endif
00065
00066 }
00067 return AL_TRUE;
00068 }
00069
00070 ALvoid *
00071 _alutMalloc (size_t size)
00072 {
00073 ALvoid *ptr = malloc (size == 0 ? 1 : size);
00074 if (ptr == NULL)
00075 {
00076 _alutSetError (ALUT_ERROR_OUT_OF_MEMORY);
00077 }
00078 return ptr;
00079 }
00080
00081 ALboolean
00082 _alutFormatConstruct (ALint numChannels, ALint bitsPerSample, ALenum *format)
00083 {
00084 switch (numChannels)
00085 {
00086 case 1:
00087 switch (bitsPerSample)
00088 {
00089 case 8:
00090 *format = AL_FORMAT_MONO8;
00091 return AL_TRUE;
00092 case 16:
00093 *format = AL_FORMAT_MONO16;
00094 return AL_TRUE;
00095 }
00096 break;
00097 case 2:
00098 switch (bitsPerSample)
00099 {
00100 case 8:
00101 *format = AL_FORMAT_STEREO8;
00102 return AL_TRUE;
00103 case 16:
00104 *format = AL_FORMAT_STEREO16;
00105 return AL_TRUE;
00106 }
00107 break;
00108 }
00109 return AL_FALSE;
00110 }
00111
00112 ALboolean
00113 _alutFormatGetNumChannels (ALenum format, ALint *numChannels)
00114 {
00115 switch (format)
00116 {
00117 case AL_FORMAT_MONO8:
00118 case AL_FORMAT_MONO16:
00119 *numChannels = 1;
00120 return AL_TRUE;
00121 case AL_FORMAT_STEREO8:
00122 case AL_FORMAT_STEREO16:
00123 *numChannels = 2;
00124 return AL_TRUE;
00125 }
00126 return AL_FALSE;
00127 }
00128
00129 ALboolean
00130 _alutFormatGetBitsPerSample (ALenum format, ALint *bitsPerSample)
00131 {
00132 switch (format)
00133 {
00134 case AL_FORMAT_MONO8:
00135 case AL_FORMAT_STEREO8:
00136 *bitsPerSample = 8;
00137 return AL_TRUE;
00138 case AL_FORMAT_MONO16:
00139 case AL_FORMAT_STEREO16:
00140 *bitsPerSample = 16;
00141 return AL_TRUE;
00142 }
00143 return AL_FALSE;
00144 }