C
C#2y ago
Majinko :D

❔ Reading a WAV file

I am trying to read wav files and save the DATA to a float array, but the array has a length of about 40 with all wav files I have tried to use. I think the issue is reading the bytes as up until then the sample rate, bit depth works fine. The issue could be the WAV files themselves aswell as they are downloaded from youtube. The code is from stackoverflow: https://stackoverflow.com/questions/8754111/how-to-read-the-data-in-a-wav-file-to-an-array and it is the post by P i (the one that is just one readWav() function)
Stack Overflow
How to read the data in a wav file to an array
I need to get all the samples of a wav file into an array (or two if you need to do that to keep the stereo) so that I can apply some modifications to them. I was wondering if this is easily done (
21 Replies
Buddy
Buddy2y ago
WAV Files: File Structure, Case Analysis and PCM Explained
Learn the definition and file structure of wave format here, with an explanation of the PCM that stores the wav file.
Buddy
Buddy2y ago
Will maybe help Offset for data seems to be 44 based on Subchunk2Size
Majinko :D
Majinko :DOP2y ago
Do you know where I could get a clean WAV file? Beacuse when I used a hex editor I found out the WAV files downloaded from youtube have a ton of bytes that are just 00? And I dont think they should be there I am so confused man, I work on this with my teacher and for some reason his WAV got read successfully and about 1,5million values were stored in the array and I cannot get it to work at all ;(
Buddy
Buddy2y ago
You can use https://imhex.werwolv.net/ to create your own format and parse it. to debunk the format issue
ImHex
A Hex Editor for Reverse Engineers, Programmers and people who value their retinas when working at 3 AM.
Buddy
Buddy2y ago
The pattern used is similar to that of C. Are you sure that the site is actually converting them? and not just renaming the extension
Majinko :D
Majinko :DOP2y ago
well I have already looked at the hex but I really lack the knowledge in this but honestly based on the site you provided the WAV file doesnt look right
Zombie
Zombie2y ago
I would recommend using NAudio for this You can load the file using that library and write the samples out
Majinko :D
Majinko :DOP2y ago
Yeah, my teacher told me to do it without using a library, but if I wont be able to do it over the weekend I will probably have to
Zombie
Zombie2y ago
Ah, so this is for a class and you have to do it manually
Majinko :D
Majinko :DOP2y ago
Well technically, it is not graded. I am also not forced to not use a library he just said that it would be preffered
Zombie
Zombie2y ago
I would recommend using NAudio then, get the program functioning, and then slowly replace parts of it with your own code You could even study how NAudio works to figure out what you might be missing
Majinko :D
Majinko :DOP2y ago
Aight that sound like a good idea
Zombie
Zombie2y ago
It'll probably be more motivating too, it's a lot more enjoyable when you aren't hitting a brick wall and you have something working right in front of you You could also try writing some code that loads the audio file with NAudio and your code, then compare the data NAudio gives you with what you read If you get something wrong, that would make it much easier to figure out what it is
Majinko :D
Majinko :DOP2y ago
Ok thanks for the help Just found out the code actually works if the WAV file is structured right So I guess i shouldnt use online converters
Buddy
Buddy2y ago
Yeah, converters usually forks it up, or just straight out renames the extension
Majinko :D
Majinko :DOP2y ago
FINALLY Audacity actually is able to convert a MP3 file to WAV without adding unnecessary bytes at the start of the file
Buddy
Buddy2y ago
You can also validate that it is a WAV file by checking the magic header
Majinko :D
Majinko :DOP2y ago
Right lads thanks for the help
Buddy
Buddy2y ago
https://en.wikipedia.org/wiki/List_of_file_signatures
52 49 46 46 ?? ?? ?? ??
57 41 56 45
52 49 46 46 ?? ?? ?? ??
57 41 56 45
RIFF????WAVE
RIFF????WAVE
it should always start with those bytes (pattern)
Majinko :D
Majinko :DOP2y ago
Thanks man, this showed me a lot about file structure and hex code it is pretty interesting
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server