AudioStreamSample: Don't crash when writing to file fails

This commit is contained in:
merumelu 2019-03-12 15:48:52 +01:00
parent 291c281fcf
commit db8f26c8e5
3 changed files with 11 additions and 6 deletions

View file

@ -12,11 +12,13 @@
</demos> </demos>
<methods> <methods>
<method name="save_to_wav"> <method name="save_to_wav">
<return type="void"> <return type="int" enum="Error">
</return> </return>
<argument index="0" name="path" type="String"> <argument index="0" name="path" type="String">
</argument> </argument>
<description> <description>
Saves the AudioStreamSample as a WAV file to [code]path[/code]. Samples with IMA ADPCM format can't be saved.
Note that a [code].wav[/code] extension is automatically appended to [code]path[/code] if it is missing.
</description> </description>
</method> </method>
</methods> </methods>

View file

@ -515,10 +515,10 @@ PoolVector<uint8_t> AudioStreamSample::get_data() const {
return pv; return pv;
} }
void AudioStreamSample::save_to_wav(String p_path) { Error AudioStreamSample::save_to_wav(const String &p_path) {
if (format == AudioStreamSample::FORMAT_IMA_ADPCM) { if (format == AudioStreamSample::FORMAT_IMA_ADPCM) {
WARN_PRINTS("Saving IMA_ADPC samples are not supported yet"); WARN_PRINTS("Saving IMA_ADPC samples are not supported yet");
return; return ERR_UNAVAILABLE;
} }
int sub_chunk_2_size = data_bytes; //Subchunk2Size = Size of data in bytes int sub_chunk_2_size = data_bytes; //Subchunk2Size = Size of data in bytes
@ -544,8 +544,9 @@ void AudioStreamSample::save_to_wav(String p_path) {
file_path += ".wav"; file_path += ".wav";
} }
Error err; FileAccessRef file = FileAccess::open(file_path, FileAccess::WRITE); //Overrides existing file if present
FileAccess *file = FileAccess::open(file_path, FileAccess::WRITE, &err); //Overrides existing file if present
ERR_FAIL_COND_V(!file, ERR_FILE_CANT_WRITE);
// Create WAV Header // Create WAV Header
file->store_string("RIFF"); //ChunkID file->store_string("RIFF"); //ChunkID
@ -583,6 +584,8 @@ void AudioStreamSample::save_to_wav(String p_path) {
} }
file->close(); file->close();
return OK;
} }
Ref<AudioStreamPlayback> AudioStreamSample::instance_playback() { Ref<AudioStreamPlayback> AudioStreamSample::instance_playback() {

View file

@ -141,7 +141,7 @@ public:
void set_data(const PoolVector<uint8_t> &p_data); void set_data(const PoolVector<uint8_t> &p_data);
PoolVector<uint8_t> get_data() const; PoolVector<uint8_t> get_data() const;
void save_to_wav(String p_path); Error save_to_wav(const String &p_path);
virtual Ref<AudioStreamPlayback> instance_playback(); virtual Ref<AudioStreamPlayback> instance_playback();
virtual String get_stream_name() const; virtual String get_stream_name() const;