Skip to content

Commit

Permalink
Add a Loop source.
Browse files Browse the repository at this point in the history
  • Loading branch information
Blaž Hrastnik committed Jul 17, 2016
1 parent 475afe6 commit 30be500
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "moon/audio/libsoundio/source.hxx"
#include "moon/audio/libsoundio/music.hxx"
#include "moon/audio/libsoundio/sound.hxx"
#include "moon/audio/libsoundio/loop.hxx"

namespace Moon {
class Handle {
Expand Down
29 changes: 29 additions & 0 deletions modules/audio-libsoundio/include/moon/audio/libsoundio/loop.hxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#ifndef MOON_AUDIO_LIBSOUNDIO_LOOP_H
#define MOON_AUDIO_LIBSOUNDIO_LOOP_H

#include "moon/intern.h"
#include <string>
#include <sndfile.hh>
#include "moon/audio/libsoundio/source.hxx"
#include "moon/audio/libsoundio/mixer.hxx"

namespace Moon
{
class Loop : public Source {
public:
Loop(Moon::Source* source, std::uint32_t trigger, std::uint32_t target);
virtual ~Loop();

int read(float* dst, int frames);
std::uint32_t seek(std::uint32_t pos);

int channels();
int sampleRate();
private:
std::uint32_t trigger;
std::uint32_t target;
Moon::Source* source; // TODO:mrb_sound/music sources will need to be wrapped... if a source deallocates, the handle will be broken (shared_ptr)
};
};

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ namespace Moon
class Source {
public:
virtual int read(float* dst, int frames) = 0;
virtual std:uint32_t seek(std::uint32_t pos);
virtual int tell() { return seek(0); };
virtual std::uint32_t seek(std::uint32_t pos);
virtual std::uint32_t tell() { return seek(0); };
//
virtual int channels() = 0;
virtual int sampleRate() = 0;
Expand Down
47 changes: 47 additions & 0 deletions modules/audio-libsoundio/src/loop.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include "moon/intern.h"
#include "moon/audio/libsoundio/music.hxx"

namespace Moon
{
Loop::Loop(Moon::Source* source, std::uint32_t trigger, std::uint32_t target) {
this->source = source;
this->trigger = trigger;
this->target = target;

// TODO: bounds checking (trigger/target being further than file length)
};

Loop::~Loop() {
}

int Loop::channels() {
return source->channels();
}

int Loop::sampleRate() {
return source->sampleRate();
}

// returns how many frames we actually read
int Loop::read(float* dst, int frames)
{
std::uint32_t current = source->tell();
std::uint32_t diff = trigger - current;

// frames fit without seeking
if (frames <= diff) {
return source->read(dst, frames);
} else { // we need to read then seek
source->read(dst, diff);
source->seek(target);
dst += diff * source->channels();
source->read(dst, frames);
}
return frames;
}

// seeks to a given offset (in frames) from the start of the file
std::uint32_t Loop::seek(std::uint32_t pos) {
return source->seek(pos);
}
}

0 comments on commit 30be500

Please sign in to comment.