Skip to content

Commit 30be500

Browse files
author
Blaž Hrastnik
committed
Add a Loop source.
1 parent 475afe6 commit 30be500

File tree

4 files changed

+79
-2
lines changed

4 files changed

+79
-2
lines changed

modules/audio-libsoundio/include/moon/audio/libsoundio/handle.hxx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
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

1112
namespace Moon {
1213
class Handle {
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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

modules/audio-libsoundio/include/moon/audio/libsoundio/source.hxx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff 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;
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
}

0 commit comments

Comments
 (0)