001/* A simple application that demonstrates the use of SoundPlayback 002 by performing simple additive synthesis in real-time. 003 004 Copyright (c) 2000-2014 The Regents of the University of California. 005 All rights reserved. 006 Permission is hereby granted, without written agreement and without 007 license or royalty fees, to use, copy, modify, and distribute this 008 software and its documentation for any purpose, provided that the above 009 copyright notice and the following two paragraphs appear in all copies 010 of this software. 011 012 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY 013 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 014 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF 015 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF 016 SUCH DAMAGE. 017 018 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, 019 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 020 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE 021 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF 022 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, 023 ENHANCEMENTS, OR MODIFICATIONS. 024 025 PT_COPYRIGHT_VERSION_2 026 COPYRIGHTENDKEY 027 028 */ 029package ptolemy.media.javasound.demo.Sines; 030 031import ptolemy.media.javasound.SoundPlayback; 032 033//////////////////////////////////////////////////// 034 035/** A simple application that demonstrates the use of SoundPlayback. 036 This application synthesizes and plays a simple harmonic 037 signal, using simple additive synthesis. The signal 038 is the some of a few harmonically related sinusoids. 039 @author Brian K. Vogel 040 @version $Id$ 041 @since Ptolemy II 1.0 042 @Pt.ProposedRating Red (vogel) 043 @Pt.AcceptedRating Red (vogel) 044 */ 045public class Sines { 046 /** Synthesize and play a simple harmonic signal using additive synthesis. 047 * @param args Not used. 048 */ 049 public static void main(String[] args) { 050 // The pitch of the signal to synthesize. 051 double fundamental_Hz = 220; 052 053 float sampleRate = 44100; // in Hz 054 int sampleSizeInBits = 16; 055 int channels = 2; // stereo. 056 int outBufferSize = 4096; // Internal buffer size for playback. 057 058 // Amount of data to read or write from/to the internal buffer 059 // at a time. This should be set smaller than the internal buffer 060 // size! 061 int putSamplesSize = 25; 062 063 // Construct a sound playback object that plays audio 064 //through the computer's speaker. 065 SoundPlayback soundPlayback = new SoundPlayback(sampleRate, 066 sampleSizeInBits, channels, outBufferSize, putSamplesSize); 067 068 // Initialize and begin playback. 069 try { 070 soundPlayback.startPlayback(); 071 } catch (Exception ex) { 072 System.err.println(ex); 073 } 074 075 double[][] samplesArray = new double[channels][putSamplesSize]; 076 077 // keep track of time, used in calculating the sine wave values. 078 double[] samples = new double[channels]; 079 080 try { 081 // Loop forever. 082 while (true) { 083 for (int j = 0; j < channels; j++) { 084 for (int i = 0; i < putSamplesSize; i++) { 085 // Generate a harmonic signal. 086 samplesArray[j][i] = java.lang.Math.sin(fundamental_Hz 087 * 2 * java.lang.Math.PI * samples[j]) * 0.4 088 + java.lang.Math.sin(2 * fundamental_Hz * 2 089 * java.lang.Math.PI * samples[j]) * 0.3 090 + java.lang.Math.sin(3 * fundamental_Hz * 2 091 * java.lang.Math.PI * samples[j]) * 0.25 092 + java.lang.Math.sin(4 * fundamental_Hz * 2 093 * java.lang.Math.PI * samples[j]) * 0.2; 094 095 // Increment time. 096 samples[j] = samples[j] + 1.0 / sampleRate; 097 } 098 } 099 100 // Play the processed audio samples. 101 soundPlayback.putSamples(samplesArray); 102 } 103 } catch (Exception ex) { 104 System.err.println(ex); 105 } 106 } 107}