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

« back to all changes in this revision

Viewing changes to tests/openal_buffers.c

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2013-09-20 22:44:35 UTC
  • mfrom: (4.1.1 sid)
  • Revision ID: package-import@ubuntu.com-20130920224435-apuwj4fsl3fqv1a6
Tags: 1.5.6~20130920~6010666-1
* New snapshot release
* Update the list of supported architectures to the same as libv8
  (Closes: #723129)
* emlibtool has been removed from upstream.
* Fix warning syntax-error-in-dep5-copyright
* Refresh of the patches

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <stdio.h>
 
2
#include <stdlib.h>
 
3
#include <assert.h>
 
4
#ifdef EMSCRIPTEN
 
5
#include <emscripten.h>
 
6
#include <AL/al.h>
 
7
#include <AL/alc.h>
 
8
#else
 
9
#include "../system/include/AL/al.h"
 
10
#include "../system/include/AL/alc.h"
 
11
#endif
 
12
 
 
13
#define NUM_BUFFERS 4
 
14
#define BUFFER_SIZE 1470*10
 
15
 
 
16
ALCdevice* device = NULL;
 
17
ALCcontext* context = NULL;
 
18
 
 
19
// Audio source state.
 
20
unsigned char* data = NULL;
 
21
unsigned int size = 0;
 
22
unsigned int offset = 0;
 
23
unsigned int channels = 0;
 
24
unsigned int frequency = 0;
 
25
unsigned int bits = 0;
 
26
ALenum format = 0;
 
27
ALuint source = 0;
 
28
 
 
29
void iter(void *arg) {
 
30
  ALuint buffer = 0;
 
31
  ALint buffersProcessed = 0;
 
32
  ALint buffersWereQueued = 0;
 
33
  ALint buffersQueued = 0;
 
34
  ALint state;
 
35
 
 
36
  alGetSourcei(source, AL_BUFFERS_PROCESSED, &buffersProcessed);
 
37
 
 
38
  while (offset < size && buffersProcessed--) {
 
39
    // unqueue the old buffer and validate the queue length
 
40
    alGetSourcei(source, AL_BUFFERS_QUEUED, &buffersWereQueued);
 
41
    alSourceUnqueueBuffers(source, 1, &buffer);
 
42
 
 
43
    assert(alGetError() == AL_NO_ERROR);
 
44
    int len = size - offset;
 
45
    if (len > BUFFER_SIZE) {
 
46
      len = BUFFER_SIZE;
 
47
    }
 
48
 
 
49
    alGetSourcei(source, AL_BUFFERS_QUEUED, &buffersQueued);
 
50
    assert(buffersQueued == buffersWereQueued - 1);
 
51
 
 
52
    // queue the new buffer and validate the queue length
 
53
    buffersWereQueued = buffersQueued;
 
54
    alBufferData(buffer, format, &data[offset], len, frequency);
 
55
 
 
56
    alSourceQueueBuffers(source, 1, &buffer);
 
57
    assert(alGetError() == AL_NO_ERROR);
 
58
 
 
59
    alGetSourcei(source, AL_BUFFERS_QUEUED, &buffersQueued);
 
60
    assert(buffersQueued == buffersWereQueued + 1);
 
61
 
 
62
    // make sure it's still playing
 
63
    alGetSourcei(source, AL_SOURCE_STATE, &state);
 
64
    assert(state == AL_PLAYING);
 
65
 
 
66
    offset += len;
 
67
  }
 
68
 
 
69
  // Exit once we've processed the entire clip.
 
70
  if (offset >= size) {
 
71
#ifdef EMSCRIPTEN
 
72
    int result = 0;
 
73
    REPORT_RESULT();
 
74
#endif
 
75
    exit(0);
 
76
  }
 
77
}
 
78
 
 
79
int main(int argc, char* argv[]) {
 
80
  //
 
81
  // Setup the AL context.
 
82
  //
 
83
  device = alcOpenDevice(NULL);
 
84
  context = alcCreateContext(device, NULL);
 
85
  alcMakeContextCurrent(context);
 
86
 
 
87
  //
 
88
  // Read in the audio sample.
 
89
  //
 
90
#ifdef EMSCRIPTEN
 
91
  FILE* fp = fopen("the_entertainer.wav", "rb");
 
92
#else
 
93
  FILE* fp = fopen("sounds/the_entertainer.wav", "rb");
 
94
#endif
 
95
  fseek(fp, 0, SEEK_END);
 
96
  size = ftell(fp);
 
97
  fseek(fp, 0, SEEK_SET);
 
98
 
 
99
  data = (unsigned char*)malloc(size);
 
100
  fread(data, size, 1, fp);
 
101
  fclose(fp);
 
102
 
 
103
  offset = 12; // ignore the RIFF header
 
104
  offset += 8; // ignore the fmt header
 
105
  offset += 2; // ignore the format type
 
106
 
 
107
  channels = data[offset + 1] << 8;
 
108
  channels |= data[offset];
 
109
  offset += 2;
 
110
  printf("Channels: %u\n", channels);
 
111
 
 
112
  frequency = data[offset + 3] << 24;
 
113
  frequency |= data[offset + 2] << 16;
 
114
  frequency |= data[offset + 1] << 8;
 
115
  frequency |= data[offset];
 
116
  offset += 4;
 
117
  printf("Frequency: %u\n", frequency);
 
118
 
 
119
  offset += 6; // ignore block size and bps
 
120
 
 
121
  bits = data[offset + 1] << 8;
 
122
  bits |= data[offset];
 
123
  offset += 2;
 
124
  printf("Bits: %u\n", bits);
 
125
 
 
126
  format = 0;
 
127
  if (bits == 8) {
 
128
    if (channels == 1) {
 
129
      format = AL_FORMAT_MONO8;
 
130
    } else if (channels == 2) {
 
131
      format = AL_FORMAT_STEREO8;
 
132
    }
 
133
  } else if (bits == 16) {
 
134
    if (channels == 1) {
 
135
      format = AL_FORMAT_MONO16;
 
136
    } else if (channels == 2) {
 
137
      format = AL_FORMAT_STEREO16;
 
138
    }
 
139
  }
 
140
  offset += 8; // ignore the data chunk
 
141
 
 
142
  //
 
143
  // Seed the buffers with some initial data.
 
144
  //
 
145
  ALuint buffers[NUM_BUFFERS];
 
146
  alGenBuffers(NUM_BUFFERS, buffers);
 
147
  alGenSources(1, &source);
 
148
 
 
149
  ALint numBuffers = 0;
 
150
  while (numBuffers < NUM_BUFFERS && offset < size) {
 
151
    int len = size - offset;
 
152
    if (len > BUFFER_SIZE) {
 
153
      len = BUFFER_SIZE;
 
154
    }
 
155
 
 
156
    alBufferData(buffers[numBuffers], format, &data[offset], len, frequency);
 
157
    alSourceQueueBuffers(source, 1, &buffers[numBuffers]);
 
158
    assert(alGetError() == AL_NO_ERROR);
 
159
 
 
160
    offset += len;
 
161
    numBuffers++;
 
162
  }
 
163
 
 
164
  //
 
165
  // Start playing the source.
 
166
  //
 
167
  alSourcePlay(source);
 
168
 
 
169
  ALint state;
 
170
  alGetSourcei(source, AL_SOURCE_STATE, &state);
 
171
  assert(state == AL_PLAYING);
 
172
 
 
173
  alGetSourcei(source, AL_BUFFERS_QUEUED, &numBuffers);
 
174
  assert(numBuffers == NUM_BUFFERS);
 
175
 
 
176
  //
 
177
  // Cycle and refill the buffers until we're done.
 
178
  //
 
179
#if EMSCRIPTEN
 
180
  emscripten_set_main_loop(iter, 0, 0);
 
181
#else
 
182
  while (1) {
 
183
    iter(NULL);
 
184
    usleep(16);
 
185
  }
 
186
#endif
 
187
}