Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to generate pure tones with different decibels in java?

Tags:

java

audio

i need to generate pure tones with different levels of decibels (dB) in java. is there any example of how to do this?

like image 415
Jane Wayne Avatar asked Aug 22 '11 12:08

Jane Wayne


2 Answers

Tones with midi Synth

You can generate tones with the synth :

import javax.sound.midi.*;

public class MidiSynthesizerSample {
  public static void main(String[] args) {
      int[] notes = new int[]{60, 62, 64, 65, 67, 69, 71, 72, 72, 71, 69, 67, 65, 64, 62, 60};
      try {
          Synthesizer synthesizer = MidiSystem.getSynthesizer();
          synthesizer.open();
          MidiChannel channel = synthesizer.getChannels()[0];

          for (int note : notes) {
              channel.noteOn(note, 50);
              try {
                  Thread.sleep(200);
              } catch (InterruptedException e) {
                  break;
              } finally {
                  channel.noteOff(note);
              }
          }
      } catch (MidiUnavailableException e) {
          e.printStackTrace();
      }
  }
}

Where you can set up the volume of the channels, with

getSetChannelVolumeMessage(int, int)

http://www.springworldgames.com/rmleditor/javadoc/com/springworldgames/music/MidiUtils.html#getSetChannelVolumeMessage(int, int)

I don't know how to convert it to decibels, maybe it depends on your sound-card's volume level.

like image 143
kisp Avatar answered Sep 26 '22 08:09

kisp


This http://jsresources.org/examples/OscillatorPlayer.html shows how to create pure sinusoidal (or pretty much any other) waveforms and play them. As for decibels, their relation to amplitude is db ~ 20 * log10 (amplitude), i.e. amplitude ~ 10 ^ (dB/20), so just calculate from that.

like image 35
Joonas Pulakka Avatar answered Sep 22 '22 08:09

Joonas Pulakka