gianclgar / gdscriptaudioimport Goto Github PK
View Code? Open in Web Editor NEWA script in GDScript for importing .wav and .ogg audio files at runtime
License: MIT License
A script in GDScript for importing .wav and .ogg audio files at runtime
License: MIT License
As the title suggest, longer wav files stop early.
I figured out that this can be fixed by replacing all << 32 operators with << 24.
This fixes the conversion of 4 bytes to an integer according to this: https://stackoverflow.com/questions/48321184/how-does-the-byte-order-fallacy-code-work#:~:text=endianness,places%20on%20StackOverflow%20as%20well.
Thank you very much for this!
Now that Godot 3.2.4 supports mp3, it would be easy to add mp3 support, it seems to work fine just like with ogg.
I tried this in RC4 and it seems to work just fine:
insert after the ogg part:
#if file is mp3
elif filepath.ends_with(".mp3"):
var newstream = AudioStreamMP3.new()
newstream.loop = true #set to false or delet this line if you dont want to loop
newstream.data = bytes
return newstream
It'd be wise to wait until 3.2.4 comes out though. I'll make a pr.
I have some mp3 files where extension is written wrong, with capitalized letters
file.MP3
This is quite rare case but I think it could to be handled.
For example godot editor handles it, when I play audiofiles in Inspector
Godot 4 came out recently. All code except .ogg can easily be translated to godot 4 mostly by applying the name changes (File > fIleAccess, PoolByteArray -> PackedByteArray, etc.).
Don't know if there is some magic for loading .ogg in godot 4 at runtime at this time, but the remainder should be updated.
As the title suggest: I found a wav file that doesn't play.
I can't send it as it's too large, but the data starts at ~byte 700, and it contains artist names + a junk chunk
changing the loop to go to that causes it to make weird glitchy sound.
I don't really know how to solve this one sadly, but it works fine when imported with the normal audio importer: https://github.com/godotengine/godot/blob/master/editor/import/resource_importer_wav.cpp
Example file: https://mega.nz/file/tFdXlSiL#YWdCsuFSaRtWa8felZX1FWsVer247oAjGyEXPFV34Yk
Thanks for your efforts developing GDScriptAudioImport
! I was glad to find this project existed when I discovered Godot unexpectedly didn't handle parsing WAV files at run time!
A couple of suggestions if you or anyone else happens to develop the code any further:
GDScriptAudioImport.gd
.get_*
family of functions for the parsing data types rather than implementing the byte/offset handling "manually": https://docs.godotengine.org/en/stable/classes/class_file.html#class-file-method-get-16get_*
functions based on changing the set_*
operations in Godot's WAV save implementation to get_*
operations. :D (Or, indeed, some of the code in the WAV resource importer, e.g. here.)PoolByteArray
in memory, it's still possible to use get_*
family style parsing methods but it's not at all obvious how...StreamPeerBuffer
class which also supports the get_*
/set_*
data type parsing methods--but this isn't immediately obvious because the methods are actually implemented on the StreamPeer
class from which it inherits.StreamPeerBuffer
class in use.StreamPeerBuffer
.The...ah..."un-intuitively"-named
StreamPeerBuffer
is un-intuitively named because it has no Peer :) and it's not a Stream. It's just a Buffer...with some useful helper methods.The naming is because it's based on a class that is used by the family of classes that do have Peer(s) & are Streams.
I would be inclined to make the modifications myself just to tidy up the implementation a bit but given this functionality will hopefully be (re-)implemented in engine at some point* it's not obvious that it's worth spending the time refining the existing code further.
Anyway, thanks again & hope some of these notes are useful to someone along the way. :)
This wav file https://drive.google.com/file/d/1sSCpvA-5a7nL2PV8vW3Yo08jo_GNiZ6_/view?usp=sharing
and many others generate noise instead of actual sound when using loadfile(). Could someone check it?
nevermind
Hey, first, thank you for providing such script.
I've tried to load a .wav file (too big to send here), and the format code that was parsed was 3, so it's not 8_BITS
, 16_BITS
or IMA_ADPCM
.
I'm not sure why, but I can provide you with more information or the file if you want to try.
I had some trouble finding the project again after discovering it last week. I know realise this might be because of the missing tags.
I would expect to be able to find the project for example like this: https://github.com/search?o=desc&q=godot+audio&s=stars&type=Repositories
Ideas for fitting tags:
Possibly some other tags from the other projects you can see in the list, but I think those are the main ones. I hope that works, I am not 100% familiar with how the GitHub search works.
I am aware that the search is definitely able to access the desription: https://github.com/search?q=A+script+in+GDScript+for+importing+.wav+and+.ogg+audio+files+at+runtime&type=Repositories so if adding the tags does not work maybe somehow add Godot in the description. But hopefully adding the tags is enough.
unable to put newstream.data into $AudioStreamPlayer.stream and play it manually.
BTW,
func _on_openmusic_file_selected(path):
var file = File.new()
file.open(path, File.READ)
$AudioStreamPlayer.stream = file.get_current_file()
Idk why this doesn't work with wav mp3 or ogg files.
But if I use your code,
I can't even hear anything or manually control it.
Just asking, should I put your code into the $audiostreamplayer or it's ok to put it in the main node?
thanks for answering!
In line 143 the samplenum
for setting loop_end is calculated wrong. It assumes 4 bytes per sample, which isn't always correct and hence causes the loop to start earlier in some cases (e.g. 16 bit per sample / mono). As the bits_per_sample is calculated above it should be used to calculate the correct samplenum
instead.
OLD: var samplenum = newstream.data.size() / 4
(line 143)
NEW: var samplenum = newstream.data.size() / (bits_per_sample / 8)
Note, that the bits_per_sample is initialized with 0. Hence this might result in a division by 0. Hence, I suggest something like assert(bits_per_sample != 0) as done above in the code... or do some other error handling. Thanks!
With ogg files, they sometimes click or make another noise before they start playing normally, or don't start at all. This doesn't seem to depend on the file and happens at random, and importing it via the in-engine importer it works fine.
Adding a 300 millisecond delay before playing the file makes this issue occur less frequent.
I'm thinking this has to do with the engine still decoding the data, as the in-engine importer does the same as your code for loading ogg files.
Edit: formatting
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.