001/**
002 * Copyright (c) 2011, The University of Southampton and the individual contributors.
003 * All rights reserved.
004 *
005 * Redistribution and use in source and binary forms, with or without modification,
006 * are permitted provided that the following conditions are met:
007 *
008 *   *  Redistributions of source code must retain the above copyright notice,
009 *      this list of conditions and the following disclaimer.
010 *
011 *   *  Redistributions in binary form must reproduce the above copyright notice,
012 *      this list of conditions and the following disclaimer in the documentation
013 *      and/or other materials provided with the distribution.
014 *
015 *   *  Neither the name of the University of Southampton nor the names of its
016 *      contributors may be used to endorse or promote products derived from this
017 *      software without specific prior written permission.
018 *
019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029 */
030/**
031 *
032 */
033package org.openimaj.demos.sandbox.audio;
034
035import java.awt.BorderLayout;
036import java.awt.Dimension;
037
038import javax.swing.JFrame;
039
040import org.jfree.chart.ChartFactory;
041import org.jfree.chart.ChartPanel;
042import org.jfree.chart.JFreeChart;
043import org.jfree.chart.plot.PlotOrientation;
044import org.jfree.data.xy.DefaultXYDataset;
045import org.openimaj.audio.AudioPlayer;
046import org.openimaj.audio.SampleChunk;
047import org.openimaj.audio.generation.Synthesizer;
048import org.openimaj.audio.samples.SampleBuffer;
049
050/**
051 *      Instantiates the synthesiser and plays the sound from the synth.
052 *      Also displays the waveform of the first 3 sample chunks delivered
053 *      by the synth.
054 *
055 *      @author David Dupplaw (dpd@ecs.soton.ac.uk)
056 *      @created 2 May 2012
057 */
058public class SynthesizerTest
059{
060        /**
061         *
062         *  @param args
063         */
064        public static void main( final String[] args )
065        {
066                final Synthesizer synth = new Synthesizer();
067                synth.setFrequency( 220 );
068                SampleChunk s = synth.nextSampleChunk();
069                SampleBuffer b = s.getSampleBuffer();
070
071                int n = 3;
072
073                final DefaultXYDataset ds = new DefaultXYDataset();
074
075                int x = 0, y = 0;
076                while( n > 0 )
077                {
078                        // Convert sample to a XY data plot
079                        final double[][] data = new double[2][];
080                        data[0] = new double[b.size()]; // x
081                        data[1] = new double[b.size()]; // y
082
083                        for( x = 0; x < b.size(); x++ )
084                        {
085                                data[0][x] = b.get(x);
086                                data[1][x] = x+y;
087                        }
088
089                        s = synth.nextSampleChunk();
090                        b = s.getSampleBuffer();
091                        y += x;
092                        n--;
093
094                        ds.addSeries( "samples "+n, data );
095                }
096
097                final JFreeChart c = ChartFactory.createXYLineChart( "Sample", "samples",
098                        "amplitude", ds, PlotOrientation.HORIZONTAL, false, false,
099                        false );
100                final ChartPanel chartPanel = new ChartPanel( c, false );
101                chartPanel.setPreferredSize( new Dimension( 640, 480 ) );
102
103                final JFrame f = new JFrame();
104                f.add( chartPanel, BorderLayout.CENTER );
105                f.pack();
106                f.setVisible( true );
107
108                // Play the audio from the synth in a new thread
109                final AudioPlayer ap = AudioPlayer.createAudioPlayer( synth );
110                new Thread( ap ).start();
111
112                // Wait 2 seconds
113                try
114                {
115                        Thread.sleep( 2000 );
116                }
117                catch( final InterruptedException e )
118                {
119                }
120
121                // Set the synth to note off
122                synth.noteOff();
123        }
124}