MaxVMH
Posts: 114
Joined: Mon Mar 19, 2018 1:26 pm

Which language for tone generator

Mon Sep 03, 2018 10:03 pm

Objective: I'd like to make a website that can generate (and playback) certain audio frequencies.

I already know some HTML / CSS / PHP but I don't think this will do the job. The files can be generated serverside (Raspberry Pi) or clientside (browser), as long as I don't have to make them beforehand in an audio editor (I could, but that would defeat the point of the tool I'm trying to create). Preferably sine waves, but triangle waves could also work.

I was thinking of JavaScript, but honestly that language scares the crap out of me because somehow I can't deal with the whole async thing (I understand the principle but it just doesn't fit my workflow), so I'd only consider that if that's the only option to generate the tones clientside (since that saves some bandwidth).

What programming language, preferably with an easy learning curve, can I use (and learn) for such a project?
Webserver stack: 2x Pi 3B | 1x Pi 3B+ ||| Pi Zero W for fooling around

Working on a soccer prediction web app:
https://github.com/MaxVMH/mijnscore
Attempt at writing up-to-date tutorials on making a Raspberry Pi webserver:
http://blog.mijnscore.be/

User avatar
topguy
Posts: 5942
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: Which language for tone generator

Mon Sep 03, 2018 10:34 pm

Do you want to create and audiofile that the client downloads or do you want to play the sound ?
And if you want to play it.. should it be played by the client/browser or by the Pi ?

Heater
Posts: 13885
Joined: Tue Jul 17, 2012 3:02 pm

Re: Which language for tone generator

Tue Sep 04, 2018 3:33 am

Pretty much any language can write sound files.

Last time I did anything like that it was generating .wav files using C/C++

No doubt one can find all kind of libraries to save generated waveforms in any sound file format. Perhaps choice of language might be influenced by the sound file format libraries available for it. The .wav file format is simple enough that you don't need any library.

For once I might agree that Javasrcipt is perhaps not the best language for generating sound files server side. That can be quite a long running and compute intensive task. C/C++ would be better.

You could spin up such a sound generator on the server from the PHP in your web server.
Memory in C++ is a leaky abstraction .

Heater
Posts: 13885
Joined: Tue Jul 17, 2012 3:02 pm

Re: Which language for tone generator

Tue Sep 04, 2018 3:41 am

MaxVMH,
I was thinking of JavaScript, but honestly that language scares the crap out of me because somehow I can't deal with the whole async thing (I understand the principle but it just doesn't fit my workflow),
You know, if you are using HTML and PHP then you already understand and are used to the async thing.

Your server runs PHP code in response to some event. In this case HTTP requests from the browser. That code runs, hopefully quickly, and produces a response that is returned to the browser and the PHP code then terminates. That PHP code may also have changed the state of the system, by updating a database for example. Your server can have many such PHP scripts running at the same time, if the same requests come from browsers at the same time.

All of this is PHP stuff is async, event driven programming. Just like Javascript. You are already very used to it. Async/event driven programming is your work flow already.
Memory in C++ is a leaky abstraction .

MaxVMH
Posts: 114
Joined: Mon Mar 19, 2018 1:26 pm

Re: Which language for tone generator

Tue Sep 04, 2018 6:42 am

topguy wrote:
Mon Sep 03, 2018 10:34 pm
Do you want to create and audiofile that the client downloads or do you want to play the sound ?
And if you want to play it.. should it be played by the client/browser or by the Pi ?
It should be played in the client / browser.
Webserver stack: 2x Pi 3B | 1x Pi 3B+ ||| Pi Zero W for fooling around

Working on a soccer prediction web app:
https://github.com/MaxVMH/mijnscore
Attempt at writing up-to-date tutorials on making a Raspberry Pi webserver:
http://blog.mijnscore.be/

MaxVMH
Posts: 114
Joined: Mon Mar 19, 2018 1:26 pm

Re: Which language for tone generator

Tue Sep 04, 2018 6:49 am

Heater wrote:
Tue Sep 04, 2018 3:33 am
No doubt one can find all kind of libraries to save generated waveforms in any sound file format.
I don’t necessarily want to save them, it’s just for playback. If I could generate them client-side / in the browser, I assume that would save me some bandwidth.
Heater wrote:
Tue Sep 04, 2018 3:33 am
Perhaps choice of language might be influenced by the sound file format libraries available for it. The .wav file format is simple enough that you don't need any library.
It can be any format, if .wav is the easiest, .wav it shall be :)
Heater wrote:
Tue Sep 04, 2018 3:33 am
For once I might agree that Javasrcipt is perhaps not the best language for generating sound files server side. That can be quite a long running and compute intensive task. C/C++ would be better.
Would C/C++ be able to generate those files in the browser / clientside?

