Convert .mid to .wav using MATLAB - matlab

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

Related

Matlab noise source discontinuity

Using Matlab, I've made some random noise, filtered it and then successfully saved it as a gnuradio readable file for a file source. Once used in gnuradio, I set the file source to repeat and then viewed it using QT Gui Frequency Sink. I can see the filtered noise fine, but every now and then (every 10 seconds or so), the spectrum will drop in power and jump around for around a tenth of a second, then return back to normal power. My sample rate for the matlab filter is 320k and same with my gnuradio sample rate if that matters.
I think it may have to do with the fact that the noise generated on matlab is going to be a sequence that is repeated on gnuradio. I think the discontinuity happens right when the sequence repeats. Any idea how I can stop this discontinuity so I can transmit without having to worry about it? If I'm missing any info, please let me know and I'll edit the question. Thanks in advance.
NOTE: I needed to create a matlab binary file to be able to read it on GNU Radio. GNU Radio reads the binary file from my desktop, then uses the information as the file source.

Feature Extraction from an audio file using python

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

Listening to waveforms

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.

How can I get into a wav file to change the sample 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');

Creating a sample mp3 with fade

I need to know if it is possible to create a 30 second sample MP3 from a WAV file. The generated MP3 file must feature a fade at the start and end.
Currently using ffmpeg, but can not find any documentation that would support being able to do such a thing.
Could someone please provide me the name of software (CLI, *nix only) that could achieve this?
This will
trim out from Position 45 sec. the next 30 seconds (0:45.0 30) and
fade the first 5 seconds (0:5) and the last 5 seconds (0 0:5) and
convert from wav to mp3
sox infile.wav outfile.mp3 trim 0:45.0 30 fade h 0:5 0 0:5
Check out SoX - Sound eXchange
I have not used it myself but one of my friends speaks highly of it.
From web page (highlighted my me):
SoX is a cross-platform (Windows,
Linux, MacOS X, etc.) command line
utility that can convert various
formats of computer audio files in to
other formats. It can also apply
various effects to these sound files,
and, as an added bonus, SoX can play
and record audio files on most
platforms.
The best way to do this is to apply the 30-second truncation, fade in and fade out to the WAV audio data before converting it to an MP3. If your conversion library has a method that takes an array of samples, this is very easy to do. If the method only accepts a WAV file (either in-memory or on disk), then this is slightly less easy as you have to learn the WAV file format (which is easy to write but somewhat more difficult to read). Either way, applying gain and/or attenuation to time-domain sample data (as in a WAV file) is much easier than trying to apply these effects to frequency-domain data (as in an MP3 file).
Of course, if your conversion library already does all this, it's best to just use that and not worry about it yourself.