[14] | 1 | #include "alutInternal.h" |
---|
| 2 | |
---|
| 3 | #if HAVE_NANOSLEEP && HAVE_TIME_H |
---|
| 4 | #include <time.h> |
---|
| 5 | #include <errno.h> |
---|
| 6 | #elif HAVE_USLEEP && HAVE_UNISTD_H |
---|
| 7 | #include <unistd.h> |
---|
| 8 | #elif HAVE_SLEEP && HAVE_WINDOWS_H |
---|
| 9 | #include <windows.h> |
---|
| 10 | #else |
---|
| 11 | #error No way to sleep on this platform |
---|
| 12 | #endif |
---|
| 13 | |
---|
| 14 | ALboolean |
---|
| 15 | alutSleep (ALfloat duration) |
---|
| 16 | { |
---|
| 17 | if (duration < 0) |
---|
| 18 | { |
---|
| 19 | _alutSetError (ALUT_ERROR_INVALID_VALUE); |
---|
| 20 | return AL_FALSE; |
---|
| 21 | } |
---|
| 22 | |
---|
| 23 | { |
---|
| 24 | ALuint seconds = (ALuint) duration; |
---|
| 25 | ALfloat rest = duration - (ALfloat) seconds; |
---|
| 26 | |
---|
| 27 | #if HAVE_NANOSLEEP && HAVE_TIME_H |
---|
| 28 | |
---|
| 29 | ALuint microSecs = (ALuint) (rest * 1000000); |
---|
| 30 | struct timespec t, remainingTime; |
---|
| 31 | t.tv_sec = (time_t) seconds; |
---|
| 32 | t.tv_nsec = ((long) microSecs) * 1000; |
---|
| 33 | |
---|
| 34 | /* At least the interaction of nanosleep and signals is specified! */ |
---|
| 35 | while (nanosleep (&t, &remainingTime) < 0) |
---|
| 36 | { |
---|
| 37 | if (errno != EINTR) |
---|
| 38 | { |
---|
| 39 | return AL_FALSE; |
---|
| 40 | } |
---|
| 41 | /* If we received a signal, let's try again with the remaining time. */ |
---|
| 42 | t.tv_sec = remainingTime.tv_sec; |
---|
| 43 | t.tv_nsec = remainingTime.tv_nsec; |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | #elif HAVE_USLEEP && HAVE_UNISTD_H |
---|
| 47 | |
---|
| 48 | while (seconds > 0) |
---|
| 49 | { |
---|
| 50 | usleep (1000000); |
---|
| 51 | seconds--; |
---|
| 52 | } |
---|
| 53 | usleep ((unsigned int) (rest * 1000000)); |
---|
| 54 | |
---|
| 55 | #elif HAVE_SLEEP && HAVE_WINDOWS_H |
---|
| 56 | |
---|
| 57 | while (seconds > 0) |
---|
| 58 | { |
---|
| 59 | Sleep (1000); |
---|
| 60 | seconds--; |
---|
| 61 | } |
---|
| 62 | Sleep ((DWORD) (rest * 1000)); |
---|
| 63 | |
---|
| 64 | #endif |
---|
| 65 | |
---|
| 66 | } |
---|
| 67 | return AL_TRUE; |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | ALvoid * |
---|
| 71 | _alutMalloc (size_t size) |
---|
| 72 | { |
---|
| 73 | ALvoid *ptr = malloc (size == 0 ? 1 : size); |
---|
| 74 | if (ptr == NULL) |
---|
| 75 | { |
---|
| 76 | _alutSetError (ALUT_ERROR_OUT_OF_MEMORY); |
---|
| 77 | } |
---|
| 78 | return ptr; |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | ALboolean |
---|
| 82 | _alutFormatConstruct (ALint numChannels, ALint bitsPerSample, ALenum *format) |
---|
| 83 | { |
---|
| 84 | switch (numChannels) |
---|
| 85 | { |
---|
| 86 | case 1: |
---|
| 87 | switch (bitsPerSample) |
---|
| 88 | { |
---|
| 89 | case 8: |
---|
| 90 | *format = AL_FORMAT_MONO8; |
---|
| 91 | return AL_TRUE; |
---|
| 92 | case 16: |
---|
| 93 | *format = AL_FORMAT_MONO16; |
---|
| 94 | return AL_TRUE; |
---|
| 95 | } |
---|
| 96 | break; |
---|
| 97 | case 2: |
---|
| 98 | switch (bitsPerSample) |
---|
| 99 | { |
---|
| 100 | case 8: |
---|
| 101 | *format = AL_FORMAT_STEREO8; |
---|
| 102 | return AL_TRUE; |
---|
| 103 | case 16: |
---|
| 104 | *format = AL_FORMAT_STEREO16; |
---|
| 105 | return AL_TRUE; |
---|
| 106 | } |
---|
| 107 | break; |
---|
| 108 | } |
---|
| 109 | return AL_FALSE; |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | ALboolean |
---|
| 113 | _alutFormatGetNumChannels (ALenum format, ALint *numChannels) |
---|
| 114 | { |
---|
| 115 | switch (format) |
---|
| 116 | { |
---|
| 117 | case AL_FORMAT_MONO8: |
---|
| 118 | case AL_FORMAT_MONO16: |
---|
| 119 | *numChannels = 1; |
---|
| 120 | return AL_TRUE; |
---|
| 121 | case AL_FORMAT_STEREO8: |
---|
| 122 | case AL_FORMAT_STEREO16: |
---|
| 123 | *numChannels = 2; |
---|
| 124 | return AL_TRUE; |
---|
| 125 | } |
---|
| 126 | return AL_FALSE; |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | ALboolean |
---|
| 130 | _alutFormatGetBitsPerSample (ALenum format, ALint *bitsPerSample) |
---|
| 131 | { |
---|
| 132 | switch (format) |
---|
| 133 | { |
---|
| 134 | case AL_FORMAT_MONO8: |
---|
| 135 | case AL_FORMAT_STEREO8: |
---|
| 136 | *bitsPerSample = 8; |
---|
| 137 | return AL_TRUE; |
---|
| 138 | case AL_FORMAT_MONO16: |
---|
| 139 | case AL_FORMAT_STEREO16: |
---|
| 140 | *bitsPerSample = 16; |
---|
| 141 | return AL_TRUE; |
---|
| 142 | } |
---|
| 143 | return AL_FALSE; |
---|
| 144 | } |
---|