summaryrefslogtreecommitdiff
path: root/src/sound_effect.cpp
blob: 4ce9db00cb60a73c6fb65d805d03938633e29272 (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
#include <vector>
#include "internal.h"


namespace audiere {

  class SingleSoundEffect : public RefImplementation<SoundEffect> {
  public:
    SingleSoundEffect(OutputStream* os) {
      m_stream = os;

      m_volume = 1;
      m_pan    = 0;
      m_shift  = 1;
    }

    void ADR_CALL play() {
      m_stream->reset();
      m_stream->setVolume(m_volume);
      m_stream->setPan(m_pan);
      m_stream->setPitchShift(m_shift);
      m_stream->play();
    }

    void ADR_CALL stop() {
      m_stream->stop();
      m_stream->reset();
    }

    void ADR_CALL setVolume(float volume) {
      m_volume = volume;
    }

    float ADR_CALL getVolume() {
      return m_volume;
    }

    void ADR_CALL setPan(float pan) {
      m_pan = pan;
    }

    float ADR_CALL getPan() {
      return m_pan;
    }

    void ADR_CALL setPitchShift(float shift) {
      m_shift = shift;
    }

    float ADR_CALL getPitchShift() {
      return m_shift;
    }

  private:
    OutputStreamPtr m_stream;

    float m_volume;
    float m_pan;
    float m_shift;
  };


  class MultipleSoundEffect : public RefImplementation<SoundEffect> {
  public:
    MultipleSoundEffect(AudioDevice* device, SampleBuffer* sb) {
      m_device = device;
      m_buffer = sb;

      m_volume = 1;
      m_pan = 0;
      m_shift = 1;
    }

    void ADR_CALL play() {
      // go through the list and see if any streams are done playing
      // so we can reuse them
      for (unsigned i = 0; i < m_streams.size(); ++i) {
        if (!m_streams[i]->isPlaying()) {
          m_streams[i]->reset();
          m_streams[i]->setVolume(m_volume);
          m_streams[i]->setPan(m_pan);
          m_streams[i]->setPitchShift(m_shift);
          m_streams[i]->play();
          return;
        }
      }

      // open a new stream and play it
      OutputStream* stream = m_device->openStream(m_buffer->openStream());
      if (!stream) {
        return;
      }
      stream->setVolume(m_volume);
      stream->setPan(m_pan);
      stream->setPitchShift(m_shift);
      stream->play();

      m_streams.push_back(stream);
    }

    void ADR_CALL stop() {
      m_streams.clear();
    }

    void ADR_CALL setVolume(float volume) {
      m_volume = volume;
    }

    float ADR_CALL getVolume() {
      return m_volume;
    }

    void ADR_CALL setPan(float pan) {
      m_pan = pan;
    }

    float ADR_CALL getPan() {
      return m_pan;
    }

    void ADR_CALL setPitchShift(float shift) {
      m_shift = shift;
    }

    float ADR_CALL getPitchShift() {
      return m_shift;
    }
    
  private:
    AudioDevicePtr m_device;
    SampleBufferPtr m_buffer;
    std::vector<OutputStreamPtr> m_streams;

    float m_volume;
    float m_pan;
    float m_shift;
  };


  ADR_EXPORT(SoundEffect*) AdrOpenSoundEffect(
    AudioDevice* device,
    SampleSource* source,
    SoundEffectType type)
  {
    if (!device || !source) {
      return 0;
    }

    switch (type) {
      case SINGLE: {
        OutputStream* os = OpenSound(device, source, false);
        return (os ? new SingleSoundEffect(os) : 0);
      }
        
      case MULTIPLE: {
        SampleBuffer* sb = CreateSampleBuffer(source);
        return (sb ? new MultipleSoundEffect(device, sb) : 0);
      }

      default:
        return 0;
    }
  }

}