audio/internal/oboe: clang-format

This commit is contained in:
Hajime Hoshi 2021-05-16 03:01:43 +09:00
parent 2edb286ec4
commit fc8b3d70fe
2 changed files with 73 additions and 73 deletions

View File

@ -25,15 +25,15 @@ namespace {
class Player : public oboe::AudioStreamDataCallback { class Player : public oboe::AudioStreamDataCallback {
public: public:
static const char* Suspend() { static const char *Suspend() {
std::lock_guard<std::mutex> lock(PlayersMutex()); std::lock_guard<std::mutex> lock(PlayersMutex());
for (Player* player : GetPlayers()) { for (Player *player : GetPlayers()) {
if (!player->IsPlaying()) { if (!player->IsPlaying()) {
continue; continue;
} }
// Close should be called rather than Pause for onPause. // Close should be called rather than Pause for onPause.
// https://github.com/google/oboe/blob/master/docs/GettingStarted.md // https://github.com/google/oboe/blob/master/docs/GettingStarted.md
if (const char* msg = player->Close(); msg) { if (const char *msg = player->Close(); msg) {
return msg; return msg;
} }
GetPlayersToResume().insert(player); GetPlayersToResume().insert(player);
@ -41,10 +41,10 @@ public:
return nullptr; return nullptr;
} }
static const char* Resume() { static const char *Resume() {
std::lock_guard<std::mutex> lock(PlayersMutex()); std::lock_guard<std::mutex> lock(PlayersMutex());
for (Player* player : GetPlayersToResume()) { for (Player *player : GetPlayersToResume()) {
if (const char* msg = player->Play(); msg) { if (const char *msg = player->Play(); msg) {
return msg; return msg;
} }
} }
@ -52,11 +52,10 @@ public:
return nullptr; return nullptr;
} }
Player(int sample_rate, int channel_num, int bit_depth_in_bytes, double volume, uintptr_t go_player) Player(int sample_rate, int channel_num, int bit_depth_in_bytes,
: sample_rate_{sample_rate}, double volume, uintptr_t go_player)
channel_num_{channel_num}, : sample_rate_{sample_rate}, channel_num_{channel_num},
bit_depth_in_bytes_{bit_depth_in_bytes}, bit_depth_in_bytes_{bit_depth_in_bytes}, go_player_{go_player} {
go_player_{go_player} {
std::atomic_store(&volume_, volume); std::atomic_store(&volume_, volume);
{ {
std::lock_guard<std::mutex> lock(PlayersMutex()); std::lock_guard<std::mutex> lock(PlayersMutex());
@ -64,32 +63,31 @@ public:
} }
} }
void SetVolume(double volume) { void SetVolume(double volume) { std::atomic_store(&volume_, volume); }
std::atomic_store(&volume_, volume);
}
bool IsPlaying() { bool IsPlaying() { return stream_->getState() == oboe::StreamState::Started; }
return stream_->getState() == oboe::StreamState::Started;
}
void AppendBuffer(uint8_t* data, int length) { void AppendBuffer(uint8_t *data, int length) {
// Sync this constants with internal/readerdriver/driver.go // Sync this constants with internal/readerdriver/driver.go
const size_t bytes_per_sample = channel_num_ * bit_depth_in_bytes_; const size_t bytes_per_sample = channel_num_ * bit_depth_in_bytes_;
const size_t one_buffer_size = sample_rate_ * channel_num_ * bit_depth_in_bytes_ / 4 / bytes_per_sample * bytes_per_sample; const size_t one_buffer_size = sample_rate_ * channel_num_ *
bit_depth_in_bytes_ / 4 / bytes_per_sample *
bytes_per_sample;
const size_t max_buffer_size = one_buffer_size * 2; const size_t max_buffer_size = one_buffer_size * 2;
std::lock_guard<std::mutex> lock(mutex_); std::lock_guard<std::mutex> lock(mutex_);
buf_.insert(buf_.end(), data, data + length); buf_.insert(buf_.end(), data, data + length);
} }
const char* Play() { const char *Play() {
if (bit_depth_in_bytes_ != 2) { if (bit_depth_in_bytes_ != 2) {
return "bit_depth_in_bytes_ must be 2 but not"; return "bit_depth_in_bytes_ must be 2 but not";
} }
if (!stream_) { if (!stream_) {
oboe::AudioStreamBuilder builder; oboe::AudioStreamBuilder builder;
oboe::Result result = builder.setDirection(oboe::Direction::Output) oboe::Result result =
builder.setDirection(oboe::Direction::Output)
->setPerformanceMode(oboe::PerformanceMode::LowLatency) ->setPerformanceMode(oboe::PerformanceMode::LowLatency)
->setSharingMode(oboe::SharingMode::Shared) ->setSharingMode(oboe::SharingMode::Shared)
->setFormat(oboe::AudioFormat::I16) ->setFormat(oboe::AudioFormat::I16)
@ -111,7 +109,7 @@ public:
return nullptr; return nullptr;
} }
const char* Pause() { const char *Pause() {
if (!stream_) { if (!stream_) {
return nullptr; return nullptr;
} }
@ -121,7 +119,7 @@ public:
return nullptr; return nullptr;
} }
const char* Close() { const char *Close() {
if (!stream_) { if (!stream_) {
return nullptr; return nullptr;
} }
@ -135,11 +133,11 @@ public:
return nullptr; return nullptr;
} }
const char* CloseAndRemove() { const char *CloseAndRemove() {
// Close and remove self from the players atomically. // Close and remove self from the players atomically.
// Otherwise, a removed player might be resumed at Resume unexpectedly. // Otherwise, a removed player might be resumed at Resume unexpectedly.
std::lock_guard<std::mutex> lock(PlayersMutex()); std::lock_guard<std::mutex> lock(PlayersMutex());
const char* msg = Close(); const char *msg = Close();
GetPlayers().erase(this); GetPlayers().erase(this);
GetPlayersToResume().erase(this); GetPlayersToResume().erase(this);
return msg; return msg;
@ -150,7 +148,9 @@ public:
return buf_.size(); return buf_.size();
} }
oboe::DataCallbackResult onAudioReady(oboe::AudioStream *oboe_stream, void *audio_data, int32_t num_frames) override { oboe::DataCallbackResult onAudioReady(oboe::AudioStream *oboe_stream,
void *audio_data,
int32_t num_frames) override {
size_t num_bytes = num_frames * channel_num_ * bit_depth_in_bytes_; size_t num_bytes = num_frames * channel_num_ * bit_depth_in_bytes_;
std::vector<uint8_t> buf(num_bytes); std::vector<uint8_t> buf(num_bytes);
{ {
@ -164,30 +164,31 @@ public:
} }
if (const double volume = std::atomic_load(&volume_); volume < 1) { if (const double volume = std::atomic_load(&volume_); volume < 1) {
for (int i = 0; i < buf.size()/2; i++) { for (int i = 0; i < buf.size() / 2; i++) {
int16_t v = static_cast<int16_t>(buf[2*i]) | (static_cast<int16_t>(buf[2*i+1]) << 8); int16_t v = static_cast<int16_t>(buf[2 * i]) |
(static_cast<int16_t>(buf[2 * i + 1]) << 8);
v = static_cast<int16_t>(static_cast<double>(v) * volume); v = static_cast<int16_t>(static_cast<double>(v) * volume);
buf[2*i] = static_cast<uint8_t>(v); buf[2 * i] = static_cast<uint8_t>(v);
buf[2*i+1] = static_cast<uint8_t>(v >> 8); buf[2 * i + 1] = static_cast<uint8_t>(v >> 8);
} }
} }
std::copy(buf.begin(), buf.end(), reinterpret_cast<uint8_t*>(audio_data)); std::copy(buf.begin(), buf.end(), reinterpret_cast<uint8_t *>(audio_data));
return oboe::DataCallbackResult::Continue; return oboe::DataCallbackResult::Continue;
} }
private: private:
static std::set<Player*>& GetPlayers() { static std::set<Player *> &GetPlayers() {
static std::set<Player*> players; static std::set<Player *> players;
return players; return players;
} }
static std::set<Player*>& GetPlayersToResume() { static std::set<Player *> &GetPlayersToResume() {
static std::set<Player*> players_to_resume; static std::set<Player *> players_to_resume;
return players_to_resume; return players_to_resume;
} }
static std::mutex& PlayersMutex() { static std::mutex &PlayersMutex() {
static std::mutex mutex; static std::mutex mutex;
return mutex; return mutex;
} }
@ -207,53 +208,51 @@ private:
extern "C" { extern "C" {
const char* Suspend() { const char *Suspend() { return Player::Suspend(); }
return Player::Suspend();
}
const char* Resume() { const char *Resume() { return Player::Resume(); }
return Player::Resume();
}
PlayerID Player_Create(int sample_rate, int channel_num, int bit_depth_in_bytes, double volume, uintptr_t go_player) { PlayerID Player_Create(int sample_rate, int channel_num, int bit_depth_in_bytes,
Player* p = new Player(sample_rate, channel_num, bit_depth_in_bytes, volume, go_player); double volume, uintptr_t go_player) {
Player *p = new Player(sample_rate, channel_num, bit_depth_in_bytes, volume,
go_player);
return reinterpret_cast<PlayerID>(p); return reinterpret_cast<PlayerID>(p);
} }
bool Player_IsPlaying(PlayerID audio_player) { bool Player_IsPlaying(PlayerID audio_player) {
Player* p = reinterpret_cast<Player*>(audio_player); Player *p = reinterpret_cast<Player *>(audio_player);
return p->IsPlaying(); return p->IsPlaying();
} }
void Player_AppendBuffer(PlayerID audio_player, uint8_t* data, int length) { void Player_AppendBuffer(PlayerID audio_player, uint8_t *data, int length) {
Player* p = reinterpret_cast<Player*>(audio_player); Player *p = reinterpret_cast<Player *>(audio_player);
p->AppendBuffer(data, length); p->AppendBuffer(data, length);
} }
const char* Player_Play(PlayerID audio_player) { const char *Player_Play(PlayerID audio_player) {
Player* p = reinterpret_cast<Player*>(audio_player); Player *p = reinterpret_cast<Player *>(audio_player);
return p->Play(); return p->Play();
} }
const char* Player_Pause(PlayerID audio_player) { const char *Player_Pause(PlayerID audio_player) {
Player* p = reinterpret_cast<Player*>(audio_player); Player *p = reinterpret_cast<Player *>(audio_player);
return p->Pause(); return p->Pause();
} }
void Player_SetVolume(PlayerID audio_player, double volume) { void Player_SetVolume(PlayerID audio_player, double volume) {
Player* p = reinterpret_cast<Player*>(audio_player); Player *p = reinterpret_cast<Player *>(audio_player);
p->SetVolume(volume); p->SetVolume(volume);
} }
const char* Player_Close(PlayerID audio_player) { const char *Player_Close(PlayerID audio_player) {
Player* p = reinterpret_cast<Player*>(audio_player); Player *p = reinterpret_cast<Player *>(audio_player);
const char* msg = p->CloseAndRemove(); const char *msg = p->CloseAndRemove();
delete p; delete p;
return msg; return msg;
} }
int Player_UnplayedBufferSize(PlayerID audio_player) { int Player_UnplayedBufferSize(PlayerID audio_player) {
Player* p = reinterpret_cast<Player*>(audio_player); Player *p = reinterpret_cast<Player *>(audio_player);
return p->GetUnplayedBufferSize(); return p->GetUnplayedBufferSize();
} }

View File

@ -25,14 +25,15 @@ extern "C" {
typedef uintptr_t PlayerID; typedef uintptr_t PlayerID;
const char* Suspend(); const char *Suspend();
const char* Resume(); const char *Resume();
PlayerID Player_Create(int sample_rate, int channel_num, int bit_depth_in_bytes, double volume, uintptr_t go_player); PlayerID Player_Create(int sample_rate, int channel_num, int bit_depth_in_bytes,
double volume, uintptr_t go_player);
bool Player_IsPlaying(PlayerID audio_player); bool Player_IsPlaying(PlayerID audio_player);
void Player_AppendBuffer(PlayerID audio_player, uint8_t* data, int length); void Player_AppendBuffer(PlayerID audio_player, uint8_t *data, int length);
const char* Player_Play(PlayerID audio_player); const char *Player_Play(PlayerID audio_player);
const char* Player_Pause(PlayerID audio_player); const char *Player_Pause(PlayerID audio_player);
const char* Player_Close(PlayerID audio_player); const char *Player_Close(PlayerID audio_player);
void Player_SetVolume(PlayerID audio_player, double volume); void Player_SetVolume(PlayerID audio_player, double volume);
int Player_UnplayedBufferSize(PlayerID audio_player); int Player_UnplayedBufferSize(PlayerID audio_player);