Generating beep

All your problems with Audio, Video and Images.

Generating beep

Postby matt46 » Thu Jan 22, 2009 8:05 pm

Hello guys,

I need to find out how to generate beep, with a specified frequency and duration (like the Beep function in C++: Beep(freq, duration)).
These beeps will have to be played in my application, under some circumstances.

Actually I found no solution except creating mp3 files and using them in SoundPool, which is not what I want.

There's a Tone generator class which exists but it cannot generate custom sounds.

If you have any idea, I will be happy to read it!

Regards! :)
matt46
Freshman
Freshman
 
Posts: 6
Joined: Sat Nov 29, 2008 8:11 pm

Top

Postby MrSnowflake » Thu Jan 22, 2009 8:38 pm

I don't think you can make beeps like in c as c uses the system speaker, which doesn't go over sound card.
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby epman » Fri Nov 27, 2009 3:53 am

Any solution to this issue ?
I also have the same question about it.
epman
Once Poster
Once Poster
 
Posts: 1
Joined: Fri Nov 27, 2009 3:48 am

Generating a beep

Postby OtagoAndroid » Wed Dec 16, 2009 2:29 am

You can use the sine function in the java math library to produce an array of short containing the beep, then play the beep using AudioTrack
OtagoAndroid
Once Poster
Once Poster
 
Posts: 1
Joined: Wed Dec 16, 2009 2:22 am

Postby blboyd » Wed Apr 14, 2010 9:52 pm

Does anyone have an example of this?

I'm really interested in this but can't find anything.

Brandon
blboyd
Freshman
Freshman
 
Posts: 2
Joined: Tue Apr 13, 2010 8:03 pm
Location: Conroe, TX

computed beep using audiotrack

Postby BobGardner » Sun May 09, 2010 7:28 am

package com.aiti.computedbeep;
//play buffer full of calculated samples using audiotrack

import android.app.Activity;
import android.graphics.Color;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioTrack;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.GestureDetector.OnGestureListener;
import android.widget.TextView;

public class computedbeep extends Activity implements OnGestureListener{
public AudioTrack at;
public TextView tv;
public GestureDetector gd;
public int minbufsizbytes;
public int bufsizbytes;
public int bufsizsamps;
public final int samprate = 8000;
public short[] buffer=null;
public final float f=2000.0f; //beep freq
public final float MAXVOL=(float)1.0;
public final float MINVOL=(float)0.01;
public float vol=MAXVOL;
public final int LOOPON= -1;
public final int LOOPOFF=0;
public int tmp;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.main);
gd=new GestureDetector(this);

tv = new TextView(this);
setContentView(tv);
tv.setTextColor(Color.WHITE);
// tv.setText("in oncreate\n"); //gets to here

minbufsizbytes = AudioTrack.getMinBufferSize(samprate,AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT); //870 at 8000, 4800 at 44100
bufsizbytes=minbufsizbytes; //
tv.setText("minbufsizebytes "+minbufsizbytes+"\n"); //gets to here
bufsizsamps=bufsizbytes/2;
buffer = new short[bufsizsamps];
fillbuf();
// dumpbuf();
// swapbuf();
try{
at = new AudioTrack(AudioManager.STREAM_MUSIC,samprate,
AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT,bufsizbytes,AudioTrack.MODE_STATIC);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
tv.append("create audiotracksnafu "+e+"\n"); //gets to here
e.printStackTrace();
}
at.setStereoVolume(1.0f,1.0f);
tmp=at.write(buffer, 0, bufsizsamps);
}//oncreate

void playit(){
tv.append("in playit!\n"); //
at.stop();
// at.setPlaybackHeadPosition(0);
at.reloadStaticData();
at.play();
// SystemClock.sleep(1000L);
// at.stop();
}

void fillbuf(){
double omega,t;
double dt=1.0/samprate; //sec per samp

t=0.0;
omega=(float)(2.0*Math.PI*f);
for(int i=0; i < bufsizsamps; i++){
buffer[i]=(short)(32000.0*Math.sin(omega*t));
t+=dt;
}
}

/*
void swapbuf(){
short i,in,out;
byte inh,inl;

for(i=0; i < bufsizsamps; i++){
in=buffer[i];
inh=(byte) (in >> 8);
inl=(byte) (in & 0xff);
out=(short) (inl*256+inh);
buffer[i]=out;
}
}

void dumpbuf(){
short i;

for(i=0; i < 256; i++){
tv.append(" "+ buffer[i]);
}
}
*/

//----------lifecycle callbacks----------------
@Override
public void onPause(){
super.onPause();
at.pause();
tv.append("onpause!\n"); //gets to here
}

@Override
public void onResume(){
super.onResume();
tv.append("onresume!\n"); //gets to here
// playit();
}

@Override
public void onStop(){
super.onStop();
at.stop();
tv.append("onstop!\n"); //gets to here
}

// @Override
// public void onDestroy(){
// super.onDestroy();
// }

//------------gesture callbacks--------------------
@Override
public boolean onTouchEvent(MotionEvent e){
if(e.getAction()==MotionEvent.ACTION_DOWN){
tv.append("in ontouch!\n"); //gets to here
playit();
}
return true;
}

@Override
public boolean onDown(MotionEvent arg0) {
// TODO Auto-generated method stub
return false;
}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
// TODO Auto-generated method stub
return false;
}

@Override
public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub
}

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
// TODO Auto-generated method stub
return false;
}

@Override
public void onShowPress(MotionEvent e) {
// TODO Auto-generated method stub
}

@Override
public boolean onSingleTapUp(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
}//activity
//-----------------------------eof-------------------------------
BobGardner
Junior Developer
Junior Developer
 
Posts: 11
Joined: Sun Apr 11, 2010 11:34 pm
Location: Orlando

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 18 guests