generate sound by formula

Hi all

I would like to generate sound by formula, for example, I would like to hear the sound of "sin(220x)+sin(300x)"

Do you have any suggestion?

1 Like

Use the TextToSpeech Component.

You should be able to use audio track to do this:

Example:

 @SimpleFunction(description = "Tone generator, freqHz is an integer number. Duration in milliseconds. wave is 1(saw),2(sqre) or 3(sine)")
    public void AnyWave(double freqHz, int durationMs, int wave) {

        int count = (int) (44100.0 * 2.0 * (durationMs / 1000.0)) & ~1;
        short[] samples = new short[count];
        for (int i = 0; i < count; i += 2) {
            // sawtooth
            if (wave == 1) {
                short sample = (short) ((2 * (i % (44100.0 / freqHz)) / (44100.0 / freqHz) - 1) * 0x7FFF);
                samples[i + 0] = sample;
                samples[i + 1] = sample;
            // square
            } else if (wave == 2) {
                short sample = (short) (Math.sin(2 * Math.PI * i / (44100.0 / freqHz)) * 0x7FFF);
                if (sample > 0.0) { sample = 32767; }
                if (sample < 0.0) { sample = 0; }
                samples[i + 0] = sample;
                samples[i + 1] = sample;
            //sine
            } else {
                short sample = (short) (Math.sin(2 * Math.PI * i / (44100.0 / freqHz)) * 0x7FFF);
                samples[i + 0] = sample;
                samples[i + 1] = sample;
            }
        }
        track1 = new AudioTrack(AudioManager.STREAM_MUSIC, 44100,
                AudioFormat.CHANNEL_OUT_STEREO, AudioFormat.ENCODING_PCM_16BIT,
                count * (Short.SIZE / 8), AudioTrack.MODE_STREAM);
        track1.write(samples, 0, count);
        track1.setNotificationMarkerPosition(44100 * (durationMs/1000));
        track1.setPlaybackPositionUpdateListener(new AudioTrack.OnPlaybackPositionUpdateListener() {
            @Override
            public void onMarkerReached(AudioTrack track) {
                track1.stop();
                track1.flush();
                track1.release();
                track1 = null;
            }
            @Override
            public void onPeriodicNotification(AudioTrack track) {
            }
        });
        track1.play();
    }

I have an unfinished extension that does this..

2 Likes

??? how will this convert a formula to a sound/tone ?

1 Like

It will say, "sin 220 x plus sin 300 x."

If you want to make it pronounce as "sine", use the replaceAll Block to replace all occurances of "sin" with "sine", and then let it speak the message.

Also, he said in the first post that he wants to 'hear' the sound of "sin(220x)+sin(300x)".

I believe you are missing the point here...

with the correct method,something like:

Math.sin(2 * Math.PI * i / (44100.0 / freqHz))
or
sin(220x)+sin(300x)

will generate a sine wave tone (sound)...

Could you try using the TextToSpeech once for me ?

(Sorry cuz I can't test it.)

NO, I dont want this, I think you make make mistake about question.
I want to generate sound that it have multi separate frequencies.

1 Like

You think when this extension will be ready?

I want to generate a sound that it is mixed frequency,

OR

Is there any way to play more frequency at the same time?
the KIO4_TONE extension , generate only ONE tone at the time. I would like to generate multi tone at the same time

Depends on how much time I get away from the real world :wink:

Drag out another instance of the extension, then start two tones at the same time in an event.

I have not tried that, yet...

2022-05-27_20-32-55
are you mean this? This play only TextBox3.text Tone

Drag the extension again to the mock phone on the designer, then you will have KIO4_Tone2. Use that for your second frequency. Does that work ?

let me to check it

when I drag extension again, it update the previous extension and I don't have KIO4_TONE2

Try this:

twotone.aia (6.7 KB)

image

thank you. that's work.
please tell me how can I add 7 numbers of KIO4_Tone to my project?
and another question? Is it possible to change the sounds amplitude separately?
for example : 2sin(220t) + 0.5sin(261t) + 0.1*sin(330t) + ...

I do not know. The amplitude appears to be built in the the calculation?

What I do know is that this maths generates the sound wave sample in the extension:

(Math.sin(2 * Math.PI * i / (44100.0 / freqHz)) * 0x7FFF)

If you can provide an equivalent, based upon your requirements, it should be possible to come up with something:

2sin(220t) + 0.5sin(261t) + 0.1*sin(330t) + ...

Amplitude is volume of the Tone. I would like to play each tone with difference volume.
for example 220Hz with 100% volume, 261Hz with 70%volume , ...