dummy.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #include "config.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <limits.h>
  6. #include "alMain.h"
  7. #include "alError.h"
  8. #include "evtqueue.h"
  9. #include "rwlock.h"
  10. #include "alu.h"
  11. #include "midi/base.h"
  12. typedef struct DSynth {
  13. DERIVE_FROM_TYPE(MidiSynth);
  14. } DSynth;
  15. static void DSynth_Construct(DSynth *self, ALCdevice *device);
  16. static DECLARE_FORWARD(DSynth, MidiSynth, void, Destruct)
  17. static DECLARE_FORWARD3(DSynth, MidiSynth, ALenum, selectSoundfonts, ALCcontext*, ALsizei, const ALuint*)
  18. static DECLARE_FORWARD1(DSynth, MidiSynth, void, setGain, ALfloat)
  19. static DECLARE_FORWARD1(DSynth, MidiSynth, void, setState, ALenum)
  20. static DECLARE_FORWARD(DSynth, MidiSynth, void, stop)
  21. static DECLARE_FORWARD(DSynth, MidiSynth, void, reset)
  22. static DECLARE_FORWARD1(DSynth, MidiSynth, void, update, ALCdevice*)
  23. static void DSynth_process(DSynth *self, ALuint SamplesToDo, ALfloat (*restrict DryBuffer)[BUFFERSIZE]);
  24. static void DSynth_Delete(DSynth *self);
  25. DEFINE_MIDISYNTH_VTABLE(DSynth);
  26. static void DSynth_Construct(DSynth *self, ALCdevice *device)
  27. {
  28. MidiSynth_Construct(STATIC_CAST(MidiSynth, self), device);
  29. SET_VTABLE2(DSynth, MidiSynth, self);
  30. }
  31. static void DSynth_processQueue(DSynth *self, ALuint64 time)
  32. {
  33. EvtQueue *queue = &STATIC_CAST(MidiSynth, self)->EventQueue;
  34. while(queue->pos < queue->size && queue->events[queue->pos].time <= time)
  35. queue->pos++;
  36. }
  37. static void DSynth_process(DSynth *self, ALuint SamplesToDo, ALfloatBUFFERSIZE*restrict UNUSED(DryBuffer))
  38. {
  39. MidiSynth *synth = STATIC_CAST(MidiSynth, self);
  40. if(synth->State != AL_PLAYING)
  41. return;
  42. synth->SamplesSinceLast += SamplesToDo;
  43. synth->SamplesToNext -= SamplesToDo;
  44. while(synth->SamplesToNext < 1.0f)
  45. {
  46. ALuint64 time = synth->NextEvtTime;
  47. if(time == UINT64_MAX)
  48. {
  49. synth->SamplesToNext = 0.0;
  50. break;
  51. }
  52. synth->SamplesSinceLast -= (time - synth->LastEvtTime) * synth->SamplesPerTick;
  53. synth->SamplesSinceLast = maxd(synth->SamplesSinceLast, 0.0);
  54. synth->LastEvtTime = time;
  55. DSynth_processQueue(self, time);
  56. synth->NextEvtTime = MidiSynth_getNextEvtTime(synth);
  57. if(synth->NextEvtTime != UINT64_MAX)
  58. synth->SamplesToNext += (synth->NextEvtTime - synth->LastEvtTime) * synth->SamplesPerTick;
  59. }
  60. }
  61. static void DSynth_Delete(DSynth *self)
  62. {
  63. free(self);
  64. }
  65. MidiSynth *DSynth_create(ALCdevice *device)
  66. {
  67. DSynth *synth = calloc(1, sizeof(*synth));
  68. if(!synth)
  69. {
  70. ERR("Failed to allocate DSynth\n");
  71. return NULL;
  72. }
  73. DSynth_Construct(synth, device);
  74. return STATIC_CAST(MidiSynth, synth);
  75. }