Skip to content

Commit 6b74d65

Browse files
committed
Add MicrophoneFeed::{FormatId,FormatFlag}
1 parent e461da1 commit 6b74d65

File tree

3 files changed

+79
-43
lines changed

3 files changed

+79
-43
lines changed

drivers/apple/microphone_driver_avfoundation.mm

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "servers/microphone/microphone_feed.h"
3535
#include "servers/microphone/microphone_server.h"
3636

37+
#include <AVFAudio/AVFAudio.h>
3738
#include <CoreAudioTypes/CoreAudioBaseTypes.h>
3839

3940
MicrophoneDriverAVFoundation::FeedEntry *MicrophoneDriverAVFoundation::get_feed_entry_from_feed(const Ref<MicrophoneFeed> p_feed) const {
@@ -57,9 +58,9 @@
5758

5859
p_feed->set_sample_rate(audio_format_stream_basic_description->mSampleRate);
5960
p_feed->set_channels_per_frame(audio_format_stream_basic_description->mChannelsPerFrame);
60-
p_feed->set_bytes_per_frame(audio_format_stream_basic_description->mBytesPerFrame);
61+
p_feed->set_bit_depth(audio_format_stream_basic_description->mBitsPerChannel);
6162

62-
BitField<MicrophoneFeed::MicrophoneFeedFormatFlag> feed_flags = MicrophoneFeed::MICROPHONE_FEED_FORMAT_FLAG_NONE;
63+
BitField<MicrophoneFeed::FormatFlag> feed_flags = MicrophoneFeed::MICROPHONE_FEED_FORMAT_FLAG_NONE;
6364

6465
#define HAS_FLAG(flag) audio_format_stream_basic_description->mFormatFlags &flag
6566
#define SET_IF_HAS_FLAG(apple_flag, microphone_feed_flag) \
@@ -189,14 +190,11 @@
189190
}
190191

