File: alutUtil.c

package info (click to toggle)
freealut 1.1.0-6
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 2,144 kB
  • sloc: sh: 8,617; ansic: 2,738; makefile: 89
file content (144 lines) | stat: -rw-r--r-- 2,902 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include "alutInternal.h"

#if HAVE_NANOSLEEP && HAVE_TIME_H
#include <time.h>
#include <errno.h>
#elif HAVE_USLEEP && HAVE_UNISTD_H
#include <unistd.h>
#elif HAVE_SLEEP && HAVE_WINDOWS_H
#include <windows.h>
#else
#error No way to sleep on this platform
#endif

ALboolean
alutSleep (ALfloat duration)
{
  if (duration < 0)
    {
      _alutSetError (ALUT_ERROR_INVALID_VALUE);
      return AL_FALSE;
    }

  {
    ALuint seconds = (ALuint) duration;
    ALfloat rest = duration - (ALfloat) seconds;

#if HAVE_NANOSLEEP && HAVE_TIME_H

    ALuint microSecs = (ALuint) (rest * 1000000);
    struct timespec t, remainingTime;
    t.tv_sec = (time_t) seconds;
    t.tv_nsec = ((long) microSecs) * 1000;

    /* At least the interaction of nanosleep and signals is specified! */
    while (nanosleep (&t, &remainingTime) < 0)
      {
        if (errno != EINTR)
          {
            return AL_FALSE;
          }
        /* If we received a signal, let's try again with the remaining time. */
        t.tv_sec = remainingTime.tv_sec;
        t.tv_nsec = remainingTime.tv_nsec;
      }

#elif HAVE_USLEEP && HAVE_UNISTD_H

    while (seconds > 0)
      {
        usleep (1000000);
        seconds--;
      }
    usleep ((unsigned int) (rest * 1000000));

#elif HAVE_SLEEP && HAVE_WINDOWS_H

    while (seconds > 0)
      {
        Sleep (1000);
        seconds--;
      }
    Sleep ((DWORD) (rest * 1000));

#endif

  }
  return AL_TRUE;
}

ALvoid *
_alutMalloc (size_t size)
{
  ALvoid *ptr = malloc (size == 0 ? 1 : size);
  if (ptr == NULL)
    {
      _alutSetError (ALUT_ERROR_OUT_OF_MEMORY);
    }
  return ptr;
}

ALboolean
_alutFormatConstruct (ALint numChannels, ALint bitsPerSample, ALenum *format)
{
  switch (numChannels)
    {
    case 1:
      switch (bitsPerSample)
        {
        case 8:
          *format = AL_FORMAT_MONO8;
          return AL_TRUE;
        case 16:
          *format = AL_FORMAT_MONO16;
          return AL_TRUE;
        }
      break;
    case 2:
      switch (bitsPerSample)
        {
        case 8:
          *format = AL_FORMAT_STEREO8;
          return AL_TRUE;
        case 16:
          *format = AL_FORMAT_STEREO16;
          return AL_TRUE;
        }
      break;
    }
  return AL_FALSE;
}

ALboolean
_alutFormatGetNumChannels (ALenum format, ALint *numChannels)
{
  switch (format)
    {
    case AL_FORMAT_MONO8:
    case AL_FORMAT_MONO16:
      *numChannels = 1;
      return AL_TRUE;
    case AL_FORMAT_STEREO8:
    case AL_FORMAT_STEREO16:
      *numChannels = 2;
      return AL_TRUE;
    }
  return AL_FALSE;
}

ALboolean
_alutFormatGetBitsPerSample (ALenum format, ALint *bitsPerSample)
{
  switch (format)
    {
    case AL_FORMAT_MONO8:
    case AL_FORMAT_STEREO8:
      *bitsPerSample = 8;
      return AL_TRUE;
    case AL_FORMAT_MONO16:
    case AL_FORMAT_STEREO16:
      *bitsPerSample = 16;
      return AL_TRUE;
    }
  return AL_FALSE;
}