Here is an example of something similar to what I want to do:
http://onlinetonegenerator.com/
Webserver stack: 2x Pi 3B | 1x Pi 3B+ ||| Pi Zero W for fooling around

Working on a soccer prediction web app:
https://github.com/MaxVMH/mijnscore
Attempt at writing up-to-date tutorials on making a Raspberry Pi webserver:
http://blog.mijnscore.be/

User avatar
topguy
Posts: 5942
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: Which language for tone generator

Tue Sep 04, 2018 8:21 am

Would C/C++ be able to generate those files in the browser / clientside?
Clientside, I would say your only option is JavaScript.

( Nobody make Java applets anymore and we don't talk about flash/actionscript :-) )

Heater
Posts: 13885
Joined: Tue Jul 17, 2012 3:02 pm

Re: Which language for tone generator

Tue Sep 04, 2018 8:55 am

If you type "javascript sound generator" into google you will find hundreds of links on how to do this in the browser. For example:
tone.js: https://tonejs.github.io/

Or get down and use the browser Web audio API directly: https://developer.mozilla.org/en-US/doc ... _Audio_API

You could write your waveform generating code in C/C++.Which might be convenient for testing on the PC. You can then transpile that C/C++ in Javascript using Emscripten https://github.com/kripken/emscripten. The resulting JS can then be used in the browser to generate waveforms for the audi API.

Now a days it is possible to get all kind of languages compiled into Javascript to run in the browser.
Memory in C++ is a leaky abstraction .

MaxVMH
Posts: 114
Joined: Mon Mar 19, 2018 1:26 pm

Re: Which language for tone generator

Tue Sep 04, 2018 12:35 pm

Thanks Heater, you are great. I didn’t think about using existing libraries/frameworks or transcompiling. Will check all of this when I get home.
Webserver stack: 2x Pi 3B | 1x Pi 3B+ ||| Pi Zero W for fooling around

Working on a soccer prediction web app:
https://github.com/MaxVMH/mijnscore
Attempt at writing up-to-date tutorials on making a Raspberry Pi webserver:
http://blog.mijnscore.be/

Heater
Posts: 13885
Joined: Tue Jul 17, 2012 3:02 pm

Re: Which language for tone generator

Tue Sep 04, 2018 9:10 pm

MaxVMH,
Thanks Heater, you are great.
I thank you for that.

I'm afraid I may have sent you down a rabbit hole of investigation and experiment that may take a lot of time before you reach a solution.

Anyway:

"Always bet on JS" - Brendan Eich.
Memory in C++ is a leaky abstraction .

MaxVMH
Posts: 114
Joined: Mon Mar 19, 2018 1:26 pm

Re: Which language for tone generator

Tue Sep 04, 2018 9:35 pm

Heater wrote:
Tue Sep 04, 2018 9:10 pm
I'm afraid I may have sent you down a rabbit hole of investigation and experiment that may take a lot of time before you reach a solution.
That started around the time I bought my first Raspberry Pi :)