191192
bool MicrophoneDriverAVFoundation::activate_feed_entry(FeedEntry *p_feed_entry) {
192-
print_line(vformat("activate_feed_entry"));
193-
194193
if (p_feed_entry->capture_session) {
195194
return true;
196195
}
197196

198197
auto init_device_capture_session = [&p_feed_entry]() -> void {
199-
print_line(vformat("start capture"));
200198
p_feed_entry->capture_session = [[MicrophoneDeviceCaptureSession alloc] initForFeed:p_feed_entry->feed
201199
andDevice:p_feed_entry->device];
202200
p_feed_entry->feed->emit_signal(SNAME("feed_activated"));
@@ -332,6 +330,40 @@ - (id)initForFeed:(Ref<MicrophoneFeed>)pFeed
332330
NSError *error;
333331
feed = pFeed;
334332

333+
BitField<MicrophoneFeed::FormatFlag> feedFlags = pFeed->get_format_flags();
334+
NSDictionary<NSString *, id> *settings = nil;
335+
336+
switch (pFeed->get_format_id()) {
337+
case MicrophoneFeed::MICROPHONE_FEED_FORMAT_ID_LINEAR_PCM: {
338+
settings = @{
339+
AVFormatIDKey : [NSNumber numberWithInt:kAudioFormatLinearPCM],
340+
AVSampleRateKey : [NSNumber numberWithFloat:feed->get_sample_rate()],
341+
AVNumberOfChannelsKey : [NSNumber numberWithInt:feed->get_channels_per_frame()],
342+
AVLinearPCMIsBigEndianKey : [NSNumber numberWithBool:feedFlags.has_flag(MicrophoneFeed::MICROPHONE_FEED_FORMAT_FLAG_IS_BIG_ENDIAN)],
343+
AVLinearPCMIsFloatKey : [NSNumber numberWithBool:feedFlags.has_flag(MicrophoneFeed::MICROPHONE_FEED_FORMAT_FLAG_IS_FLOAT)],
344+
AVLinearPCMIsNonInterleavedKey : [NSNumber numberWithBool:feedFlags.has_flag(MicrophoneFeed::MICROPHONE_FEED_FORMAT_FLAG_IS_NON_INTERLEAVED)],
345+
AVLinearPCMBitDepthKey : [NSNumber numberWithInt:feed->get_bit_depth()],
346+
};
347+
} break;
348+
case MicrophoneFeed::MICROPHONE_FEED_FORMAT_ID_ALAW: {
349+
settings = @{
350+
AVFormatIDKey : [NSNumber numberWithInt:kAudioFormatALaw],
351+
AVSampleRateKey : [NSNumber numberWithFloat:feed->get_sample_rate()],
352+
AVNumberOfChannelsKey : [NSNumber numberWithInt:feed->get_channels_per_frame()]
353+
};
354+
} break;
355+
case MicrophoneFeed::MICROPHONE_FEED_FORMAT_ID_ULAW: {
356+
settings = @{
357+
AVFormatIDKey : [NSNumber numberWithInt:kAudioFormatULaw],
358+
AVSampleRateKey : [NSNumber numberWithFloat:feed->get_sample_rate()],
359+
AVNumberOfChannelsKey : [NSNumber numberWithInt:feed->get_channels_per_frame()]
360+
};
361+
} break;
362+
case MicrophoneFeed::MICROPHONE_FEED_FORMAT_ID_MAX: {
363+
ERR_FAIL_V(nil);
364+
} break;
365+
}
366+
335367
[self beginConfiguration];
336368

337369
inputDevice = [AVCaptureDeviceInput
@@ -343,21 +375,7 @@ - (id)initForFeed:(Ref<MicrophoneFeed>)pFeed
343375
dataOutput = [AVCaptureAudioDataOutput new];
344376
ERR_FAIL_COND_V_MSG(!dataOutput, self, vformat(R"*(could not get data output for MicrophoneFeed "%s")*", feed->get_name()));
345377

346-
AudioFormatID formatId;
347-
switch (pFeed->get_format_id()) {
348-
case MicrophoneFeed::MICROPHONE_FEED_FORMAT_ID_LINEAR_PCM:
349-
default: {
350-
formatId = kAudioFormatLinearPCM;
351-
} break;
352-
}
353-
354-
NSDictionary<NSString *, id> *settings = @{
355-
AVFormatIDKey : [NSNumber numberWithInt:formatId],
356-
AVSampleRateKey : [NSNumber numberWithFloat:feed->get_sample_rate()],
357-
AVNumberOfChannelsKey : [NSNumber numberWithInt:feed->get_channels_per_frame()]
358-
};
359378
dataOutput.audioSettings = settings;
360-
361379
[dataOutput setSampleBufferDelegate:self queue:dispatch_get_main_queue()];
362380

363381
[self addOutput:dataOutput];

servers/microphone/microphone_feed.cpp

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,13 @@
3030

3131
#include "microphone_feed.h"
3232

33+
#include "core/object/class_db.h"
3334
#include "core/object/object.h"
3435
#include "servers/microphone/microphone_driver.h"
3536
#include "servers/microphone/microphone_server.h"
3637

3738
void MicrophoneFeed::update_ring_buffer_size() {
38-
uint64_t new_ring_buffer_size = (uint64_t)(buffer_length * sample_rate) * channels_per_frame * bytes_per_frame;
39+
uint64_t new_ring_buffer_size = (uint64_t)(buffer_length * sample_rate) * channels_per_frame * bit_depth;
3940
if (new_ring_buffer_size == ring_buffer_size) {
4041
return;
4142
}
@@ -94,14 +95,21 @@ void MicrophoneFeed::_bind_methods() {
9495
ClassDB::bind_method(D_METHOD("set_name", "name"), &MicrophoneFeed::set_name);
9596
ClassDB::bind_method(D_METHOD("get_description"), &MicrophoneFeed::get_description);
9697
ClassDB::bind_method(D_METHOD("set_description", "description"), &MicrophoneFeed::set_description);
98+
99+
ClassDB::bind_method(D_METHOD("get_format_id"), &MicrophoneFeed::get_format_id);
100+
ClassDB::bind_method(D_METHOD("set_format_id", "format_id"), &MicrophoneFeed::set_format_id);
101+
ClassDB::bind_method(D_METHOD("get_format_flags"), &MicrophoneFeed::get_format_flags);
102+
ClassDB::bind_method(D_METHOD("set_format_flags", "format_flags"), &MicrophoneFeed::set_format_flags);
103+
97104
ClassDB::bind_method(D_METHOD("get_sample_rate"), &MicrophoneFeed::get_sample_rate);
98105
ClassDB::bind_method(D_METHOD("set_sample_rate", "sample_rate"), &MicrophoneFeed::set_sample_rate);
99106
ClassDB::bind_method(D_METHOD("get_buffer_length"), &MicrophoneFeed::get_buffer_length);
100107
ClassDB::bind_method(D_METHOD("set_buffer_length", "buffer_length"), &MicrophoneFeed::set_buffer_length);
101108
ClassDB::bind_method(D_METHOD("get_channels_per_frame"), &MicrophoneFeed::get_channels_per_frame);
102109
ClassDB::bind_method(D_METHOD("set_channels_per_frame", "channels_per_frame"), &MicrophoneFeed::set_channels_per_frame);
110+
ClassDB::bind_method(D_METHOD("get_bit_depth"), &MicrophoneFeed::get_bit_depth);
111+
ClassDB::bind_method(D_METHOD("set_bit_depth", "bit_depth"), &MicrophoneFeed::set_bit_depth);
103112
ClassDB::bind_method(D_METHOD("get_bytes_per_frame"), &MicrophoneFeed::get_bytes_per_frame);
104-
ClassDB::bind_method(D_METHOD("set_bytes_per_frame", "bytes_per_frame"), &MicrophoneFeed::set_bytes_per_frame);
105113

106114
ClassDB::bind_method(D_METHOD("get_buffer"), &MicrophoneFeed::get_buffer);
107115
ClassDB::bind_method(D_METHOD("clear_buffer"), &MicrophoneFeed::clear_buffer);
@@ -115,10 +123,25 @@ void MicrophoneFeed::_bind_methods() {
115123
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "sample_rate"), "set_sample_rate", "get_sample_rate");
116124
ADD_PROPERTY(PropertyInfo(Variant::INT, "buffer_length"), "set_buffer_length", "get_buffer_length");
117125
ADD_PROPERTY(PropertyInfo(Variant::INT, "channels_per_frame"), "set_channels_per_frame", "get_channels_per_frame");
118-
ADD_PROPERTY(PropertyInfo(Variant::INT, "bytes_per_frame"), "set_bytes_per_frame", "get_bytes_per_frame");
126+
ADD_PROPERTY(PropertyInfo(Variant::INT, "bit_depth"), "set_bit_depth", "get_bit_depth");
119127

120128
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "active"), "set_active", "is_active");
121129

122130
ADD_SIGNAL(MethodInfo(SNAME("feed_activated")));
123131
ADD_SIGNAL(MethodInfo(SNAME("feed_deactivated")));
132+
133+
BIND_ENUM_CONSTANT(MICROPHONE_FEED_FORMAT_ID_ALAW);
134+
BIND_ENUM_CONSTANT(MICROPHONE_FEED_FORMAT_ID_ULAW);
135+
BIND_ENUM_CONSTANT(MICROPHONE_FEED_FORMAT_ID_LINEAR_PCM);
136+
BIND_ENUM_CONSTANT(MICROPHONE_FEED_FORMAT_ID_MAX);
137+
138+
BIND_BITFIELD_FLAG(MICROPHONE_FEED_FORMAT_FLAG_NONE);
139+
BIND_BITFIELD_FLAG(MICROPHONE_FEED_FORMAT_FLAG_IS_ALIGNED_HIGH);
140+
BIND_BITFIELD_FLAG(MICROPHONE_FEED_FORMAT_FLAG_IS_BIG_ENDIAN);
141+
BIND_BITFIELD_FLAG(MICROPHONE_FEED_FORMAT_FLAG_IS_FLOAT);
142+
BIND_BITFIELD_FLAG(MICROPHONE_FEED_FORMAT_FLAG_IS_NON_INTERLEAVED);
143+
BIND_BITFIELD_FLAG(MICROPHONE_FEED_FORMAT_FLAG_IS_NON_MIXABLE);
144+
BIND_BITFIELD_FLAG(MICROPHONE_FEED_FORMAT_FLAG_IS_PACKED);
145+
BIND_BITFIELD_FLAG(MICROPHONE_FEED_FORMAT_FLAG_IS_SIGNED_INTEGER);
146+
BIND_BITFIELD_FLAG(MICROPHONE_FEED_FORMAT_FLAG_ALL);
124147
}

servers/microphone/microphone_feed.h

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,14 @@ class MicrophoneFeed : public RefCounted {
4545
friend MicrophoneDriver;
4646

4747
public:
48-
enum MicrophoneFeedFormatId {
48+
enum FormatId {
4949
MICROPHONE_FEED_FORMAT_ID_ALAW,
5050
MICROPHONE_FEED_FORMAT_ID_ULAW,
5151
MICROPHONE_FEED_FORMAT_ID_LINEAR_PCM,
5252
MICROPHONE_FEED_FORMAT_ID_MAX,
5353
};
5454

55-
enum MicrophoneFeedFormatFlag {
55+
enum FormatFlag {
5656
MICROPHONE_FEED_FORMAT_FLAG_NONE = 0,
5757
MICROPHONE_FEED_FORMAT_FLAG_IS_ALIGNED_HIGH = 1 << 0,
5858
MICROPHONE_FEED_FORMAT_FLAG_IS_BIG_ENDIAN = 1 << 1,
@@ -73,21 +73,17 @@ class MicrophoneFeed : public RefCounted {
7373

7474
String name;
7575
String description;
76-
MicrophoneFeedFormatId format_id = MICROPHONE_FEED_FORMAT_ID_LINEAR_PCM;
77-
BitField<MicrophoneFeedFormatFlag> format_flags = 0;
76+
FormatId format_id = MICROPHONE_FEED_FORMAT_ID_LINEAR_PCM;
77+
BitField<FormatFlag> format_flags = 0;
7878
double sample_rate = 44100;
7979
uint32_t channels_per_frame = 1;
80-
uint32_t bytes_per_frame = sizeof(float);
80+
uint32_t bit_depth = sizeof(float) * 8;
8181

8282
mutable RingBuffer<uint8_t> ring_buffer;
8383
uint64_t ring_buffer_size = 0;
8484

8585
float buffer_length = 0.5;
8686

87-
inline uint32_t get_bits_per_channel() {
88-
return (8 * bytes_per_frame) / channels_per_frame;
89-
}
90-
9187
void resize_buffer();
9288
void update_ring_buffer_size();
9389

@@ -101,10 +97,10 @@ class MicrophoneFeed : public RefCounted {
10197
String get_description() const { return description; }
10298
void set_description(String p_description) { description = p_description; }
10399

104-
void set_format_flags(BitField<MicrophoneFeedFormatFlag> p_format_flags) { format_flags = p_format_flags; }
105-
BitField<MicrophoneFeedFormatFlag> get_format_flags() const { return format_flags; }
106-
MicrophoneFeedFormatId get_format_id() const { return format_id; }
107-
void set_format_id(MicrophoneFeedFormatId p_format_id) { format_id = p_format_id; }
100+
void set_format_flags(BitField<FormatFlag> p_format_flags) { format_flags = p_format_flags; }
101+
BitField<FormatFlag> get_format_flags() const { return format_flags; }
102+
FormatId get_format_id() const { return format_id; }
103+
void set_format_id(FormatId p_format_id) { format_id = p_format_id; }
108104

109105
float get_sample_rate() const { return sample_rate; }
110106
void set_sample_rate(float p_sample_rate) {
@@ -133,14 +129,10 @@ class MicrophoneFeed : public RefCounted {
133129
update_ring_buffer_size();
134130
}
135131

136-
uint32_t get_bytes_per_frame() const { return bytes_per_frame; }
137-
void set_bytes_per_frame(uint32_t p_bytes_per_frame) {
138-
if (bytes_per_frame == p_bytes_per_frame) {
139-
return;
140-
}
141-
bytes_per_frame = p_bytes_per_frame;
142-
update_ring_buffer_size();
143-
}
132+
uint32_t get_bit_depth() const { return bit_depth; }
133+
void set_bit_depth(const uint32_t p_bit_depth) { bit_depth = p_bit_depth; }
134+
135+
inline uint32_t get_bytes_per_frame() const { return channels_per_frame * bit_depth; }
144136

145137
PackedByteArray get_buffer() const;
146138
void clear_buffer();
@@ -154,3 +146,6 @@ class MicrophoneFeed : public RefCounted {
154146
MicrophoneFeed(String p_name);
155147
virtual ~MicrophoneFeed();
156148
};
149+
150+
VARIANT_ENUM_CAST(MicrophoneFeed::FormatId);
151+
VARIANT_BITFIELD_CAST(MicrophoneFeed::FormatFlag);

0 commit comments

Comments
 (0)