I want to read wav files and get amplitude data in relation to time in dart to use it in dart and plot it in chart using flutter
At first i converted .mp3 and .m4a files to wav files using flutter_ffmpeg but i want to read those files and get sample rate and amplitude And try to plot already existing file to chart with amplitude on y axis and time on x axis
My question about extracting amplitude and other wav info from .wav or .pcm files as there is no documentation i found on web.
natural audio in the wild is a continuous wobble of a curve .. think here of your ear drum or the membrane of a microphone or a drum head ... digital audio is that same curve recorded as a progression of audio samples ... typically 44100 samples are recorded per second and each sample records 16 bits of resolution of the height of this curve meaning 2^16 == 65,536 possible distinct height values for a given point on the raw audio curve ( more detail research PCM audio ) ... so a single audio sample amplitude represents the curve height at a specific point in time lets call it s1 ... this height as plotted on the raw audio curve is its amplitude for that sample
when reading a WAV format file the first 44 bytes is a header followed by the payload which contains the raw audio curve of each channel of the audio ( mono one channel, stereo 2 channels, etc ) ... typically audio is recorded using many microphones however to create an audio CD the music studio mixes down multi track audio ( possibly dozens of channels originally ) into two channels of audio ( one for left speaker one for right speaker meaning stereo that is two channels ) ... this header will tell you these critical details of what appears in the payload: sample_rate ( number of audio samples captured per second per channel ), bit_depth of each sample ( number of bits of data used to store each audio sample for a given channel ) , payload size in bytes, and number of channels ... you can write a WAV parser yourself ( takes about two pages of code ) or use a library to retrieve these data structures ... once parsed the raw audio found in the WAV file payload will give you the raw audio curve s1, s2, s3, etc for each channel ...
typically when folks need to identify the amplitude they refer to an aggregate of this curve height of many audio samples ... s1, s2, s3, ... one way to skin this cat is to calculate the Root Mean Square of a set of audio samples to generate one value of currAmplitude aggregate amplitude then slide forward in time to repeat for another set of audio sample points ... the number of samples in a given RMS calculation is up to you perhaps 1k or 2k more or less depending on your appetite for CPU consumption and resolution of this aggregated amplitude measurement
currAmplitude = square_root_of( ( s1*s1 + s2*s2 + s3*s3 + ... sn*sn ) / n ) // this is the RMS forumula
keep in mind each audio sample has its own amplitude and perhaps you can simply plot these ( s1, s2, s3, ... ) or instead repeatedly do above RMS to get a set of aggregate amplitudes which is more helpful if a general ballpark amplitude is desired instead of the instantaneous amplitude of each sample