Beatmup
Beatmup::Audio::Android::AAudioPlayback::Backend Class Reference

Public Member Functions

 Backend ()
 
 ~Backend ()
 
void initialize (BasicRealtimePlayback *frontend, AbstractPlayback::Mode mode)
 
void start ()
 
void stop ()
 

Private Member Functions

void check (aaudio_result_t code, const char *message)
 

Private Attributes

AAudioStream * stream
 

Detailed Description

Definition at line 40 of file aaudio_playback.cpp.

Constructor & Destructor Documentation

◆ Backend()

Beatmup::Audio::Android::AAudioPlayback::Backend::Backend ( )
inline

Definition at line 53 of file aaudio_playback.cpp.

◆ ~Backend()

Beatmup::Audio::Android::AAudioPlayback::Backend::~Backend ( )
inline

Definition at line 55 of file aaudio_playback.cpp.

55  {
56  if (stream)
57  AAudioStream_close(stream);
58  }

Member Function Documentation

◆ check()

void Beatmup::Audio::Android::AAudioPlayback::Backend::check ( aaudio_result_t  code,
const char *  message 
)
inlineprivate

Definition at line 44 of file aaudio_playback.cpp.

44  {
45  if (code < AAUDIO_OK) {
46  std::string report(message);
47  report = report + '\n' + AAudio_convertResultToText(code);
48  throw Audio::PlaybackException(message, (int)code);
49  }
50  }
Communicates an error occurred during the playback.

◆ initialize()

void Beatmup::Audio::Android::AAudioPlayback::Backend::initialize ( BasicRealtimePlayback frontend,
AbstractPlayback::Mode  mode 
)
inline

Definition at line 60 of file aaudio_playback.cpp.

60  {
61  // if there is already an open stream, close it
62  if (stream) {
63  check(AAudioStream_close(stream), "Error when closing stream");
64  stream = nullptr;
65  }
66 
67  // create builder
68  AAudioStreamBuilder *builder;
69  check(AAudio_createStreamBuilder(&builder),
70  "Error when creating stream builder.");
71 
72  // set mode
73  AAudioStreamBuilder_setSampleRate(builder, mode.sampleRate);
74  AAudioStreamBuilder_setChannelCount(builder, mode.numChannels);
75  switch (mode.sampleFormat) {
76  case Int16:
77  AAudioStreamBuilder_setFormat(builder, AAUDIO_FORMAT_PCM_I16);
78  break;
79  case Float32:
80  AAudioStreamBuilder_setFormat(builder, AAUDIO_FORMAT_PCM_FLOAT);
81  break;
82  default:
83  throw PlaybackException("Sample format unsupported by AAudio.", 0, mode);
84 
85  }
86  AAudioStreamBuilder_setBufferCapacityInFrames(builder, mode.bufferLength);
87 
88  // tweak performance
89  AAudioStreamBuilder_setSharingMode(builder, AAUDIO_SHARING_MODE_EXCLUSIVE);
90  AAudioStreamBuilder_setDataCallback(builder, aaudioDataCallback, frontend);
91  AAudioStreamBuilder_setPerformanceMode(builder, AAUDIO_PERFORMANCE_MODE_LOW_LATENCY);
92 
93  // open stream
94  check(AAudioStreamBuilder_openStream(builder, &stream),
95  "Error when opening stream");
96 
97 #ifdef BEATMUP_DEBUG
98  BEATMUP_DEBUG_I("AAudio stream opened %d@%d Hz, %d samples:\n"
99  " device id: %d\n"
100  " sharing mode: %d\n"
101  " performance mode: %d\n",
102  (int)mode.numChannels, (int)mode.sampleRate, (int)mode.bufferLength,
103  (int)AAudioStream_getDeviceId(stream),
104  (int)AAudioStream_getSharingMode(stream),
105  (int)AAudioStream_getPerformanceMode(stream));
106 #endif
107 
108  // clean up
109  AAudioStreamBuilder_delete(builder);
110  }
aaudio_data_callback_result_t aaudioDataCallback(AAudioStream *stream, void *userData, void *audioData, int32_t numFrames)
void check(aaudio_result_t code, const char *message)
#define BEATMUP_DEBUG_I(...)
Definition: debug.h:33
@ Float32
floating point, 32 bit per sample
@ Int16
signed integer, 16 bit per sample
JNIEnv jlong jint mode

◆ start()

void Beatmup::Audio::Android::AAudioPlayback::Backend::start ( )
inline

Definition at line 113 of file aaudio_playback.cpp.

113  {
114  check( AAudioStream_requestStart(stream),
115  "Error when requesting start of a stream");
116 
117  aaudio_stream_state_t nextState = AAUDIO_STREAM_STATE_STARTED;
118  check( AAudioStream_waitForStateChange(stream, AAUDIO_STREAM_STATE_STARTING, &nextState, ONE_SECOND_IN_NANOS),
119  "Error when starting a stream");
120  }
static const int64_t ONE_SECOND_IN_NANOS

◆ stop()

void Beatmup::Audio::Android::AAudioPlayback::Backend::stop ( )
inline

Definition at line 123 of file aaudio_playback.cpp.

123  {
124  check( AAudioStream_requestStop(stream),
125  "Error when requesting stop of a stream");
126 
127  aaudio_stream_state_t nextState = AAUDIO_STREAM_STATE_STOPPED;
128  check( AAudioStream_waitForStateChange(stream, AAUDIO_STREAM_STATE_STOPPING, &nextState, ONE_SECOND_IN_NANOS),
129  "Error when stopping a stream");
130  }

Member Data Documentation

◆ stream

AAudioStream* Beatmup::Audio::Android::AAudioPlayback::Backend::stream
private

Definition at line 42 of file aaudio_playback.cpp.


The documentation for this class was generated from the following file: