Skip to content

Commit

Permalink
Get duration for Sound Players (#7747)
Browse files Browse the repository at this point in the history
  • Loading branch information
NickHardeman authored Nov 3, 2023
1 parent 120c182 commit 5c5f760
Show file tree
Hide file tree
Showing 19 changed files with 158 additions and 9 deletions.
6 changes: 5 additions & 1 deletion addons/ofxEmscripten/src/ofxEmscriptenSoundPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,11 @@ float ofxEmscriptenSoundPlayer::getVolume() const{
return html5audio_sound_volume(player_id);
}

int ofxEmscriptenSoundPlayer::getDurationMS() const{
float ofxEmscriptenSoundPlayer::getDuration() const {
return html5audio_sound_duration(player_id);
}

unsigned int ofxEmscriptenSoundPlayer::getDurationMS() const{
return html5audio_sound_duration(player_id) * 1000;
}

Expand Down
4 changes: 3 additions & 1 deletion addons/ofxEmscripten/src/ofxEmscriptenSoundPlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ class ofxEmscriptenSoundPlayer: public ofBaseSoundPlayer {
float getPan() const;
bool isLoaded() const;
float getVolume() const;
int getDurationMS() const;

float getDuration() const;
unsigned int getDurationMS() const;
double getDurationSecs() const;

static float * getSystemSpectrum(int bands);
Expand Down
3 changes: 3 additions & 0 deletions addons/ofxiOS/src/sound/AVSoundPlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,7 @@
- (void)positionMs:(int)value;
- (int)positionMs;

// total duration in seconds of player's audio
- (float)duration;

@end
7 changes: 7 additions & 0 deletions addons/ofxiOS/src/sound/AVSoundPlayer.m
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,13 @@ - (int)positionMs {
return self.player.currentTime * 1000;
}

- (float)duration {
if(self.player == nil) {
return 0.f;
}
return self.player.duration;
}

//----------------------------------------------------------- timer.
- (void)updateTimer {
if([self.delegate respondsToSelector:@selector(soundPlayerDidChange)]) {
Expand Down
11 changes: 11 additions & 0 deletions addons/ofxiOS/src/sound/ofxOpenALSoundPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ void ofxOpenALSoundPlayer::unload() {
else
SoundEngine_UnloadEffect(myId);
}
length = 0;
}

//--------------------------------------------------------------
Expand Down Expand Up @@ -368,6 +369,16 @@ bool ofxOpenALSoundPlayer::isLoaded() const{
return bLoadedOk;
}

//--------------------------------------------------------------
float ofxOpenALSoundPlayer::getDuration() const {
return (float)length / 1000.0f;
}

//--------------------------------------------------------------
unsigned int ofxOpenALSoundPlayer::getDurationMS() const {
return length;
}

//--------------------------------------------------------------


Expand Down
3 changes: 3 additions & 0 deletions addons/ofxiOS/src/sound/ofxOpenALSoundPlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ class ofxOpenALSoundPlayer : public ofBaseSoundPlayer{

bool isLoaded() const;

float getDuration() const;
unsigned int getDurationMS() const;

// IPHONE EXTRA FUNCTIONS
static void vibrate();

Expand Down
3 changes: 3 additions & 0 deletions addons/ofxiOS/src/sound/ofxiOSSoundPlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ class ofxiOSSoundPlayer : public ofBaseSoundPlayer {
float getPan() const;
bool isLoaded() const;
float getVolume() const;

float getDuration() const;
unsigned int getDurationMS() const;

void * getAVSoundPlayer();

Expand Down
13 changes: 13 additions & 0 deletions addons/ofxiOS/src/sound/ofxiOSSoundPlayer.mm
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,19 @@
return [(__bridge AVSoundPlayer *)soundPlayer volume];
}

float ofxiOSSoundPlayer::getDuration() const {
if(soundPlayer == NULL) {
return 0.f;
}
return [(__bridge AVSoundPlayer *)soundPlayer duration];
}
unsigned int ofxiOSSoundPlayer::getDurationMS() const {
if(soundPlayer == NULL) {
return 0;
}
return [(__bridge AVSoundPlayer *)soundPlayer duration] * 1000.0;
}

void * ofxiOSSoundPlayer::getAVSoundPlayer() {
return soundPlayer;
}
3 changes: 3 additions & 0 deletions libs/openFrameworks/sound/ofAVEngineSoundPlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ class ofAVEngineSoundPlayer : public ofBaseSoundPlayer {
float getPan() const;
bool isLoaded() const;
float getVolume() const;

float getDuration() const;
unsigned int getDurationMS() const;

void * getAVEnginePlayer();

Expand Down
14 changes: 14 additions & 0 deletions libs/openFrameworks/sound/ofAVEngineSoundPlayer.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1219,6 +1219,20 @@ - (float)soundDurationSeconds{
return [(AVEnginePlayer *)soundPlayer volume];
}

float ofAVEngineSoundPlayer::getDuration() const {
if(soundPlayer == NULL) {
return 0.0f;
}
return [(AVEnginePlayer *)soundPlayer soundDurationSeconds];
}

unsigned int ofAVEngineSoundPlayer::getDurationMS() const {
if(soundPlayer == NULL) {
return 0;
}
return ([(AVEnginePlayer *)soundPlayer soundDurationSeconds] * 1000.0f);
}

void * ofAVEngineSoundPlayer::getAVEnginePlayer() {
return (__bridge void *)soundPlayer;
}
Expand Down
12 changes: 12 additions & 0 deletions libs/openFrameworks/sound/ofFmodSoundPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ bool ofFmodSoundPlayer::load(const of::filesystem::path& _fileName, bool stream)
} else {
bLoadedOk = true;
FMOD_Sound_GetLength(sound, &length, FMOD_TIMEUNIT_PCM);
FMOD_Sound_GetLength(sound, &durationMS, FMOD_TIMEUNIT_MS);
isStreaming = stream;
}

Expand All @@ -222,6 +223,7 @@ bool ofFmodSoundPlayer::load(const of::filesystem::path& _fileName, bool stream)

//------------------------------------------------------------
void ofFmodSoundPlayer::unload(){
durationMS = 0;
if (bLoadedOk){
stop(); // try to stop the sound
FMOD_Sound_Release(sound);
Expand Down Expand Up @@ -259,6 +261,16 @@ bool ofFmodSoundPlayer::isLoaded() const{
return bLoadedOk;
}

//------------------------------------------------------------
float ofFmodSoundPlayer::getDuration() const {
return (float)durationMS / 1000.f;
}

//------------------------------------------------------------
unsigned int ofFmodSoundPlayer::getDurationMS() const {
return durationMS;
}

//------------------------------------------------------------
void ofFmodSoundPlayer::setVolume(float vol){
if (isPlaying()){
Expand Down
4 changes: 4 additions & 0 deletions libs/openFrameworks/sound/ofFmodSoundPlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ class ofFmodSoundPlayer : public ofBaseSoundPlayer {
float getPan() const;
float getVolume() const;
bool isLoaded() const;

float getDuration() const;
unsigned int getDurationMS() const;

static void initializeFmod();
static void closeFmod();
Expand All @@ -77,6 +80,7 @@ class ofFmodSoundPlayer : public ofBaseSoundPlayer {
float internalFreq; // 44100 ?
float speed; // -n to n, 1 = normal, -1 backwards
unsigned int length; // in samples;
unsigned int durationMS; // duration in milliseconds

FMOD_RESULT result;
FMOD_CHANNEL * channel = NULL;
Expand Down
2 changes: 1 addition & 1 deletion libs/openFrameworks/sound/ofMediaFoundationSoundPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1072,4 +1072,4 @@ bool ofMediaFoundationSoundPlayer::_readToBuffer(IMFSourceReader* areader) {
double durSeconds = (double)durMillis / 1000.0;
ofLogVerbose("ofMediaFoundationSoundPlayer::_readToBuffer") << "Total frames read: " << (totalFrames) << " mTotalNumFrames: " << mTotalNumFrames << " dur millis: " << durMillis << " dur seconds: " << durSeconds << std::endl;
return mBuffer.size() > 0;
}
}
35 changes: 31 additions & 4 deletions libs/openFrameworks/sound/ofMediaFoundationSoundPlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,34 @@

#include <list>

// References
//------------------------------------------------------------------
// https://github.com/microsoft/DirectXTK/blob/main/Audio/AudioEngine.cpp
// https://github.com/walbourn/directx-sdk-samples/blob/main/XAudio2/XAudio2MFStream/XAudio2MFStream.cpp
/*
MIT License
Copyright (c) Microsoft Corporation.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE
*/
//------------------------------------------------------------------

namespace of {
struct MFSourceReaderNotifyCallback {
Expand Down Expand Up @@ -112,8 +139,8 @@ class ofMediaFoundationSoundPlayer : public ofBaseSoundPlayer, public of::MFSour
bool isLoaded() const override;
float getVolume() const override;

float getDurationSeconds() { return mDurationSeconds; }
uint32_t getDurationMS() { return mDurationMS; }
float getDuration() const { return mDurationSeconds; }
unsigned int getDurationMS() const { return mDurationMS; }

protected:

Expand Down Expand Up @@ -184,7 +211,7 @@ class ofMediaFoundationSoundPlayer : public ofBaseSoundPlayer, public of::MFSour

bool mBCanSeek = false;
double mDurationSeconds = 0;
uint32_t mDurationMS = 0;
unsigned int mDurationMS = 0;

// 2 = INT_16, 3 = INT_24 and 4 = FLOAT_32
// TODO: Adjust this based on file loaded
Expand Down Expand Up @@ -273,4 +300,4 @@ class ofMediaFoundationSoundPlayer : public ofBaseSoundPlayer, public of::MFSour
std::shared_ptr<SourceReaderCallback> mSrcReaderCallback;
std::mutex mSrcReaderMutex;
bool mBRequestNewReaderSample = false;
};
};
16 changes: 14 additions & 2 deletions libs/openFrameworks/sound/ofOpenALSoundPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ bool ofOpenALSoundPlayer::sfReadFile(const of::filesystem::path& path, std::vect
ofLogError("ofOpenALSoundPlayer") << "sfReadFile(): couldn't read \"" << path << "\"";
return false;
}

duration = 0.0f;
buffer.resize(sfInfo.frames*sfInfo.channels);
fftAuxBuffer.resize(sfInfo.frames*sfInfo.channels);

Expand Down Expand Up @@ -286,6 +286,7 @@ bool ofOpenALSoundPlayer::sfReadFile(const of::filesystem::path& path, std::vect
//------------------------------------------------------------
bool ofOpenALSoundPlayer::mpg123ReadFile(const of::filesystem::path& path,std::vector<short> & buffer,std::vector<float> & fftAuxBuffer){
int err = MPG123_OK;
duration = 0.0f;
mpg123_handle * f = mpg123_new(nullptr,&err);
if(mpg123_open(f,path.string().c_str())!=MPG123_OK){
ofLogError("ofOpenALSoundPlayer") << "mpg123ReadFile(): couldn't read \"" << path << "\"";
Expand Down Expand Up @@ -623,6 +624,16 @@ bool ofOpenALSoundPlayer::isLoaded() const{
return bLoadedOk;
}

//------------------------------------------------------------
float ofOpenALSoundPlayer::getDuration() const {
return duration;
}

//------------------------------------------------------------
unsigned int ofOpenALSoundPlayer::getDurationMS() const {
return duration * 1000.0f;
}

//------------------------------------------------------------
void ofOpenALSoundPlayer::threadedFunction(){
std::vector<std::vector<short> > multibuffer;
Expand Down Expand Up @@ -695,6 +706,7 @@ void ofOpenALSoundPlayer::update(ofEventArgs & args){

//------------------------------------------------------------
void ofOpenALSoundPlayer::unload(){

stop();
ofRemoveListener(ofEvents().update,this,&ofOpenALSoundPlayer::update);

Expand Down Expand Up @@ -723,7 +735,7 @@ void ofOpenALSoundPlayer::unload(){
sf_close(streamf);
}
streamf = 0;

duration = 0.0f;
bLoadedOk = false;
}

Expand Down
3 changes: 3 additions & 0 deletions libs/openFrameworks/sound/ofOpenALSoundPlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ class ofOpenALSoundPlayer : public ofBaseSoundPlayer, public ofThread {
float getVolume() const;
bool isPaused() const;
bool isLoaded() const;

float getDuration() const;
unsigned int getDurationMS() const;

static void initialize();
static void close();
Expand Down
2 changes: 2 additions & 0 deletions libs/openFrameworks/sound/ofSoundBaseTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,6 @@ class ofBaseSoundPlayer {
virtual bool isLoaded() const = 0;
virtual float getVolume() const = 0;

virtual float getDuration() const = 0;
virtual unsigned int getDurationMS() const = 0;
};
18 changes: 18 additions & 0 deletions libs/openFrameworks/sound/ofSoundPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,3 +279,21 @@ float ofSoundPlayer::getVolume() const{
return 0;
}
}

//--------------------------------------------------------------------
float ofSoundPlayer::getDuration() const {
if( player ){
return player->getDuration();
} else {
return 0;
}
}

//--------------------------------------------------------------------
unsigned int ofSoundPlayer::getDurationMS() const {
if( player ){
return player->getDurationMS();
} else {
return 0;
}
}
8 changes: 8 additions & 0 deletions libs/openFrameworks/sound/ofSoundPlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,14 @@ class ofSoundPlayer : public ofBaseSoundPlayer {
/// \brief Queries the player to see if its file was loaded successfully.
/// \return whether or not the player is ready to begin playback.
bool isLoaded() const;

/// \brief Gets duration in seconds.
/// \return duration in seconds.
float getDuration() const;

/// \brief Gets duration in milliseconds.
/// \return duration in milliseconds.
unsigned int getDurationMS() const;

protected:
std::shared_ptr<ofBaseSoundPlayer> player;
Expand Down

0 comments on commit 5c5f760

Please sign in to comment.