aboutsummaryrefslogtreecommitdiff
path: root/src/library_openal.js
blob: 2e2061ca65c53d6b19a3ef3cb354ae3e475ac862 (plain)
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
//"use strict";

var LibraryOpenAL = {
  $AL__deps: ['$Browser'],
  $AL: {
    contexts: [],
    currentContext: null,

    structs: {
      Rect: Runtime.generateStructInfo([
        ['i32', 'x'], ['i32', 'y'], ['i32', 'w'], ['i32', 'h'], 
      ])
    },
  },

  alcProcessContext: function(context) {},
  alcSuspendContext: function(context) {},

  alcMakeContextCurrent: function(context) {
    if (context == 0) {
      AL.currentContext = null;
    } else {
      AL.currentContext = AL.contexts[context];
    }
  },

  alcDestroyContext: function(context) {
    // Stop playback, etc
  },

  alcCloseDevice: function(device) {
    // Stop playback, etc
  },

  alcOpenDevice: function(deviceName) {
    if (typeof(AudioContext) == "function" ||
        typeof(webkitAudioContext) == "function") {
      return 1; // non-null pointer -- we just simulate one device
    } else {
      return 0;
    }
  },

  alcCreateContext: function(device, attrList) {
    if (device != 1) {
      return 0;
    }

    if (attrList) {
      console.log("The attrList argument of alcCreateContext is not supported yet");
      return 0;
    }

    var ctx;
    try {
      ctx = new AudioContext();
    } catch (e) {
      try {
        ctx = new webkitAudioContext();
      } catch (e) {}
    }

    if (ctx) {
      AL.contexts.push({ctx: ctx, err: 0, src: [], buf: []});
      return AL.contexts.length - 1;
    } else {
      return 0;
    }
  },

  alGetError: function() {
    if (!AL.currentContext) {
      return 0xA004 /* AL_INVALID_OPERATION */;
    } else {
      return AL.currentContext.err;
    }
  },

  alGenSources: function(count, sources) {
    if (!AL.currentContext) {
      console.error("alGenSources called without a valid context");
      return;
    }
    for (var i = 0; i < count; ++i) {
      var src = AL.currentContext.ctx.createBufferSource();
      var gain = AL.currentContext.ctx.createGain();
      var panner = AL.currentContext.ctx.createPanner();
      src.connect(gain);
      gain.connect(panner);
      panner.connect(AL.currentContext.destination);
      AL.currentContext.src.push({src: src, gain: gain, panner: panner});
      {{{ makeSetValue('sources', 'i', 'AL.currentContext.src.length - 1', 'i32') }}};
    }
  },

  alSourcei: function(source, param, value) {
    if (!AL.currentContext) {
      console.error("alSourcei called without a valid context");
      return;
    }
    if (source >= AL.currentContext.src.length) {
      console.error("alSourcei called with an invalid source");
      return;
    }
    switch (param) {
    case 0x1007 /* AL_LOOPING */:
      AL.currentContext.src[source].src.loop = (value != 0 /* AL_FALSE */);
      break;
    case 0x1009 /* AL_BUFFER */:
      AL.currentContext.src[source].src.buffer = AL.currentContext.buf[value].buf;
      break;
    default:
      console.log("alSourcei with param " + param + " not implemented yet");
      break;
    }
  },

  alSourcef: function(source, param, value) {
    if (!AL.currentContext) {
      consoue.error("alSourcef called without a valid context");
      return;
    }
    if (source >= AL.currentContext.src.length) {
      console.error("alSourcef called with an invalid source");
      return;
    }
    switch (param) {
    case 0x100A /* AL_GAIN */:
      AL.currentContext.src[source].gain.gain.value = value;
      break;
    case 0x1003 /* AL_PITCH */:
      console.log("alSourcef was called with AL_PITCH, but Web Audio does not support static pitch changes");
      break;
    default:
      console.log("alSourcef with param " + param + " not implemented yet");
      break;
    }
  },

  alSourceQueueBuffers: function(source, count, buffers) {
    if (!AL.currentContext) {
      console.error("alSourceQueueBuffers called without a valid context");
      return;
    }
    if (source >= AL.currentContext.src.length) {
      console.error("alSourceQueueBuffers called with an invalid source");
      return;
    }
    if (count != 1) {
      console.error("Queuing multiple buffers using alSourceQueueBuffers is not supported yet");
      return;
    }
    for (var i = 0; i < count; ++i) {
      var buffer = {{{ makeGetValue('buffers', 'i', 'i32') }}};
      if (buffer >= AL.currentContext.buf.length) {
        console.error("alSourceQueueBuffers called with an invalid buffer");
        return;
      }
      AL.currentCOntext.src[source].src.buffer = AL.currentContext.buf[buffer].buf;
    }
  },

  alGenBuffers: function(count, buffers) {
    if (!AL.currentContext) {
      console.error("alGenBuffers called without a valid context");
      return;
    }
    for (var i = 0; i < count; ++i) {
      AL.currentContext.buf.push({buf: null});
      {{{ makeSetValue('buffers', 'i', 'AL.currentContext.buf.length - 1', 'i32') }}};
    }
  },

  alBufferData: function(buffer, format, data, size, freq) {
    if (!AL.currentContext) {
      console.error("alBufferData called without a valid context");
      return;
    }
    if (buffer >= AL.currentContext.buf.length) {
      console.error("alBufferData called with an invalid buffer");
      return;
    }
    var channels, bytes;
    switch (format) {
    case 0x1100 /* AL_FORMAT_MONO8 */:
      bytes = 1;
    case 0x1101 /* AL_FORMAT_MONO16 */:
      bytes = 2;
      channels = 1;
      break;
    case 0x1102 /* AL_FORMAT_STEREO8 */:
      bytes = 1;
    case 0x1103 /* AL_FORMAT_STEREO16 */:
      bytes = 2;
      channels = 2;
      break;
    default:
      console.error("alBufferData called with invalid format " + format);
      return;
    }
    AL.currentContext.buf[buffer].buf = AL.currentContext.ctx.createBuffer(channels, size / (bytes * channels), freq);
    var data = new Array(channels);
    for (var i = 0; i < channels; ++i) {
      data[i] = AL.currentContext.buf[buffer].buf.getChannelData(i);
    }
    for (var i = 0; i < size / (bytes * channels); ++i) {
      for (var j = 0; j < channels; ++j) {
        switch (bytes) {
        case 1:
          var val = {{{ makeGetValue('data', 'i+j', 'i8') }}};
          data[j][i] = -1.0 + val * (2/256);
          break;
        case 2:
          var val = {{{ makeGetValue('data', 'i+j', 'i16') }}};
          data[j][i] = -1.0 + (val + -32768) * (2/65536);
          break;
        }
      }
    }
  },

  alDistanceModel: function(model) {
    if (model != 0 /* AL_NONE */) {
      console.log("Only alDistanceModel(AL_NONE) is currently supported");
    }
  },

  alListenerfv: function(param, values) {
    console.log("alListenerfv is not supported yet");
  },

  alIsExtensionPresent: function(extName) {
    return 0;
  },

  alcIsExtensionPresent: function(