3232
3333#include " core/object/class_db.h"
3434#include " core/object/object.h"
35+ #include " core/string/string_builder.h"
3536#include " servers/microphone/microphone_driver.h"
3637#include " servers/microphone/microphone_server.h"
3738
3839void MicrophoneFeed::update_ring_buffer_size () {
39- uint64_t new_ring_buffer_size = (uint64_t )(buffer_length * sample_rate) * channels_per_frame * (bit_depth / 8 );
40+ uint64_t new_ring_buffer_size = (uint64_t )(buffer_length * sample_rate) * channels * (bit_depth / 8 );
4041 if (new_ring_buffer_size == ring_buffer_size) {
4142 return ;
4243 }
@@ -78,9 +79,61 @@ void MicrophoneFeed::deactivate_feed() {
7879 MicrophoneDriver::get_singleton ()->deactivate_feed (this );
7980}
8081
82+ String MicrophoneFeed::get_human_readable_explanation () {
83+ const String INDENT = String (" " ).repeat (2 );
84+ const String YES = " yes" ;
85+ const String NO = " no" ;
86+ #define BOOL_TO_YES_NO (value ) (value ? YES : NO)
87+ #define FORMAT_FLAG_TO_YES_NO (flag ) BOOL_TO_YES_NO(format_flags.has_flag(MicrophoneFeed::FormatFlag::flag))
88+
89+ StringBuilder root_builder;
90+ root_builder.append (vformat (" Name: %s\n " , name));
91+ root_builder.append (vformat (" Description: %s\n " , description));
92+ root_builder.append (vformat (" Sample rate: %s\n " , sample_rate));
93+ root_builder.append (vformat (" Channels: %s\n " , channels));
94+ root_builder.append (vformat (" Bit depth: %s\n " , bit_depth));
95+ String format_id_string;
96+ switch (format_id) {
97+ case FORMAT_ID_UNDEFINED: {
98+ format_id_string = " UNDEFINED [invalid value]" ;
99+ } break ;
100+ case FORMAT_ID_NOT_SUPPORTED: {
101+ format_id_string = vformat (" NOT SUPPORTED (%s) [invalid value]" , not_supported_format_id_name);
102+ } break ;
103+ case FORMAT_ID_ALAW_PCM: {
104+ format_id_string = " A-law PCM" ;
105+ } break ;
106+ case FORMAT_ID_ULAW_PCM: {
107+ format_id_string = " μ-law PCM" ;
108+ } break ;
109+ case FORMAT_ID_LINEAR_PCM: {
110+ format_id_string = " Linear PCM" ;
111+ } break ;
112+ case FORMAT_ID_MAX: {
113+ format_id_string = " MAX [invalid value]" ;
114+ } break ;
115+ }
116+ root_builder.append (vformat (" Format id: %s\n " , format_id_string));
117+ root_builder.append (" Format flags: \n " );
118+ StringBuilder flags_builder;
119+ flags_builder.append (vformat (" Aligned high: %s\n " , FORMAT_FLAG_TO_YES_NO (FORMAT_FLAG_IS_ALIGNED_HIGH)));
120+ flags_builder.append (vformat (" Big endian: %s\n " , FORMAT_FLAG_TO_YES_NO (FORMAT_FLAG_IS_BIG_ENDIAN)));
121+ flags_builder.append (vformat (" Float: %s\n " , FORMAT_FLAG_TO_YES_NO (FORMAT_FLAG_IS_FLOAT)));
122+ flags_builder.append (vformat (" Interleaved: %s\n " , FORMAT_FLAG_TO_YES_NO (FORMAT_FLAG_IS_INTERLEAVED)));
123+ flags_builder.append (vformat (" Mixable: %s\n " , FORMAT_FLAG_TO_YES_NO (FORMAT_FLAG_IS_MIXABLE)));
124+ flags_builder.append (vformat (" Packed: %s\n " , FORMAT_FLAG_TO_YES_NO (FORMAT_FLAG_IS_PACKED)));
125+ flags_builder.append (vformat (" Signed integer: %s\n " , FORMAT_FLAG_TO_YES_NO (FORMAT_FLAG_IS_SIGNED_INTEGER)));
126+ root_builder.append (flags_builder.as_string ().indent (INDENT));
127+
128+ return root_builder.as_string ();
129+
130+ #undef FORMAT_FLAG_TO_YES_NO
131+ #undef BOOL_TO_YES_NO
132+ }
133+
81134MicrophoneFeed::MicrophoneFeed () {
82135 id = -1 ;
83- name = " ??? " ;
136+ name = " <uninitialized> " ;
84137}
85138
86139MicrophoneFeed::~MicrophoneFeed () {
@@ -100,47 +153,57 @@ void MicrophoneFeed::_bind_methods() {
100153 ClassDB::bind_method (D_METHOD (" set_format_id" , " format_id" ), &MicrophoneFeed::set_format_id);
101154 ClassDB::bind_method (D_METHOD (" get_format_flags" ), &MicrophoneFeed::get_format_flags);
102155 ClassDB::bind_method (D_METHOD (" set_format_flags" , " format_flags" ), &MicrophoneFeed::set_format_flags);
156+ ClassDB::bind_method (D_METHOD (" get_not_supported_format_id_name" ), &MicrophoneFeed::get_not_supported_format_id_name);
157+ ClassDB::bind_method (D_METHOD (" set_not_supported_format_id_name" , " not_supported_format_id_name" ), &MicrophoneFeed::set_not_supported_format_id_name);
103158
104159 ClassDB::bind_method (D_METHOD (" get_sample_rate" ), &MicrophoneFeed::get_sample_rate);
105160 ClassDB::bind_method (D_METHOD (" set_sample_rate" , " sample_rate" ), &MicrophoneFeed::set_sample_rate);
106161 ClassDB::bind_method (D_METHOD (" get_buffer_length" ), &MicrophoneFeed::get_buffer_length);
107162 ClassDB::bind_method (D_METHOD (" set_buffer_length" , " buffer_length" ), &MicrophoneFeed::set_buffer_length);
108- ClassDB::bind_method (D_METHOD (" get_channels_per_frame " ), &MicrophoneFeed::get_channels_per_frame );
109- ClassDB::bind_method (D_METHOD (" set_channels_per_frame " , " channels_per_frame " ), &MicrophoneFeed::set_channels_per_frame );
163+ ClassDB::bind_method (D_METHOD (" get_channels " ), &MicrophoneFeed::get_channels );
164+ ClassDB::bind_method (D_METHOD (" set_channels " , " channels " ), &MicrophoneFeed::set_channels );
110165 ClassDB::bind_method (D_METHOD (" get_bit_depth" ), &MicrophoneFeed::get_bit_depth);
111166 ClassDB::bind_method (D_METHOD (" set_bit_depth" , " bit_depth" ), &MicrophoneFeed::set_bit_depth);
112167 ClassDB::bind_method (D_METHOD (" get_bytes_per_frame" ), &MicrophoneFeed::get_bytes_per_frame);
113168
114169 ClassDB::bind_method (D_METHOD (" get_buffer" ), &MicrophoneFeed::get_buffer);
115170 ClassDB::bind_method (D_METHOD (" clear_buffer" ), &MicrophoneFeed::clear_buffer);
116171
172+ ClassDB::bind_method (D_METHOD (" get_human_readable_explanation" ), &MicrophoneFeed::get_human_readable_explanation);
173+
117174 GDVIRTUAL_BIND (_activate_feed);
118175 GDVIRTUAL_BIND (_deactivate_feed);
119176
120177 ADD_PROPERTY (PropertyInfo (Variant::STRING, " name" ), " set_name" , " get_name" );
121178 ADD_PROPERTY (PropertyInfo (Variant::STRING, " description" ), " set_description" , " get_description" );
122179
180+ ADD_PROPERTY (PropertyInfo (Variant::INT, " format_id" , PROPERTY_HINT_ENUM, " Undefined,Not Supported,A-law PCM,μ-law PCM,Linear PCM" ), " set_format_id" , " get_format_id" );
181+ ADD_PROPERTY (PropertyInfo (Variant::INT, " format_flags" , PROPERTY_HINT_FLAGS, " Is Aligned High,Is Big Endian,Is Float,Is Interleaved,Is Mixable,Is Packed,Is Signed Integer" ), " set_format_flags" , " get_format_flags" );
182+ ADD_PROPERTY (PropertyInfo (Variant::STRING, " not_supported_format_id_name" , PROPERTY_HINT_FLAGS), " set_not_supported_format_id_name" , " get_not_supported_format_id_name" );
183+
123184 ADD_PROPERTY (PropertyInfo (Variant::FLOAT, " sample_rate" ), " set_sample_rate" , " get_sample_rate" );
124185 ADD_PROPERTY (PropertyInfo (Variant::INT, " buffer_length" ), " set_buffer_length" , " get_buffer_length" );
125- ADD_PROPERTY (PropertyInfo (Variant::INT, " channels_per_frame " ), " set_channels_per_frame " , " get_channels_per_frame " );
186+ ADD_PROPERTY (PropertyInfo (Variant::INT, " channels " ), " set_channels " , " get_channels " );
126187 ADD_PROPERTY (PropertyInfo (Variant::INT, " bit_depth" ), " set_bit_depth" , " get_bit_depth" );
127188
128189 ADD_PROPERTY (PropertyInfo (Variant::BOOL, " active" ), " set_active" , " is_active" );
129190
130191 ADD_SIGNAL (MethodInfo (SNAME (" activated" )));
131192 ADD_SIGNAL (MethodInfo (SNAME (" deactivated" )));
132193
133- BIND_ENUM_CONSTANT (FORMAT_ID_ALAW);
134- BIND_ENUM_CONSTANT (FORMAT_ID_ULAW);
194+ BIND_ENUM_CONSTANT (FORMAT_ID_UNDEFINED);
195+ BIND_ENUM_CONSTANT (FORMAT_ID_NOT_SUPPORTED);
196+ BIND_ENUM_CONSTANT (FORMAT_ID_ALAW_PCM);
197+ BIND_ENUM_CONSTANT (FORMAT_ID_ULAW_PCM);
135198 BIND_ENUM_CONSTANT (FORMAT_ID_LINEAR_PCM);
136199 BIND_ENUM_CONSTANT (FORMAT_ID_MAX);
137200
138201 BIND_BITFIELD_FLAG (FORMAT_FLAG_NONE);
139202 BIND_BITFIELD_FLAG (FORMAT_FLAG_IS_ALIGNED_HIGH);
140203 BIND_BITFIELD_FLAG (FORMAT_FLAG_IS_BIG_ENDIAN);
141204 BIND_BITFIELD_FLAG (FORMAT_FLAG_IS_FLOAT);
142- BIND_BITFIELD_FLAG (FORMAT_FLAG_IS_NON_INTERLEAVED );
143- BIND_BITFIELD_FLAG (FORMAT_FLAG_IS_NON_MIXABLE );
205+ BIND_BITFIELD_FLAG (FORMAT_FLAG_IS_INTERLEAVED );
206+ BIND_BITFIELD_FLAG (FORMAT_FLAG_IS_MIXABLE );
144207 BIND_BITFIELD_FLAG (FORMAT_FLAG_IS_PACKED);
145208 BIND_BITFIELD_FLAG (FORMAT_FLAG_IS_SIGNED_INTEGER);
146209 BIND_BITFIELD_FLAG (FORMAT_FLAG_ALL);
0 commit comments