瀏覽代碼

AudioStreamWAV: Inline tag remap inside load

DeeJayLSP 3 月之前
父節點
當前提交
afcc647886
共有 2 個文件被更改,包括 29 次插入36 次删除
  1. 29 31
      scene/resources/audio_stream_wav.cpp
  2. 0 5
      scene/resources/audio_stream_wav.h

+ 29 - 31
scene/resources/audio_stream_wav.cpp

@@ -485,10 +485,6 @@ Dictionary AudioStreamWAV::get_tags() const {
 	return tags;
 	return tags;
 }
 }
 
 
-HashMap<String, String>::ConstIterator AudioStreamWAV::remap_tag_id(const String &p_tag_id) {
-	return tag_id_remaps.find(p_tag_id);
-}
-
 double AudioStreamWAV::get_length() const {
 double AudioStreamWAV::get_length() const {
 	int len = data_bytes;
 	int len = data_bytes;
 	switch (format) {
 	switch (format) {
@@ -1146,16 +1142,37 @@ Ref<AudioStreamWAV> AudioStreamWAV::load_from_buffer(const Vector<uint8_t> &p_st
 	sample->set_loop_end(loop_end);
 	sample->set_loop_end(loop_end);
 	sample->set_stereo(format_channels == 2);
 	sample->set_stereo(format_channels == 2);
 
 
-	Dictionary tag_dictionary;
-	for (const KeyValue<String, String> &E : tag_map) {
-		HashMap<String, String>::ConstIterator remap = sample->remap_tag_id(E.key);
-		if (remap) {
-			tag_map.replace_key(E.key, remap->value);
-		}
+	if (!tag_map.is_empty()) {
+		// Used to make the metadata tags more unified across different AudioStreams.
+		// See https://www.recordingblogs.com/wiki/list-chunk-of-a-wave-file
+		HashMap<String, String> tag_id_remaps;
+		tag_id_remaps.reserve(15);
+		tag_id_remaps["IARL"] = "location";
+		tag_id_remaps["IART"] = "artist";
+		tag_id_remaps["ICMS"] = "organization";
+		tag_id_remaps["ICMT"] = "comments";
+		tag_id_remaps["ICOP"] = "copyright";
+		tag_id_remaps["ICRD"] = "date";
+		tag_id_remaps["IGNR"] = "genre";
+		tag_id_remaps["IKEY"] = "keywords";
+		tag_id_remaps["IMED"] = "medium";
+		tag_id_remaps["INAM"] = "title";
+		tag_id_remaps["IPRD"] = "album";
+		tag_id_remaps["ISBJ"] = "description";
+		tag_id_remaps["ISFT"] = "software";
+		tag_id_remaps["ITRK"] = "tracknumber";
+		Dictionary tag_dictionary;
+		for (const KeyValue<String, String> &E : tag_map) {
+			HashMap<String, String>::ConstIterator remap = tag_id_remaps.find(E.key);
+			String tag_key = E.key;
+			if (remap) {
+				tag_key = remap->value;
+			}
 
 
-		tag_dictionary[E.key] = E.value;
+			tag_dictionary[tag_key] = E.value;
+		}
+		sample->set_tags(tag_dictionary);
 	}
 	}
-	sample->set_tags(tag_dictionary);
 
 
 	return sample;
 	return sample;
 }
 }
@@ -1215,22 +1232,3 @@ void AudioStreamWAV::_bind_methods() {
 	BIND_ENUM_CONSTANT(LOOP_PINGPONG);
 	BIND_ENUM_CONSTANT(LOOP_PINGPONG);
 	BIND_ENUM_CONSTANT(LOOP_BACKWARD);
 	BIND_ENUM_CONSTANT(LOOP_BACKWARD);
 }
 }
-
-AudioStreamWAV::AudioStreamWAV() {
-	// Used to make the metadata tags more unified across different AudioStreams.
-	// See https://www.recordingblogs.com/wiki/list-chunk-of-a-wave-file
-	tag_id_remaps["IARL"] = "location";
-	tag_id_remaps["IART"] = "artist";
-	tag_id_remaps["ICMS"] = "organization";
-	tag_id_remaps["ICMT"] = "comments";
-	tag_id_remaps["ICOP"] = "copyright";
-	tag_id_remaps["ICRD"] = "date";
-	tag_id_remaps["IGNR"] = "genre";
-	tag_id_remaps["IKEY"] = "keywords";
-	tag_id_remaps["IMED"] = "medium";
-	tag_id_remaps["INAM"] = "title";
-	tag_id_remaps["IPRD"] = "album";
-	tag_id_remaps["ISBJ"] = "description";
-	tag_id_remaps["ISFT"] = "software";
-	tag_id_remaps["ITRK"] = "tracknumber";
-}

+ 0 - 5
scene/resources/audio_stream_wav.h

@@ -124,7 +124,6 @@ private:
 	LocalVector<uint8_t> data;
 	LocalVector<uint8_t> data;
 	uint32_t data_bytes = 0;
 	uint32_t data_bytes = 0;
 
 
-	HashMap<String, String> tag_id_remaps;
 	Dictionary tags;
 	Dictionary tags;
 
 
 protected:
 protected:
@@ -155,8 +154,6 @@ public:
 	void set_tags(const Dictionary &p_tags);
 	void set_tags(const Dictionary &p_tags);
 	virtual Dictionary get_tags() const override;
 	virtual Dictionary get_tags() const override;
 
 
-	HashMap<String, String>::ConstIterator remap_tag_id(const String &p_tag_id);
-
 	virtual double get_length() const override; //if supported, otherwise return 0
 	virtual double get_length() const override; //if supported, otherwise return 0
 
 
 	virtual bool is_monophonic() const override;
 	virtual bool is_monophonic() const override;
@@ -292,8 +289,6 @@ public:
 			dst_ptr += qoa_encode_frame(data16.ptr(), p_desc, frame_len, dst_ptr);
 			dst_ptr += qoa_encode_frame(data16.ptr(), p_desc, frame_len, dst_ptr);
 		}
 		}
 	}
 	}
-
-	AudioStreamWAV();
 };
 };
 
 
 VARIANT_ENUM_CAST(AudioStreamWAV::Format)
 VARIANT_ENUM_CAST(AudioStreamWAV::Format)