How to generate a frequency modulated square wave?

I want to simulate the sound of a modulated square wave. I have parameters that I enter and want to simulate what it sounds like. I tried to generate using a wav file of a 1ms 1200hz tone and 1ms of silence. I also used a 1ms delay to stop the sounds from running into one another. The problem is that the sound generated is about 10 times slower than the actual wav in real time. The only way that I can make an accurate representation is for the app to generate a wav file and then play it. Is there a way to do this in app inventor?

This extension by Juan Antonio will produce a modulated tone Bill https://community.thunkable.com/t/extension-tonegenerator-beep-frequency-duration/7978 and it might do what you want. I don't know whether this extension generates a sinusoidal or square wave.

Regards,
Steve

Thanks Steve, but it seems to have a problem with 1ms durations. I want to write a WAV file but the file contains binary 0 data and I can not write sequential nulls (00 hex) to the files as it is only text files. I can store most special characters but not consecutive nulls. Any ideas. I know the data I need to store for a wav file. I can store the binary data in a list but don't know how to send that to the sound component without it being in a file.

Perhaps there is something here and you can implement what you want using javascript and the WebViewer https://search.yahoo.com/search?fr=mcafee&type=E210US105G0&p=javascript+generate+square+wave

1 ms is an issue? It is doubtful a 1 ms duration can be measured with App Inventor or achieved using the Sound or Player components. To solve your problem, not knowing how, why you are doing this, you might provide a series of mp3 files recorded using one of audio manipulation programs used on PCs, import the files to Media and play that back using Sound or Player. A bit of caution, those controls probably cannot provide 1ms 'precision' .. somewhere in the myriad discussions of Player are some discussions about the ability of the control to provide 'instantaneous' playing of sound ... here is the haystack Search results for 'audio player' - MIT App Inventor Community and those from the old forum Redirecting to Google Groups

or get an lab quality square wave generator and forget using an Android device.

Thanks Steve. The problem is the 1ms duration. I used Cool Edit to make a 1ms 1200hz square wave but when it is used to generate waveform the sound component is too slow and the simulated waveform is about 10 x slower. I therefore wanted to generate a wav file but app inventor has no facility to write binary files , only text files.

I have no experience with java script or making extensions.

Here is some general advice

-- Links ..to use javascript with App Inventor, you use the capabilities of the WebViewer ... these may help

Two New Tutorials on Using Javascript in App Inventor

WebView Javascript Processor for App Inventor

App Inventor and embedded HTML/JavaScript - Pura Vida Apps

-- Forum discussions https://community.appinventor.mit.edu/search?q=javascript

Will this be a panacea? It depends on what you need to accomplish. You might try the javascript 'solutions' links to working with square wave in the fourth post in this thread Bill. Will they work with AI2? You certainly can try.

Hi Steve, I managed to get an array with all the wav data and header information in it using Javascript but can not work out how to get it to play or how to write it to a binary file with a .wav extention. I am not up to speed with BLOB or how that works. So I have basically a WAV file image in an Uint8Array. Where do I go from here.? I tried some of the examples I found and the player pops up but nothing plays. Any help is appreciated.

I don't know. If you post your code (and perhaps the aia) someone else might have an idea. Great you were able to get a wave file image so you are learning javascript.

'I found and the player pops up but nothing plays.'. If you use the player to play a very small file, perhaps 1 ms duration or very small, the player might not be able to play the file for reasons discussed earlier.

Share your Project, explain more clearly what you are doing and why and someone might be able to suggest "Where do I go from here.?"

Hi Steve, basically I generate a square wave waveform which I display graphically on a canvas. Now I want to simulate what that sounds like. At first i tried to simulate it with 1ms sounds of tone and silence but that did not work out as it was too slow. The only way was to generate a wav file or sound file/data that represents the waveform. I worked out the header info for a .wav file and the data etc and generated that in javascript that could be run via webview. I managed to get an array with the correct byte data in it as per a wav file but not sure how to get that into a binary file that I can call waveform.wav or send it to the player on the "web page" or to the AI sound block. Looking into Blob constructor as I don't understand it quite yet.

I have managed to get it to work with javascript on a windows PC but no sound when I run AI on an Andriod tablet. It runs the code but no sound. I am so close but still have not cracked it.
I have the wav file data in an array intBuffer.
I then create an ArrayBuffer buffer and use View to get the data into buffer. DL has the bytes in the wav form data and there is 44 bytes of wav header info. Then a Blob to create a URL. Unfortunately I am stuck with no sound when running IA on Android.

var buffer = new ArrayBuffer(DL + 44);
var View = new Uint8Array(buffer);

        for (x = 0; x < DL + 44; x++) {
            View[x] = intBuffer[x];
        }

var b = new Blob([buffer], {
type: 'audio/wav'
});
var URLObject = window.webkitURL || window.URL;
var url = URLObject.createObjectURL(b);
var audio = new Audio(url);
audio.play();
audio.loop=true;

         setTimeout(function(){ audio.loop=false; }, 3000);

Hi,

are you shure that it is possible at all? A 1200 Hz frequency has a period of 0.83 ms. Assume your signal starts with high value and has a puse width of 50%. You will have a high signal of 0.416 ms first then a low value for 0.416 ms and at last a high value vor 0.17 ms. Then your limit of 1 ms is reached. Is this realy what you want?

Maybe you mean seconds instead of ms or kHz instead of Hz.

Ulli

Hi Ulli,
that is why I want to do it via a wav file so I can get the 1200hz. The 1ms was to get a rough approximation but as it turned out, it took long to play the sounds. So I generated a wav file image of the squarewave I wanted to simulate.

You can get a wav file online
https://www.wavtones.com/functiongenerator.php

and download to your device.

With this library you can convert a sound to a wav file

https://addpipe.com/simple-web-audio-recorder-demo/

Thank you for your suggestion. However, I can already generate the wavfile data. The problem is getting it to play on the Andriod tablet. It is running the javascript but no sound. Works on a PC. Not sure if Webviewer in AI suports the Audio. Does anyone know the answer to this?

I created a Blob in webviewer with the data but dont know how to get that blob to play a sound back in blocks. (Sound component)

I have managed to get it to work with javascript on a windows PC but no sound when I ran AI on an Andriod tablet.
I have the wav file data in an array intBuffer.
I then create an ArrayBuffer buffer and use View to get the data into buffer. DL has the bytes in the wav form data and there is 44 bytes of wav header info. Then a Blob to create a URL.
Now I have found a solution that works. I also have a button on the page for done. I do have one question, how do I unload the page via IA blocks?

        var b = new Blob([buffer], {
            type: 'audio/wav'
        });

const url = URL.createObjectURL(b);

var x = document.createElement("AUDIO");

 x.setAttribute("src",url);

x.setAttribute("controls", "controls");
x.setAttribute("loop", "loop");
document.body.appendChild(x);

function alldone() {
window.AppInventor.setWebViewString("Done");
URL.revokeObjectURL(url);
}

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.