File: alutBufferData.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 (147 lines) | stat: -rw-r--r-- 3,117 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
145
146
147
#include "alutInternal.h"

struct BufferData_struct
{
  ALvoid *data;
  size_t length;
  ALint numChannels;
  ALint bitsPerSample;
  ALfloat sampleFrequency;
};

BufferData *
_alutBufferDataConstruct (ALvoid *data, size_t length, ALint numChannels,
                          ALint bitsPerSample, ALfloat sampleFrequency)
{
  BufferData *bufferData = (BufferData *) _alutMalloc (sizeof (BufferData));
  if (bufferData == NULL)
    {
      return NULL;
    }

  bufferData->data = data;
  bufferData->length = length;
  bufferData->numChannels = numChannels;
  bufferData->bitsPerSample = bitsPerSample;
  bufferData->sampleFrequency = sampleFrequency;

  return bufferData;
}

ALboolean
_alutBufferDataDestroy (BufferData *bufferData)
{
  if (bufferData->data != NULL)
    {
      free (bufferData->data);
    }
  free (bufferData);
  return AL_TRUE;
}

ALvoid *
_alutBufferDataGetData (const BufferData *bufferData)
{
  return bufferData->data;
}

void
_alutBufferDataDetachData (BufferData *bufferData)
{
  bufferData->data = NULL;
}

size_t
_alutBufferDataGetLength (const BufferData *bufferData)
{
  return bufferData->length;
}

static ALint
getNumChannels (const BufferData *bufferData)
{
  return bufferData->numChannels;
}

static ALint
getBitsPerSample (const BufferData *bufferData)
{
  return bufferData->bitsPerSample;
}

ALfloat
_alutBufferDataGetSampleFrequency (const BufferData *bufferData)
{
  return bufferData->sampleFrequency;
}

/****************************************************************************
 * The utility functions below do not know the internal BufferData
 * representation.
 ****************************************************************************/

ALboolean
_alutGetFormat (const BufferData *bufferData, ALenum *format)
{
  if (!_alutFormatConstruct
      (getNumChannels (bufferData), getBitsPerSample (bufferData), format))
    {
      _alutSetError (ALUT_ERROR_UNSUPPORTED_FILE_SUBTYPE);
      return AL_FALSE;
    }
  return AL_TRUE;
}

static ALuint
generateBuffer (void)
{
  ALuint buffer;
  alGenBuffers (1, &buffer);
  if (alGetError () != AL_NO_ERROR)
    {
      _alutSetError (ALUT_ERROR_GEN_BUFFERS);
      return AL_NONE;
    }
  return buffer;
}

static ALboolean
passBufferData (BufferData *bufferData, ALuint bid)
{
  ALenum format;
  size_t size;
  ALfloat frequency;
  if (!_alutGetFormat (bufferData, &format))
    {
      return AL_FALSE;
    }
  /* GCC is a bit picky about casting function calls, so we do it in two
     steps... */
  size = _alutBufferDataGetLength (bufferData);
  frequency = _alutBufferDataGetSampleFrequency (bufferData);
  alBufferData (bid, format, _alutBufferDataGetData (bufferData),
                (ALsizei) size, (ALsizei) frequency);
  if (alGetError () != AL_NO_ERROR)
    {
      _alutSetError (ALUT_ERROR_BUFFER_DATA);
      return AL_FALSE;
    }
  return AL_TRUE;
}

ALuint
_alutPassBufferData (BufferData *bufferData)
{
  ALuint buffer = generateBuffer ();
  if (buffer == AL_NONE)
    {
      return AL_NONE;
    }

  if (!passBufferData (bufferData, buffer))
    {
      return AL_NONE;
    }

  return buffer;
}