File tree Expand file tree Collapse file tree 4 files changed +79
-2
lines changed
include/moon/audio/libsoundio Expand file tree Collapse file tree 4 files changed +79
-2
lines changed Original file line number Diff line number Diff line change 77#include " moon/audio/libsoundio/source.hxx"
88#include " moon/audio/libsoundio/music.hxx"
99#include " moon/audio/libsoundio/sound.hxx"
10+ #include " moon/audio/libsoundio/loop.hxx"
1011
1112namespace Moon {
1213 class Handle {
Original file line number Diff line number Diff line change 1+ #ifndef MOON_AUDIO_LIBSOUNDIO_LOOP_H
2+ #define MOON_AUDIO_LIBSOUNDIO_LOOP_H
3+
4+ #include " moon/intern.h"
5+ #include < string>
6+ #include < sndfile.hh>
7+ #include " moon/audio/libsoundio/source.hxx"
8+ #include " moon/audio/libsoundio/mixer.hxx"
9+
10+ namespace Moon
11+ {
12+ class Loop : public Source {
13+ public:
14+ Loop (Moon::Source* source, std::uint32_t trigger, std::uint32_t target);
15+ virtual ~Loop ();
16+
17+ int read (float * dst, int frames);
18+ std::uint32_t seek (std::uint32_t pos);
19+
20+ int channels ();
21+ int sampleRate ();
22+ private:
23+ std::uint32_t trigger;
24+ std::uint32_t target;
25+ Moon::Source* source; // TODO:mrb_sound/music sources will need to be wrapped... if a source deallocates, the handle will be broken (shared_ptr)
26+ };
27+ };
28+
29+ #endif
Original file line number Diff line number Diff line change @@ -8,8 +8,8 @@ namespace Moon
88 class Source {
99 public:
1010 virtual int read (float * dst, int frames) = 0;
11- virtual std:uint32_t seek (std::uint32_t pos);
12- virtual int tell () { return seek (0 ); };
11+ virtual std:: uint32_t seek (std::uint32_t pos);
12+ virtual std:: uint32_t tell () { return seek (0 ); };
1313 //
1414 virtual int channels () = 0;
1515 virtual int sampleRate () = 0;
Original file line number Diff line number Diff line change 1+ #include " moon/intern.h"
2+ #include " moon/audio/libsoundio/music.hxx"
3+
4+ namespace Moon
5+ {
6+ Loop::Loop (Moon::Source* source, std::uint32_t trigger, std::uint32_t target) {
7+ this ->source = source;
8+ this ->trigger = trigger;
9+ this ->target = target;
10+
11+ // TODO: bounds checking (trigger/target being further than file length)
12+ };
13+
14+ Loop::~Loop () {
15+ }
16+
17+ int Loop::channels () {
18+ return source->channels ();
19+ }
20+
21+ int Loop::sampleRate () {
22+ return source->sampleRate ();
23+ }
24+
25+ // returns how many frames we actually read
26+ int Loop::read (float * dst, int frames)
27+ {
28+ std::uint32_t current = source->tell ();
29+ std::uint32_t diff = trigger - current;
30+
31+ // frames fit without seeking
32+ if (frames <= diff) {
33+ return source->read (dst, frames);
34+ } else { // we need to read then seek
35+ source->read (dst, diff);
36+ source->seek (target);
37+ dst += diff * source->channels ();
38+ source->read (dst, frames);
39+ }
40+ return frames;
41+ }
42+
43+ // seeks to a given offset (in frames) from the start of the file
44+ std::uint32_t Loop::seek (std::uint32_t pos) {
45+ return source->seek (pos);
46+ }
47+ }
You can’t perform that action at this time.
0 commit comments