summaryrefslogtreecommitdiff
path: root/src/basic_source.cpp
blob: 19c705253bff4a03d699c86a917712a0888f36bd (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
#include "basic_source.h"
#include "debug.h"
#include "utility.h"


namespace audiere {

  BasicSource::BasicSource() {
    m_repeat = false;
  }

  int
  BasicSource::read(int frame_count, void* buffer) {
    if (m_repeat) {
      const int frame_size = GetFrameSize(this);

      // the main read loop:
      u8* out = (u8*)buffer;
      int frames_left = frame_count;
      while (frames_left > 0) {

        // read some frames.  if we can't read anything, reset the stream
        // and try again.
        int frames_read = doRead(frames_left, out);
        if (frames_read == 0) {
          reset();
          frames_read = doRead(frames_left, out);

          // if we still can't read anything, we're done
          if (frames_read == 0) {
            ADR_LOG("Can't read any samples even after reset");
            break;
          }
        }

        frames_left -= frames_read;
        out += frames_read * frame_size;
      }

      return frame_count - frames_left;

    } else {

      return doRead(frame_count, buffer);

    }
  }

}