Skip to content

Commit ea1e04e

Browse files
committed
audio: use unique_ptr for sources, fix AServer source updating by tracking sources properly
1 parent f30dbdd commit ea1e04e

23 files changed

Lines changed: 132 additions & 124 deletions

src/OpenCOVER/audio/AlutContext.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
using namespace opencover::audio;
55

66
int AlutContext::_refcount = 0;
7+
bool AlutContext::is_initialized = false;
8+
bool AlutContext::has_context = false;
79

810
AlutContext::AlutContext()
911
{
@@ -35,5 +37,7 @@ AlutContext::~AlutContext()
3537
if (_refcount == 0 && is_initialized)
3638
{
3739
alutExit();
40+
is_initialized = false;
41+
has_context = false;
3842
}
3943
}

src/OpenCOVER/audio/AlutContext.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ class COVEREXPORT AlutContext
1919
AlutContext();
2020
~AlutContext();
2121

22-
bool is_initialized;
23-
bool has_context;
22+
static bool is_initialized;
23+
static bool has_context;
2424

2525
private:
2626
static int _refcount;

src/OpenCOVER/audio/Audio.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ void Audio::loadFile()
4545
return;
4646
}
4747

48-
if (!_alut.is_initialized)
48+
if (!AlutContext::is_initialized)
4949
{
5050
std::cerr << "Audio: ALUT not loaded" << std::endl;
5151
return;
@@ -92,13 +92,13 @@ void Audio::loadFileToBuffer()
9292
return;
9393
}
9494

95-
if (!_alut.has_context)
95+
if (!AlutContext::has_context)
9696
{
9797
std::cerr << "Audio: no ALUT context available to load audio file to buffer" << std::endl;
9898
return;
9999
}
100100

101-
if (!_alut.is_initialized)
101+
if (!AlutContext::is_initialized)
102102
{
103103
std::cerr << "Audio: ALUT not loaded" << std::endl;
104104
return;

src/OpenCOVER/audio/Player.cpp

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "Listener.h"
99
#include "Player.h"
1010
#include <assert.h>
11+
#include <algorithm>
1112
#include <boost/algorithm/string.hpp>
1213

1314
#include <config/CoviseConfig.h>
@@ -20,9 +21,16 @@ using std::endl;
2021
using namespace opencover::audio;
2122
using covise::coCoviseConfig;
2223

23-
Player::Source::Source(const Audio *audio)
24-
: audio(audio)
24+
Player::Source::Source(Player *player, const Audio *audio)
25+
: player(player)
26+
, audio(audio)
2527
{
28+
player->registerSource(this);
29+
}
30+
31+
Player::Source::~Source()
32+
{
33+
player->unregisterSource(this);
2634
}
2735

2836
void Player::Source::setAudio(const Audio *audio)
@@ -98,7 +106,10 @@ Player::Player(const Listener *listener)
98106

99107
void Player::update()
100108
{
101-
//
109+
for (auto &source : sources)
110+
{
111+
source->update(this);
112+
}
102113
}
103114

104115
Player *Player::createPlayer(Listener *listener, const std::string &type)
@@ -135,8 +146,19 @@ Player *Player::createPlayer(Listener *listener, const std::string &type)
135146
return nullptr;
136147
}
137148

138-
Player::Source *
139-
Player::newSource(const Audio *audio)
149+
std::unique_ptr<Player::Source>
150+
Player::makeSource(const Audio *audio)
151+
{
152+
return std::make_unique<Player::Source>(this, audio);
153+
}
154+
155+
void Player::registerSource(Source *source)
156+
{
157+
sources.insert(source);
158+
}
159+
160+
void Player::unregisterSource(Source *source)
140161
{
141-
return new Source(audio);
162+
163+
sources.erase(source);
142164
}

src/OpenCOVER/audio/Player.h

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
#include <util/coExport.h>
1212
#include "Audio.h"
1313

14-
#include <vector>
14+
#include <set>
15+
#include <memory>
1516
#include <glm/vec3.hpp>
1617

1718
#include "Listener.h"
@@ -30,7 +31,9 @@ class COVEREXPORT Player
3031
friend class PlayerOsc;
3132

3233
public:
33-
Source(const Audio *audio);
34+
Source(Player *player, const Audio *audio);
35+
virtual ~Source();
36+
3437
virtual void setPitch(float pitch);
3538
virtual void setIntensity(float intensity);
3639
virtual void setStart(double start);
@@ -44,17 +47,12 @@ class COVEREXPORT Player
4447
virtual void play(double start);
4548
virtual void stop();
4649
virtual bool isPlaying();
47-
virtual int update(const Player *genericPlayer = 0, char *buf = 0, int bufsiz = 0)
48-
{
49-
(void)genericPlayer;
50-
(void)buf;
51-
(void)bufsiz;
52-
return -1;
53-
}
50+
virtual void update(const Player *genericPlayer) { }
5451
virtual void stopForRestart() { }
5552
virtual void restart() { }
5653

