-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Blaž Hrastnik
committed
Jul 17, 2016
1 parent
475afe6
commit 30be500
Showing
4 changed files
with
79 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
modules/audio-libsoundio/include/moon/audio/libsoundio/loop.hxx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |