In Matlab I want to hear the differences between what two waveforms sound like. What is the function used to listen to audio in Matlab? For example I have two waveforms from a file
wav1 = wavread('audio1.wav');
wav2 = wavread('audio2.wav');
how am I able to play these waveforms over my speakers?
The Matlab command to play a waveform is the sound command. it is used like so:
sound(wav1,F1);
sound(wav2,F2);
where F1 and F2 are the frequency used in playback. you can obtain the frequency from an audio file using your same wavread command thusly:
[wav1,F1,Nbits1] = wavread('audio1.wav');
where Nbits1 is the number of data points in the audio file.
Check out sound and soundsc functions.
You should try sound(wav1, 22050).
Of course, if you need higher sample rate, you can always change it.
You could use just sound(wav1) - however, you should always specify sample rate
in order to make sure you hear the waveform properly.
Related
Im currently struggling to understand what is happening. So, I created a sound using the audiowrite function in Matlab (the sound is created using two different sounds but I dont think it matters) first with a sampling frequency of 44100 Hz, and another one, the sound file is the same but the sampling frequency is 48000 Hz. Now I'm observing that the sound produced at 44100Hz is approx. 30sec longer than the other one (48000Hz sampling). It looks like phase shifting of some sort, but I'm not sure. Any help/explanation is appreciated. I also made a amplitude/time plot for better understanding:
(I set the x axis to 350sec to see where the signal ends).
EDIT: here is the code for how I create the sound file:
[y1,F1] = audioread(cave_file); %cave and forest files are mp3 files loaded earlier both have samp.freq of 48000Hz
[y2,F2] = audioread(forest_file);
samp_freq=44100;
%samp_freq=48000;
a = max(size(y1),size(y2));
z = [[y1;zeros(abs([a(1),0]-size(y1)))],[y2;zeros(abs([a(1),0]- size(y2)))]]
audiowrite('test_sound.wav', z,samp_freq);
What is the storage format? More specifically, is the info about sampling rate and number of channels stored in file meta data? which is then used during playback.
If so, then there are 3 possibilities for this behavior:
1) The sampling rate meta data of the 44.1KHz file is incorrect, while the audio was sampled at the correct rate i.e. 44.1KHz. Because the 44.1KHz file is playing longer than 48KHz, which I'm assuming to be producing the correct sound, and playing for the correct duration, it can be concluded that the sampling rate meta data of 44.1KHz is much lesser than 44.1KHz.
Could you please check the meta data? or attach the files here so that I can try to take a look?
2) The sampling didn't happen at the correct rate, while the meta data has 44.1KHz as the sampling rate.
3) The number of channels is incorrectly stored.
In case the files are raw PCMs, then this probably the correct sampling rate and/or number of channels is not selected when playing the 44.1KHz file.
Hope this helps
What I have
An mp3 file, 16kHz, 1 channel. Read like:
[data,Fs] = audioread('file.mp3');
This file is playable in Windows Media Player i.e., and works fine.
What I want
To play it inside matlab. After reading it, I've tried to play it, like:
soundsc(data);
However, it doesn't sound even near to how it should (neither using sound instead of soundsc).
The Problem then is..
How can I play this mp3 vector inside matlab? Is it even possible? Or do I need to convert it to other format so I can work with it? (wav I guess?)
You are missing the sample frequency. You need
soundsc(data, Fs)
If not present, the Fs argument defaults to 8192 Hz, which is not the correct one.
Also, note that if you don't need scaling you can use
sound(data, Fs)
which will run a little faster.
I've been using this library (http://kenschutte.com/midi) to work with midi files and the functions on here have been very helpful. However, the midi2audio() method only produces garbled .wav files no matter what midi I put in (although the notes are recognizable and the correct midi is being played). Has anyone else used this function library and run into this same problem and if so, how could I fix this? Or is there another function I can use online somewhere that does the same thing?
Below is the code used to generate the .wav file (copied and pasted from the link above)
[y,Fs] = midi2audio(midi);
% save to file:
% (normalize so as not clipped in writing to wav)
y = .95.*y./max(abs(y));
wavwrite(y, Fs, 'out.wav');
It appears that midi2audio only include very rudimentary sound synthesis, with frequency modulated synthesis as default. If you change to simple sine wave synthesis maybe it will sound better?
[y,Fs] = midi2audio(midi, 'sine')
If that still doesn't cut it you'd probably want to use more sophisticated software instruments.
The simplest cross-platform method for this is probably FluidSynth (also available through various repositories like MacPorts, Homebrew, apt-get, GitHub…)
FluidSynth uses sample based sound synthesis to translate the MIDI instructions into audio, and a sample bank in the SoundFont2 format is required for it to work. One such can be found here.
Having sorted that out, all you have to do to make a WAVE file out of your MIDI file is to type this into your terminal/console:
fluidsynth -F out.wav path-to-fm2-file in.mid
I am writing a chord-recognizer for a school project. I have to extract features from an mp3 file and use SVM with chord labels.
How can I extract frequencies from an audio file.
Is there any scipy package which could get me beat synchronous chroma.
The decoding becomes much easier even using a home-grown tool if to get some raw stream (including WAV file which is simply a raw stream and an envelope around it). Under a usual Unix-like, you can do it e.g. with mpg123 -s, mplayer -ao pcm:fast:file=$outfile and so on. But I doubt you can find a library which eventually supports all compressed audio formats.
(Also, SoX is good to convert between all uncompressed formats.)
You can read wave files with python's wave package. Probably the easiest way to get out frequencies is by taking the FFT (numpy.fft) and finding peaks in the output. You'll want to time-box your FFT calls to something that makes sense (windows where the pitches are consistent), or else you'll be looking at a bunch of frequency patterns on top of each other.
Have fun!
You may consider computing a chromagram, which is just like a spectrogram but with the musical notes in the Y axis instead of frequencies. The Librosa python library has a built-in function to compute it. https://librosa.github.io/librosa/generated/librosa.feature.chroma_stft.html
What you're looking for my friend, is Librosa. It's perfect for Audio feature extraction and manipulation. It has a separate submodule for features. You can extract features at the lowest levels and their documentation has some very easy to understand tutorials.
Here's the link to their website. Along with a sample code
https://librosa.github.io/librosa/index.html
import librosa
audio_file = 'your_audio_file.wav'
signal , sampling_rate = librosa.load(audio_file, sr=16000)
print(type(signal), type(sampling_rate)
len(signal), sampling_rate
I have a wav file pulled up in MATLAB, and I can see it's sample rate. All I need to do is change this 1 number. Everything else in the file will remain uncahnged. (The resulting sound would play at a different speed but would have an identical array of sample data.)
The reason I need to do this is because MATLAB seems to freak out when I tell it to open something sampled at anything other than 8k. All I need MATLAB for is to edit the file, so the sample rate really doesn't matter at all, since I'll be putting it back into a wav file when I'm done. So I either need to be able to change the value in the wav file that stores the sample rate, or to get MATLAB to change the sample rate it prefers from 8k to the sample rate that my files were recorded at.
if you just want to change the sampling frequency, here is the code, but it would distort the original wav file. If you decrease the sampling frequency, then the beat and music would be very slow.
Code:
[y, fs, nbits]=wavread('stego_lab');
fs2=11025;
wavwrite(y,fs2,nbits,'stego2_lab.wav');
sound(y,fs2,nbits)
you can hear it but the samples will remain the same.
Hope it helps.
There is the SOX tool, which should help you in that respect, and it comes on almost any platform - http://sox.sourceforge.net
There is also libsndrate, libsamplerate, libsndfile and others, that might have executables too.
Try this solution
[x,fs] = wavread('infile.wav');
<br>[p,q] = rat(16000/fs) % to convert to 16k sample rate</br>
<br>y = resample(x,p,q); % signal package require
wavwrite(x,16000,'outfile.wav');