Now it's time to put my nose in some JavaScript - I hope this time my attempt at learning it goes better than last time :lol:
(although to be fair the previous time I tried JavaScript it was server-side, Node.JS, so I hope I'll like client-side better)
Webserver stack: 2x Pi 3B | 1x Pi 3B+ ||| Pi Zero W for fooling around

Working on a soccer prediction web app:
https://github.com/MaxVMH/mijnscore
Attempt at writing up-to-date tutorials on making a Raspberry Pi webserver:
http://blog.mijnscore.be/

MaxVMH
Posts: 114
Joined: Mon Mar 19, 2018 1:26 pm

Re: Which language for tone generator

Wed Sep 05, 2018 7:42 pm

Heater wrote:
Tue Sep 04, 2018 8:55 am
Or get down and use the browser Web audio API directly: https://developer.mozilla.org/en-US/doc ... _Audio_API
That's the option I'm going for. I don't know why, but I like to keep things as 'vanilla' as possible.

Here is my first result. This plays a 400Hz sine wave 1 second after opening the page for 3 seconds. Don't mind the excessive commentation, that's because I'm trying to learn the API while I'm writing the code.

Code: Select all

    <script>
    // create web audio api context
    var audioCtx = new AudioContext();

    // create oscillator (tone) and gain (volume) node
    var tone = audioCtx.createOscillator();
    var volume = audioCtx.createGain();

    // set oscillator type (sine, square, sawtooth or triangle)
    tone.type = 'sine';
    // set oscillator frequency in Hz
    tone.frequency.value = 400;
    // set gain volume (above 1 will clip)
    volume.gain.value = 0.5;

    // set tone destination to go through volume
    tone.connect(volume);
    // set volume destination to output
    volume.connect(audioCtx.destination);

    // start oscillator x seconds after timestamp
    tone.start(1);
    // stop oscillator x seconds after timestamp
    tone.stop(4);

    </script>

I would like to end up with a tone generator that chooses a random frequency from a list (those most commonly found on graphic equalisers) to train hearing. I know some websites already exist for this purpose, but I haven't found a website that is also mobile-friendly. And I like to do things myself :)

I guess the hardest part from now on will be creating a nice user interface.
Webserver stack: 2x Pi 3B | 1x Pi 3B+ ||| Pi Zero W for fooling around

Working on a soccer prediction web app:
https://github.com/MaxVMH/mijnscore
Attempt at writing up-to-date tutorials on making a Raspberry Pi webserver:
http://blog.mijnscore.be/

User avatar
r3d4
Posts: 968
Joined: Sat Jul 30, 2011 8:21 am
Location: ./

Re: Which language for tone generator

Thu Sep 06, 2018 9:11 am

perhaps ? on the server
try faust
https://en.wikipedia.org/wiki/FAUST_(pr ... _language)
blurb wrote: FAUST (Functional AUdio STream) is a domain-specific purely functional programming language
for implementing signal processing algorithms
in the form of libraries,
audio plug-ins,
or standalone applications.

MaxVMH
Posts: 114
Joined: Mon Mar 19, 2018 1:26 pm

Re: Which language for tone generator

Sun Sep 09, 2018 8:36 pm

@Heater (and everyone else of course): I have had significant progress with the Web Audio API and made a working demo: www.frequencytrainer.com (hosted on a Raspberry Pi of course). Thanks again for the suggestion!


r3d4 wrote:
Thu Sep 06, 2018 9:11 am
perhaps ? on the server
try faust
https://en.wikipedia.org/wiki/FAUST_(pr ... _language)
I prefer client side because that means less stress for the server. But thanks for the suggestion, it looks like a really cool programming language and I might have some fun with it later on :)
Webserver stack: 2x Pi 3B | 1x Pi 3B+ ||| Pi Zero W for fooling around

Working on a soccer prediction web app:
https://github.com/MaxVMH/mijnscore
Attempt at writing up-to-date tutorials on making a Raspberry Pi webserver:
http://blog.mijnscore.be/

Heater
Posts: 13885
Joined: Tue Jul 17, 2012 3:02 pm

Re: Which language for tone generator

Mon Sep 10, 2018 10:32 am

Cool.

Seems I can't hear 12.5KHz anymore :(
Memory in C++ is a leaky abstraction .

MaxVMH
Posts: 114
Joined: Mon Mar 19, 2018 1:26 pm

Re: Which language for tone generator

Mon Sep 10, 2018 11:17 am

Heater wrote:
Mon Sep 10, 2018 10:32 am
Seems I can't hear 12.5KHz anymore :(
Are you sure your speakers can play 12.5K? I can comfortably hear it on my studio speakers, barely hear it on my iPhone/iPad and can't hear it at all on a pair of old dusty HiFi speakers.

Anyhow, speech range ends around 8K, so worst case scenario you don't hear those annoying high tones some old televisions make :lol:
Webserver stack: 2x Pi 3B | 1x Pi 3B+ ||| Pi Zero W for fooling around

Working on a soccer prediction web app:
https://github.com/MaxVMH/mijnscore
Attempt at writing up-to-date tutorials on making a Raspberry Pi webserver:
http://blog.mijnscore.be/

Heater
Posts: 13885
Joined: Tue Jul 17, 2012 3:02 pm

Re: Which language for tone generator

Mon Sep 10, 2018 2:54 pm

All I have here are some Sennheiser Bluetooth headphones. I have no idea what their bandwidth is.

I have to dig out my old HIFI speakers that have tweeters that will easily get to 12.5KHz.
Memory in C++ is a leaky abstraction .

Return to “General programming discussion”