Hello
I'm new to programming and I'm trying to make a java application that will "hear" (not record necessarily) the sound and display how loud is.I'm thinking of converting the sound recordings to numbers,so I can see the difference on the sound levels.I got this code and I added the "getLevel()" method,which returns the amplitude of the current recording,but it's returning -1 everytime.I guess I'm not using it properly.Any ideas how I must call this method?I have to deliver my project in a week,so any help will be much appreciated! - public class Capture extends JFrame {
-
-
protected boolean running;
-
ByteArrayOutputStream out;
-
-
public Capture() {
-
super("Capture Sound Demo");
-
setDefaultCloseOperation(EXIT_ON_CLOSE);
-
Container content = getContentPane();
-
-
final JButton capture = new JButton("Capture");
-
final JButton stop = new JButton("Stop");
-
final JButton play = new JButton("Play");
-
-
capture.setEnabled(true);
-
stop.setEnabled(false);
-
play.setEnabled(false);
-
-
ActionListener captureListener =
-
new ActionListener() {
-
public void actionPerformed(ActionEvent e) {
-
capture.setEnabled(false);
-
stop.setEnabled(true);
-
play.setEnabled(false);
-
captureAudio();
-
}
-
};
-
capture.addActionListener(captureListener);
-
content.add(capture, BorderLayout.NORTH);
-
-
ActionListener stopListener =
-
new ActionListener() {
-
public void actionPerformed(ActionEvent e) {
-
capture.setEnabled(true);
-
stop.setEnabled(false);
-
play.setEnabled(true);
-
running = false;
-
}
-
};
-
stop.addActionListener(stopListener);
-
content.add(stop, BorderLayout.CENTER);
-
-
ActionListener playListener =
-
new ActionListener() {
-
public void actionPerformed(ActionEvent e) {
-
playAudio();
-
}
-
};
-
play.addActionListener(playListener);
-
content.add(play, BorderLayout.SOUTH);
-
}
-
-
private void captureAudio() {
-
try {
-
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();
-
-
Runnable runner = new Runnable() {
-
int bufferSize = (int)format.getSampleRate()
-
* format.getFrameSize();
-
byte buffer[] = new byte[bufferSize];
-
-
public void run() {
-
out = new ByteArrayOutputStream();
-
running = true;
-
try {
-
while (running) {
-
int count =
-
line.read(buffer, 0, buffer.length);
-
if (count > 0) {
-
out.write(buffer, 0, count);
-
-
System.out.println(line.getLevel()); // |-this is what i added-|
-
}
-
}
-
out.close();
-
} catch (IOException e) {
-
System.err.println("I/O problems: " + e);
-
System.exit(-1);
-
}
-
}
-
};
-
Thread captureThread = new Thread(runner);
-
captureThread.start();
-
} catch (LineUnavailableException e) {
-
System.err.println("Line unavailable: " + e);
-
System.exit(-2);
-
}
-
}
-
-
private void playAudio() {
-
try {
-
byte audio[] = out.toByteArray();
-
InputStream input =
-
new ByteArrayInputStream(audio);
-
final AudioFormat format = getFormat();
-
final AudioInputStream ais =
-
new AudioInputStream(input, format,
-
audio.length / format.getFrameSize());
-
DataLine.Info info = new DataLine.Info(
-
SourceDataLine.class, format);
-
final SourceDataLine line = (SourceDataLine)
-
AudioSystem.getLine(info);
-
line.open(format);
-
line.start();
-
-
Runnable runner = new Runnable() {
-
int bufferSize = (int) format.getSampleRate()
-
* format.getFrameSize();
-
byte buffer[] = new byte[bufferSize];
-
-
public void run() {
-
try {
-
int count;
-
while ((count = ais.read(
-
buffer, 0, buffer.length)) != -1) {
-
if (count > 0) {
-
line.write(buffer, 0, count);
-
}
-
}
-
-
line.drain();
-
line.close();
-
-
} catch (IOException e) {
-
System.err.println("I/O problems: " + e);
-
System.exit(-3);
-
}
-
}
-
};
-
Thread playThread = new Thread(runner);
-
playThread.start();
-
} catch (LineUnavailableException e) {
-
System.err.println("Line unavailable: " + e);
-
System.exit(-4);
-
}
-
}
-
-
private AudioFormat getFormat() {
-
float sampleRate = 8000;
-
int sampleSizeInBits = 8;
-
int channels = 1;
-
boolean signed = true;
-
boolean bigEndian = true;
-
return new AudioFormat(sampleRate,
-
sampleSizeInBits, channels, signed, bigEndian);
-
}
-
-
@SuppressWarnings("deprecation")
-
public static void main(String args[]) {
-
JFrame frame = new Capture();
-
frame.pack();
-
frame.show();
-
}
-
}
5 1963
Ok,I managed to make it capture audio and print on a xls file the timestamp and the value of the current sample,but there is a problem : even I've put some spaces between the time and the value and it seems that they are in different columns,they are actualy on the same column of the xls,it's just expanded and covers the next column (I can put a print screen if you don't understand).How can I make it print the data of time and amplitude in two different columns?Here's my code of the class which creates the file and saves the data on xls : - package soundRecording;
-
-
import java.io.File;
-
import java.util.Formatter;
-
-
-
public class Save {
-
-
static Formatter y;
-
-
public static void createFile() {
-
-
Date thedate = new Date();
-
final String folder = thedate.curDate();
-
final String fileName = thedate.curTime();
-
-
try {
-
String name = "Time_"+fileName+".csv";
-
y = new Formatter(name);
-
File nof = new File(name);
-
nof.createNewFile();
-
System.out.println("A new file was created.");
-
}
-
catch(Exception e) {
-
System.out.println("There was an error.");
-
}
-
}
-
-
public void addValues(byte audio) {
-
Date d = new Date();
-
y.format("%s " + " %s%n",d.curTime(), audio);
-
}
-
-
public void closeFile() {
-
y.close();
-
}
-
}
If you use CSV, you need to separate the columns by comma.
Spaces are not working. if you have data that contains comma, then you also need to use quotation marks surrounding the columns. An example with 2 columns: - "This is, and it really is, column 1", "Spaces and tabs do not disturb me"
By the way, why do you use "thedate" and not "theDate", "aDate", "myDate" or "ourDate"? You just started programming, so don't get used to bad habits. Simply name it "date".
And what is "nof"? Is it that what the fox says? No? Then why didn't you name it "batman" instead? Just kidding!
But please, no kidding, use meaningful names!
Thanks for your help and your example,I understood how to use it.
About the variable names,you are right about "thedate",I should name it "theDate".I didn't want to use just "date",because "Date" is the name of my class (I know that "date" and "Date" are different),but I wanted to make clear that this reffers to a specific day,so I named it "thedate".
"nof" stands for "name of file".Although "batman" would be cool,I'll propably use it in my next java project!
If you name a variable "date", because it is an instance of class Date, then that is a good thing. Every programmer then can easily remember what it is. Prefixes like "the" "a" "our" or "my" do not add anything to understand the meaning of the variable. They are just typing clutter and makes it longer and harder (explanation see below) to read. If you want to make clear that this variable refers to a specific day, then just name that day and what is so specific on it. For example you could name the variable "today", "yesterday", "christmasEve". In a more general form, you could name it "oldDate" if there also exists a "newDate". That means, if you use many dates, don't call them "date1", "date2" etc., but put the difference in a meaningful name, like "birthdayOfCat", "birthdayOfDog".
Explanation: Usually you know the name of a variable without reading all letters, you just read the first 2 letters (and maybe the last 2 letters and your brain already delivers the meaning) But it cannot do that if all your local variables start with "the" and ends with "String". Just test yourself: can you figure out the odd one out faster in the first or second line?
First line:
theframestring theframestring theframestring theframestring theframestring theframestring theframestring thegreenstring theframestring theframestring theframestring theframestring
Second line:
frame frame frame frame frame green frame frame frame frame frame frame frame frame
P.s: If your variable is only used temporary (for example a loop counter), that means within the surrounding 5 lines of code, then just give it a single letter.
Thanks,I'll try to do that on my future codes!
Sign in to post your reply or Sign up for a free account.
Similar topics
by: wee |
last post by:
I've been using Java for a few years now and am just amazed at how many
new "technologies" or tools come out all the time (i.e., Struts, Java
Server Faces, JMeter, jad, etc.). It is nigh impossible...
|
by: jim shirreffs |
last post by:
Hello, I am trying to put together a Java system for playing audio CDs on a
large CD player, I have most of it working But entering all the data like
Artist, Title, track name, track length...
|
by: VB.NET |
last post by:
I've seen tons of image galleries in java, but has anyone seen a fancy way
of showcasing audio files?
|
by: Jobs |
last post by:
Download the JAVA , .NET and SQL Server interview with answers
Download the JAVA , .NET and SQL Server interview sheet and rate
yourself. This will help you judge yourself are you really worth of...
|
by: mmc |
last post by:
3 methods needs doing;
wave1: This static method takes an AudioInputStream and gradually lowers and then raises the volume so that the new AudioInputStream appears to be constantly alerting in...
|
by: cyberbrain7 |
last post by:
Please help me how to add audio application (not applet) using java. Thanks!!!
|
by: pankhudi |
last post by:
Hi everyone...
I have to do a project work related on online audio system in Java...I have done the server-client prog(socket prog) n playing audio files..But how to stream my audio file from one PC...
|
by: hzgt9b |
last post by:
Using VS2005, VB.NET,
I want to record audio from my sound card, or mic input... and while
recording get feedback to create a sound meter for visual display. Due
to budget constraints, I can't...
|
by: s0suk3 |
last post by:
I've been programming Python for a couple of years now. Now I'm
looking to move on to either C++ or Java, but I'm not sure which.
Which one do you think will be a better transition for a Python...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |