~ubuntu-branches/ubuntu/vivid/emscripten/vivid

« back to all changes in this revision

Viewing changes to tests/freealut/src/alutUtil.c

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2013-05-02 13:11:51 UTC
  • Revision ID: package-import@ubuntu.com-20130502131151-q8dvteqr1ef2x7xz
Tags: upstream-1.4.1~20130504~adb56cb
ImportĀ upstreamĀ versionĀ 1.4.1~20130504~adb56cb

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 alutSleep(ALfloat duration)
 
15
{
 
16
  if (duration < 0)
 
17
  {
 
18
    _alutSetError(ALUT_ERROR_INVALID_VALUE);
 
19
    return AL_FALSE;
 
20
  }
 
21
 
 
22
  {
 
23
    ALuint seconds = (ALuint) duration;
 
24
    ALfloat rest = duration - (ALfloat) seconds;
 
25
 
 
26
#if HAVE_NANOSLEEP && HAVE_TIME_H
 
27
 
 
28
    ALuint microSecs = (ALuint) (rest * 1000000);
 
29
    struct timespec t, remainingTime;
 
30
 
 
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 *_alutMalloc(size_t size)
 
71
{
 
72
  ALvoid *ptr = malloc(size == 0 ? 1 : size);
 
73
 
 
74
  if (ptr == NULL)
 
75
  {
 
76
    _alutSetError(ALUT_ERROR_OUT_OF_MEMORY);
 
77
  }
 
78
  return ptr;
 
79
}
 
80
 
 
81
ALboolean _alutFormatConstruct(ALint numChannels, ALint bitsPerSample, ALenum * format)
 
82
{
 
83
  switch (numChannels)
 
84
  {
 
85
  case 1:
 
86
    switch (bitsPerSample)
 
87
    {
 
88
    case 8:
 
89
      *format = AL_FORMAT_MONO8;
 
90
      return AL_TRUE;
 
91
    case 16:
 
92
      *format = AL_FORMAT_MONO16;
 
93
      return AL_TRUE;
 
94
    }
 
95
    break;
 
96
  case 2:
 
97
    switch (bitsPerSample)
 
98
    {
 
99
    case 8:
 
100
      *format = AL_FORMAT_STEREO8;
 
101
      return AL_TRUE;
 
102
    case 16:
 
103
      *format = AL_FORMAT_STEREO16;
 
104
      return AL_TRUE;
 
105
    }
 
106
    break;
 
107
  }
 
108
  return AL_FALSE;
 
109
}
 
110
 
 
111
ALboolean _alutFormatGetNumChannels(ALenum format, ALint * numChannels)
 
112
{
 
113
  switch (format)
 
114
  {
 
115
  case AL_FORMAT_MONO8:
 
116
  case AL_FORMAT_MONO16:
 
117
    *numChannels = 1;
 
118
    return AL_TRUE;
 
119
  case AL_FORMAT_STEREO8:
 
120
  case AL_FORMAT_STEREO16:
 
121
    *numChannels = 2;
 
122
    return AL_TRUE;
 
123
  }
 
124
  return AL_FALSE;
 
125
}
 
126
 
 
127
ALboolean _alutFormatGetBitsPerSample(ALenum format, ALint * bitsPerSample)
 
128
{
 
129
  switch (format)
 
130
  {
 
131
  case AL_FORMAT_MONO8:
 
132
  case AL_FORMAT_STEREO8:
 
133
    *bitsPerSample = 8;
 
134
    return AL_TRUE;
 
135
  case AL_FORMAT_MONO16:
 
136
  case AL_FORMAT_STEREO16:
 
137
    *bitsPerSample = 16;
 
138
    return AL_TRUE;
 
139
  }
 
140
  return AL_FALSE;
 
141
}