Modified Microphone implementation to handle only one device at a time (WIP)

This commit is contained in:
Marcelo Fernandez 2018-07-03 22:08:43 -03:00 committed by Saracen
parent 76fd9d215c
commit 061358d838
22 changed files with 556 additions and 1018 deletions

View file

@ -53,9 +53,11 @@ class AudioDriver {
#endif
protected:
Vector<int32_t> audio_input_buffer;
unsigned int audio_input_position;
void audio_server_process(int p_frames, int32_t *p_buffer, bool p_update_mix_time = true);
void update_mix_time(int p_frames);
void update_microphone_default(StringName p_device_name);
#ifdef DEBUG_ENABLED
_FORCE_INLINE_ void start_counting_ticks() { prof_ticks = OS::get_singleton()->get_ticks_usec(); }
@ -68,126 +70,6 @@ protected:
public:
double get_mix_time() const; //useful for video -> audio sync
class MicrophoneDeviceOutput;
class MicrophoneReciever {
public:
MicrophoneDeviceOutput *owner;
MicrophoneReciever() {
owner = NULL;
}
~MicrophoneReciever() {
}
};
class MicrophoneDeviceOutput : public MicrophoneReciever {
public:
StringName name;
bool active;
Vector<MicrophoneReciever *> recievers;
virtual unsigned int get_mix_rate() = 0;
virtual Vector<AudioFrame> &get_buffer() = 0;
virtual int get_current_buffer_size() = 0;
virtual int get_read_index() = 0;
virtual void set_read_index(int p_temp_index) = 0;
void add_reciever(MicrophoneReciever *p_reciever) {
if (p_reciever == NULL) {
ERR_PRINT("Attempted to add NULL reciever")
return;
}
if (recievers.find(p_reciever) == -1) {
recievers.push_back(p_reciever);
p_reciever->owner = this;
} else {
ERR_PRINT("Duplicate reciever added")
}
}
void remove_reciever(MicrophoneReciever *p_reciever) {
if (p_reciever == NULL) {
ERR_PRINT("Attempted to remove NULL reciever")
return;
}
int index = recievers.find(p_reciever);
if (index != -1) {
recievers.remove(index);
p_reciever->owner = NULL;
} else {
ERR_PRINT("Attempted to remove invalid reciever")
}
}
};
class MicrophoneDeviceOutputDirect : public MicrophoneDeviceOutput {
public:
enum MicrophoneFormat {
FORMAT_FLOAT,
FORMAT_PCM
};
MicrophoneFormat microphone_format;
unsigned short bits_per_sample;
unsigned int channels;
unsigned int mix_rate;
unsigned short frame_size;
int read_index = -2048;
unsigned int current_capture_index;
unsigned int current_capture_size;
Vector<AudioFrame> buffer;
unsigned int get_mix_rate() {
return mix_rate;
};
Vector<AudioFrame> &get_buffer() {
return buffer;
};
int get_current_buffer_size() { return current_capture_size; }
int get_read_index() {
return read_index;
}
void set_read_index(int p_read_index) {
read_index = p_read_index;
}
};
class MicrophoneDeviceOutputIndirect : public MicrophoneDeviceOutput {
public:
unsigned int get_mix_rate() {
return owner->get_mix_rate();
};
Vector<AudioFrame> &get_buffer() {
return owner->get_buffer();
};
int get_read_index() {
return owner->get_read_index();
}
void set_read_index(int p_read_index) {
owner->set_read_index(p_read_index);
}
int get_current_buffer_size() { return owner->get_current_buffer_size(); }
};
MicrophoneDeviceOutputIndirect *default_microphone_device_output;
Vector<MicrophoneDeviceOutput *> microphone_device_outputs;
Map<StringName, MicrophoneDeviceOutput *> microphone_device_output_map;
Vector<MicrophoneReciever *> direct_recievers;
enum SpeakerMode {
SPEAKER_MODE_STEREO,
SPEAKER_SURROUND_31,
@ -214,18 +96,19 @@ public:
virtual void unlock() = 0;
virtual void finish() = 0;
virtual bool capture_device_start(StringName p_name) = 0;
virtual bool capture_device_stop(StringName p_name) = 0;
virtual PoolStringArray capture_device_get_names() = 0;
virtual StringName capture_device_get_default_name() = 0;
virtual Error capture_start() { return FAILED; }
virtual Error capture_stop() { return FAILED; }
virtual void capture_set_device(StringName p_name) {}
virtual StringName capture_get_device() { return "Default"; }
virtual Array capture_get_device_list(); // TODO: convert this and get_device_list to PoolStringArray
virtual float get_latency() { return 0; }
SpeakerMode get_speaker_mode_by_total_channels(int p_channels) const;
int get_total_channels_by_speaker_mode(SpeakerMode) const;
AudioDriver::MicrophoneReciever *create_microphone_reciever(const StringName &p_device_name);
void destroy_microphone_reciever(AudioDriver::MicrophoneReciever *p_microphone_reciever);
Vector<int32_t> get_audio_input_buffer() { return audio_input_buffer; }
unsigned int get_audio_input_position() { return audio_input_position; }
#ifdef DEBUG_ENABLED
uint64_t get_profiling_time() const { return prof_time; }
@ -379,7 +262,6 @@ private:
friend class AudioDriver;
void _driver_process(int p_frames, int32_t *p_buffer);
void _change_default_device(StringName p_recording_device_default_name);
protected:
static void _bind_methods();
@ -479,10 +361,9 @@ public:
String get_device();
void set_device(String device);
AudioDriver::MicrophoneReciever *create_microphone_reciever(const StringName &p_device_name);
void destroy_microphone_reciever(AudioDriver::MicrophoneReciever *p_microphone_reciever);
PoolStringArray audio_in_get_device_names();
Array capture_get_device_list();
StringName capture_get_device();
void capture_set_device(StringName device);
float get_output_latency() { return output_latency; }
AudioServer();