Watch, Follow, &
Connect with Us

Please visit our new home
community.embarcadero.com.


Welcome, Guest
Guest Settings
Help

Thread: How to play a sine tone without TMediaPlayer on an android device?


This question is answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 1 - Last Post: May 7, 2015 11:27 AM Last Post By: Peer Dahl
Peer Dahl

Posts: 17
Registered: 6/23/02
How to play a sine tone without TMediaPlayer on an android device?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Nov 3, 2014 10:46 AM
Hello everyone,

I have to play e.g. an endless sine wave on an android device. TMediaPlayer can only play files. Does anybody know how to do this with the C-Builder?

Thank you in advance!
Peer
Peer Dahl

Posts: 17
Registered: 6/23/02
Re: How to play a sine tone without TMediaPlayer on an android device?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: May 7, 2015 11:27 AM   in response to: Peer Dahl in response to: Peer Dahl
Hello again,

I've found a way to play a sinus tone on Android. Maybe there are better ones but this suffices my task at the moment.
Here are the steps. Perhaps they can be usefull for somebody .

Best regards
Peer

(1) In the header-file I added:
=====================
...
#include <Androidapi.JNI.media.hpp>

#define ZWEI_PI 2*M_PI
#define AUDIOPUFFERGROESSE 1024
#define MAX_VOLUME 10000
...


(2) In the forms class declaration I added:
=============================
...
public:

// Audio
_di_JAudioTrack track;
Androidapi::Jnibridge::TJavaArray__1<short> *pAudioData;
...


(3) In the function "void __fastcall TForm1::FormCreate(TObject *Sender)" I added:
===========================================================

...
// Audio initialisieren
pAudioData = new Androidapi::Jnibridge::TJavaArray__1<short> (AUDIOPUFFERGROESSE);
int minSize = TJAudioTrack::JavaClass->getMinBufferSize( 44100,
TJAudioFormat::JavaClass->CHANNEL_OUT_MONO,
TJAudioFormat::JavaClass->ENCODING_PCM_16BIT );

track = TJAudioTrack::JavaClass->init(
TJAudioManager::JavaClass->STREAM_MUSIC, 44100,
TJAudioFormat::JavaClass->CHANNEL_OUT_MONO,
TJAudioFormat::JavaClass->ENCODING_PCM_16BIT,
minSize,
TJAudioTrack::JavaClass->MODE_STREAM);

track->play();
...


(4) In the function "void __fastcall TForm1::FormDestroy(TObject *Sender)" I added:
============================================================

...
delete pAudioData;
...


(5) And finally I use a 10 ms timer for the sine tone calculation so this is a independend task that does not block other app activity:
============================

...

void __fastcall TForm1::AudioTimerTimer(TObject *Sender)
{
float fFrequency;
float fIncrement;
static float fAngle = 0;

fFrequency = 1000;
fIncrement = (float)(ZWEI_PI) * fFrequency / 44100;
for( int i = 0; i < AUDIOPUFFERGROESSE; i++ )
{
pAudioData->Items [i] = (short)((float)sin( fAngle ) * MAX_VOLUME);
fAngle += fIncrement;
if(fAngle > ZWEI_PI) fAngle -= ZWEI_PI;
}

track->write(pAudioData, 0, AUDIOPUFFERGROESSE);
}
...


(6) Play and stop of the tone is done via a Button
==================================

void __fastcall TForm1::Button3Click(TObject *Sender)
{
if(AudioTimer->Enabled == false)
AudioTimer->Enabled = true;
else
AudioTimer->Enabled = false;
return;
}

Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02