Skip to main content
What Shazam doesn't want you to know Roy van Rijn Software Craftsman
Imagine this...
Friday afternoon
Getting inspiration
Singing along...
Meet Stewie!
Music matching
Shazam is magic... alien technology! Start the application.
Let it listen for +/- 20 seconds.
It tells you: Music matching
Saturday morning
Specifying the audio format: private  AudioFormat getFormat() {   float  sampleRate = 44100;   int  sampleSizeInBits = 8;   int  channels = 1;   boolean  signed =  true ;   boolean  bigEndian =  true ; return   new  AudioFormat( sampleRate, sampleSizeInBits, channels, signed,  bigEndian); } The microphone
Accessing the microphone final  AudioFormat format = getFormat();  DataLine.Info info =  new  DataLine.Info(TargetDataLine. class , format); final  TargetDataLine line =    (TargetDataLine) AudioSystem. getLine (info); line.open(format); line.start(); The microphone
Reading the sound: out  =  new  ByteArrayOutputStream(); running  =  true ; try  {   while  ( running ) {   int  count = line.read( buffer , 0,  buffer . length );   if  (count > 0) {   out .write( buffer , 0, count);   }   }   out .close(); }  catch  (IOException e) {   throw new RuntimeException(e); } The microphone
0 0 0 1 2 5 0 -1 -3 -4 -5 -2 0 1 2 0 2 (etc) Inside the byte array
Plotting a graph
Membrane, cochlear, brain The human ear
Hertz
The amount of cycles per second
i.e. one sine wave. Frequencies...?
To match music we need frequencies, not waves. Time vs Frequency
Fourier Transformation
Fourier Transformation
Fourier Transformation
Fourier Transformation
Fourier Transformation
Excellent explanation by Stuart Riffle http://altdevblogaday.com Fourier Transformation
We've lost track of time! Frequency domain
Solution: Apply transformation on pieces byte  audio[] =  out .toByteArray(); final int  amountSlices = audio. length / SLICE_SIZE ; Complex[][] results =  new  Complex[amountChucks][]; for ( int  slice = 0;slice < amountSlices; slice++) {   Complex[] complex =  new  Complex[ SLICE_SIZE ];   for ( int  i = 0;i< SLICE_SIZE ;i++) {   complex[i] =  new  Complex(audio[(slice* SLICE_SIZE )+i], 0);   }   results[slice] = FFT. fft (complex); } Windowing
From wikipedia: Spectum Analyzer A spectrum analyzer or spectral analyzer is a device used to examine the  spectral composition  of some electrical,  acoustic , or optical  waveform. Spectrum Analyzer
Demo:  Aphex Twin
Sunday
Determine the key points (in ranges):
34 41 92 129 186
39 41 117 130 218
40 42 106 129 191
40 47 117 121 217
40 53 81 129 208
40 48 109 132 260
39 45 89 135 247
40 42 84 125 251
40 41 81 121 232
38 42 113 131 245 (etc...) Matching the song
Playing/decoding MP3 files: JLayer (real time MP3 decoder) jl1.0.1.jar MP3SPI (Java plugin, based on JLayer) mp3spi1.9.4.jar Tritonus (implementation of Java Sound API) tritonus_share.jar Something to match against
Harvesting my music collection: public   void  harvest(File rootDirectory) {   String[] itemsInDirectory = rootDirectory.list();     for (String itemInDirectory:itemsInDirectory) {   if (itemInDirectory.endsWith( &quot;.mp3&quot; )) {   //Assume mp3 file   File mp3File =  new  File(mp3Directory, itemInDirectory);   captureAudio(mp3File);   }  else   if ( new  File(mp3Directory, itemInDirectory).isDirectory()) {   //Directory? Recurse!   harvest( new  File(mp3Directory, itemInDirectory));   }   } } Something to match against
We have: Set of +/- 3000 files of reference data (songs)