Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose functions from AudioStreamPlaybackMicrophone to access the microphone AudioDriver::input_buffer independently of the rest of the Audio system #100508

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
b70c10e
expose useful functions from AudioStreamPlaybackMicrophone
goatchurchprime Dec 17, 2024
c44d252
add in the audiostreamplaybackmicrophone.xml
goatchurchprime Dec 17, 2024
32eb4ca
add descriptions to the AudioStreamPlaybackMicrophone class
goatchurchprime Dec 17, 2024
3c2adea
fix spelling of and
goatchurchprime Dec 17, 2024
d32df03
move & signs to the right
goatchurchprime Dec 17, 2024
68947ae
add in separate start/stop_microphone functions to avoid overriding a…
goatchurchprime Dec 18, 2024
88e361c
change docs reference to p_frames
goatchurchprime Dec 18, 2024
cef55e1
fix core dump due to referencing null object
goatchurchprime Dec 19, 2024
232e53b
add mix_microphone and fix null pointer in destructor
goatchurchprime Dec 19, 2024
6f40fd9
Merge branch 'gtch/micplaybackmaster' of github.com:goatchurchprime/g…
goatchurchprime Dec 19, 2024
3ea099c
remove blank lines for formatting, really
goatchurchprime Dec 19, 2024
41568cc
format blank line and debug messages about microphone actual turning on
goatchurchprime Dec 21, 2024
dd3d3ea
enable debug warnings
goatchurchprime Jan 2, 2025
6196352
input_buffer_write is thread locked and android input_stop now works
goatchurchprime Jan 4, 2025
e367108
fix the code style issues
goatchurchprime Jan 5, 2025
539ede8
remove a couple more spaces
goatchurchprime Jan 5, 2025
79aa8c9
add virtual override on try_lock
goatchurchprime Jan 5, 2025
2b5716f
remove double delcaration of buf
goatchurchprime Jan 5, 2025
cedc257
remove the post-permission callback to the input_start() function
goatchurchprime Jan 5, 2025
3427401
remove space at end of line in comment
goatchurchprime Jan 6, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 40 additions & 3 deletions servers/audio/audio_stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ bool AudioStreamMicrophone::is_monophonic() const {
int AudioStreamPlaybackMicrophone::_mix_internal(AudioFrame *p_buffer, int p_frames) {
AudioDriver::get_singleton()->lock();

Vector<int32_t> buf = AudioDriver::get_singleton()->get_input_buffer();
Vector<int32_t>& buf = AudioDriver::get_singleton()->get_input_buffer();
unsigned int input_size = AudioDriver::get_singleton()->get_input_size();
int mix_rate = AudioDriver::get_singleton()->get_input_mix_rate();
unsigned int playback_delay = MIN(((50 * mix_rate) / 1000) * 2, buf.size() >> 1);
Expand Down Expand Up @@ -438,6 +438,24 @@ int AudioStreamPlaybackMicrophone::_mix_internal(AudioFrame *p_buffer, int p_fra
return mixed_frames;
}

PackedVector2Array AudioStreamPlaybackMicrophone::get_microphone_buffer(int p_frames) {
PackedVector2Array ret;
unsigned int input_position = AudioDriver::get_singleton()->get_input_position();
Vector<int32_t>& buf = AudioDriver::get_singleton()->get_input_buffer();
if (input_position < input_ofs)
input_position += buf.size();
if (input_position < input_ofs + p_frames * 2)
return ret;
Vector<AudioFrame> streaming_data;
streaming_data.resize(p_frames);
int mixed_frames = _mix_internal(streaming_data.ptrw(), p_frames);
ret.resize(mixed_frames);
for (int32_t i = 0; i < mixed_frames; i++) {
ret.write[i] = Vector2(streaming_data[i].left, streaming_data[i].right);
}
return ret;
}

int AudioStreamPlaybackMicrophone::mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames) {
return AudioStreamPlaybackResampled::mix(p_buffer, p_rate_scale, p_frames);
}
Expand All @@ -458,15 +476,24 @@ void AudioStreamPlaybackMicrophone::start(double p_from_pos) {

input_ofs = 0;

if (AudioDriver::get_singleton()->input_start() == OK) {
if (AudioDriver::get_singleton()->input_start_count == 0) {
if (AudioDriver::get_singleton()->input_start() == OK) {
active = true;
begin_resample();
AudioDriver::get_singleton()->input_start_count++;
}
} else {
active = true;
begin_resample();
AudioDriver::get_singleton()->input_start_count++;
}
}

void AudioStreamPlaybackMicrophone::stop() {
if (active) {
AudioDriver::get_singleton()->input_stop();
AudioDriver::get_singleton()->input_start_count--;
if (AudioDriver::get_singleton()->input_start_count == 0)
AudioDriver::get_singleton()->input_stop();
active = false;
}
}
Expand All @@ -491,6 +518,16 @@ void AudioStreamPlaybackMicrophone::tag_used_streams() {
microphone->tag_used(0);
}

void AudioStreamPlaybackMicrophone::_bind_methods() {
ClassDB::bind_method(D_METHOD("start"), &AudioStreamPlaybackMicrophone::start);
ClassDB::bind_method(D_METHOD("stop"), &AudioStreamPlaybackMicrophone::stop);
ClassDB::bind_method(D_METHOD("is_playing"), &AudioStreamPlaybackMicrophone::is_playing);
ClassDB::bind_method(D_METHOD("get_microphone_buffer", "p_frames"), &AudioStreamPlaybackMicrophone::get_microphone_buffer);

// how do we get this one in and available to GDExtensions that has AudioFrame* as a parameter type?
//ClassDB::bind_method(D_METHOD("mix", "p_buffer", "p_rate_scale", "p_frames"), &AudioStreamPlaybackResampled::mix);
}

AudioStreamPlaybackMicrophone::~AudioStreamPlaybackMicrophone() {
microphone->playbacks.erase(this);
stop();
Expand Down
4 changes: 4 additions & 0 deletions servers/audio/audio_stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@ class AudioStreamPlaybackMicrophone : public AudioStreamPlaybackResampled {
Ref<AudioStreamMicrophone> microphone;

protected:
static void _bind_methods();

virtual int _mix_internal(AudioFrame *p_buffer, int p_frames) override;
virtual float get_stream_sampling_rate() override;
virtual double get_playback_position() const override;
Expand All @@ -257,6 +259,8 @@ class AudioStreamPlaybackMicrophone : public AudioStreamPlaybackResampled {
virtual void stop() override;
virtual bool is_playing() const override;

PackedVector2Array get_microphone_buffer(int p_frames);

virtual int get_loop_count() const override; //times it looped

virtual void seek(double p_time) override;
Expand Down
5 changes: 4 additions & 1 deletion servers/audio_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,13 @@ class AudioDriver {
SafeNumeric<uint64_t> prof_time;
#endif

friend class AudioStreamPlaybackMicrophone;

protected:
Vector<int32_t> input_buffer;
unsigned int input_position = 0;
unsigned int input_size = 0;
int32_t input_start_count = 0;

void audio_server_process(int p_frames, int32_t *p_buffer, bool p_update_mix_time = true);
void update_mix_time(int p_frames);
Expand Down Expand Up @@ -123,7 +126,7 @@ class AudioDriver {
SpeakerMode get_speaker_mode_by_total_channels(int p_channels) const;
int get_total_channels_by_speaker_mode(SpeakerMode) const;

Vector<int32_t> get_input_buffer() { return input_buffer; }
Vector<int32_t>& get_input_buffer() { return input_buffer; }
unsigned int get_input_position() { return input_position; }
unsigned int get_input_size() { return input_size; }

Expand Down
1 change: 1 addition & 0 deletions servers/register_server_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ void register_server_types() {
GDREGISTER_CLASS(AudioStreamPlayback);
GDREGISTER_VIRTUAL_CLASS(AudioStreamPlaybackResampled);
GDREGISTER_CLASS(AudioStreamMicrophone);
GDREGISTER_CLASS(AudioStreamPlaybackMicrophone);
GDREGISTER_CLASS(AudioStreamRandomizer);
GDREGISTER_CLASS(AudioSample);
GDREGISTER_CLASS(AudioSamplePlayback);
Expand Down