5754
protected:
55+
Player *player;
5856
const Audio *audio;
5957
float pitch = 1.f;
6058
float intensity = 0.f;
@@ -68,16 +66,17 @@ class COVEREXPORT Player
6866
};
6967

7068
Player(const Listener *listener);
71-
7269
virtual void update();
73-
74-
// Source related
75-
virtual Source *newSource(const Audio *);
70+
virtual std::unique_ptr<Source> makeSource(const Audio *);
7671

7772
static Player *createPlayer(Listener *listener, const std::string &type);
7873

7974
protected:
8075
const Listener *listener;
76+
77+
std::set<Source *> sources;
78+
void registerSource(Source *source);
79+
void unregisterSource(Source *source);
8180
};
8281
} // namespace
8382
#endif

src/OpenCOVER/audio/PlayerAServer.cpp

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -341,14 +341,13 @@ int PlayerAServer::read_answer(char *buf, int maxsize) const
341341
return -1;
342342
}
343343

344-
PlayerAServer::Source::Source(const Audio *audio, PlayerAServer *player)
345-
: Player::Source(audio)
344+
PlayerAServer::Source::Source(Player *player, const Audio *audio)
345+
: Player::Source(player, audio)
346346
, asHandle(-1)
347347
, odirection(0.0)
348348
, ointensity(0.0)
349349
, opitch(1.0)
350350
, ov(0.0, 0.0, 0.0)
351-
, player(player)
352351
{
353352
loadAudio(audio);
354353
}
@@ -359,6 +358,8 @@ PlayerAServer::Source::Source(const Audio *audio, PlayerAServer *player)
359358

360359
void PlayerAServer::Source::loadAudio(const Audio *audio)
361360
{
361+
const PlayerAServer *player = (PlayerAServer *)this->player;
362+
362363
if (!player)
363364
return;
364365

@@ -443,6 +444,7 @@ void PlayerAServer::Source::setAudio(const Audio *audio)
443444
if (asHandle >= 0)
444445
{
445446
snprintf(msg, sizeof(msg), "RHDL %d", asHandle);
447+
const PlayerAServer *player = (PlayerAServer *)this->player;
446448
player->send_cmd(msg);
447449
}
448450

@@ -460,6 +462,7 @@ PlayerAServer::Source::~Source()
460462
if (asHandle >= 0)
461463
{
462464
snprintf(msg, sizeof(msg), "RHDL %d", asHandle);
465+
const PlayerAServer *player = (PlayerAServer *)this->player;
463466
player->send_cmd(msg);
464467
}
465468
}
@@ -471,6 +474,7 @@ void PlayerAServer::Source::play(double start)
471474
if (asHandle >= 0)
472475
{
473476
snprintf(msg, sizeof(msg), "PLAY %d %f", asHandle, start);
477+
const PlayerAServer *player = (PlayerAServer *)this->player;
474478
if (player->send_cmd(msg) < 0)
475479
{
476480
CERR << "playing handle " << asHandle << " failed" << endl;
@@ -494,30 +498,21 @@ void PlayerAServer::Source::stop()
494498
if (asHandle >= 0)
495499
{
496500
snprintf(msg, sizeof(msg), "STOP %d", asHandle);
501+
const PlayerAServer *player = (PlayerAServer *)this->player;
497502
player->send_cmd(msg);
498503
}
499504
}
500505

501-
int PlayerAServer::Source::update(const Player *genericPlayer, char *buf, int bufsize)
506+
void PlayerAServer::Source::update(const Player *genericPlayer)
502507
{
503-
Player::Source::update(genericPlayer, buf, bufsize);
508+
Player::Source::update(genericPlayer);
504509

505510
// dynamic_cast causes problems on gcc2 systems
506511
// const PlayerAServer *player = dynamic_cast<const PlayerAServer *>(genericPlayer);
507512
const PlayerAServer *player = (const PlayerAServer *)(genericPlayer);
508-
if (!player)
509-
{
510-
return -1;
511-
}
512-
513-
if (asHandle < 0)
513+
if (!player || asHandle < 0 || !isPlaying())
514514
{
515-
return -1;
516-
}
517-
518-
if (!isPlaying())
519-
{
520-
return -1;
515+
return;
521516
}
522517

523518
char msg[MAX_BUFLEN];
@@ -559,8 +554,6 @@ int PlayerAServer::Source::update(const Player *genericPlayer, char *buf, int bu
559554
player->send_cmd(msg);
560555
opitch = pitch;
561556
}
562-
563-
return 0;
564557
}
565558

566559
void PlayerAServer::Source::setLoop(bool loop)
@@ -572,12 +565,13 @@ void PlayerAServer::Source::setLoop(bool loop)
572565
if (asHandle >= 0)
573566
{
574567
snprintf(msg, sizeof(msg), "SSLP %d %d", asHandle, loop ? 1 : 0);
568+
const PlayerAServer *player = (PlayerAServer *)this->player;
575569
player->send_cmd(msg);
576570
}
577571
}
578572

579-
Player::Source *
580-
PlayerAServer::newSource(const Audio *audio)
573+
std::unique_ptr<Player::Source>
574+
PlayerAServer::makeSource(Player *player, const Audio *audio)
581575
{
582-
return new Source(audio, this);
576+
return std::make_unique<PlayerAServer::Source>(this, audio);
583577
}

src/OpenCOVER/audio/PlayerAServer.h

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class COVEREXPORT PlayerAServer : public Player
2020
public:
2121
PlayerAServer(const Listener *listener, const std::string &host, int port);
2222
virtual ~PlayerAServer();
23-
virtual Player::Source *newSource(const Audio *audio);
23+
virtual std::unique_ptr<Source> makeSource(Player *player, const Audio *audio);
2424

2525
virtual int send_cmd(const char *cmd) const;
2626
virtual int send_data(const char *data, int size, bool swapped = false) const;
@@ -31,23 +31,22 @@ class COVEREXPORT PlayerAServer : public Player
3131
class Source : public Player::Source
3232
{
3333
public:
34-
Source(const Audio *audio, PlayerAServer *player);
34+
Source(Player *player, const Audio *audio);
3535
virtual ~Source();
36-
virtual void setAudio(const Audio *audio);
37-
virtual void play(double start);
38-
virtual void play();
39-
virtual void stop();
36+
virtual void setAudio(const Audio *audio) override;
37+
virtual void play(double start) override;
38+
virtual void play() override;
39+
virtual void stop() override;
4040

41-
virtual void setLoop(bool loop);
41+
virtual void setLoop(bool loop) override;
4242

43-
virtual int update(const Player *player, char *buf = 0, int numFrames = 0);
43+
virtual void update(const Player *player) override;
4444

4545
int asHandle;
4646
float odirection;
4747
float ointensity;
4848
float opitch;
4949
glm::vec3 ov;
50-
PlayerAServer *player;
5150

5251
private:
5352
virtual void loadAudio(const Audio *audio);

src/OpenCOVER/audio/PlayerOpenAL.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,14 @@ void PlayerOpenAL::update()
6262
}
6363
}
6464

65-
Player::Source *
66-
PlayerOpenAL::newSource(const Audio *audio)
65+
std::unique_ptr<Player::Source>
66+
PlayerOpenAL::makeSource(const Audio *audio)
6767
{
68-
return new Source(audio);
68+
return std::make_unique<PlayerOpenAL::Source>(this, audio);
6969
}
7070

71-
PlayerOpenAL::Source::Source(const Audio *audio)
72-
: Player::Source(audio)
71+
PlayerOpenAL::Source::Source(Player* player, const Audio *audio)
72+
: Player::Source(player, audio)
7373
{
7474
alGenSources(1, &alSource);
7575
alSource3f(alSource, AL_POSITION, x.x, x.y, x.z);

src/OpenCOVER/audio/PlayerOpenAL.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,23 @@ class COVEREXPORT PlayerOpenAL : public Player
3030
PlayerOpenAL(const Listener *listener);
3131
virtual void update();
3232

33-
virtual Player::Source *newSource(const Audio *audio);
33+
virtual std::unique_ptr<Player::Source> makeSource(const Audio *audio);
3434

3535
protected:
3636
class Source : public Player::Source
3737
{
3838
public:
39-
Source(const Audio *audio);
39+
Source(Player* player, const Audio *audio);
4040
virtual ~Source();
4141

42-
virtual void setLoop(bool loop);
43-
virtual void setPitch(float pitch);
44-
virtual void setSpatialize(bool spatialize);
45-
virtual void setPosition(float x, float y, float z);
46-
virtual void setVelocity(float vx, float vy, float vz);
47-
virtual void setIntensity(float intensity);
48-
virtual void play(double start);
49-
virtual void stop();
42+
virtual void setLoop(bool loop) override;
43+
virtual void setPitch(float pitch) override;
44+
virtual void setSpatialize(bool spatialize) override;
45+
virtual void setPosition(float x, float y, float z) override;
46+
virtual void setVelocity(float vx, float vy, float vz) override;
47+
virtual void setIntensity(float intensity) override;
48+
virtual void play(double start) override;
49+
virtual void stop() override;
5050
ALuint alSource;
5151
};
5252

0 commit comments

Comments
 (0)