Recent

Author Topic: Tone Generator  (Read 1448 times)

Gigatron

  • Sr. Member
  • ****
  • Posts: 431
  • Amiga Rulez !!
    • Gigatron Shader Network Demo
Tone Generator
« on: May 19, 2025, 10:41:46 pm »
Hi

Last days i've sarted a project called Sin table generator
https://forum.lazarus.freepascal.org/index.php/topic,71121.0.html
This new projet is
based on generated sin wave to produce a simple sound (be carefull to play with parameters, sure bugged at now ).
It use mmsystem and fill buffer like players i converted. I will try to improve it to
make an interesting program (missing formula).

Regards GTR


Coding faster than Light !  https://www.youtube.com/@gtrgtr9505

Thaddy

  • Hero Member
  • *****
  • Posts: 19607
  • Glad to be alive.
Re: Tone Generator
« Reply #1 on: May 20, 2025, 11:05:58 am »
Did it occur to you that for a pure sine in audio you just need half the wave and mirror that for the lower part? Or at most one complete sine cycle.
If it is sine, you can simply switch buffers two , not regenerate them.
That would greatly simplify the code and is less demanding on the CPU.
You should also generate the pre-compiled sine waves depending on the sample rate. Adjusting for note value can simply be done with the twelft root of two (~1.05 * note value)
This is how most synthesizers work.
Any "programmer" that knows only one programming language is not a programmer

Gigatron

  • Sr. Member
  • ****
  • Posts: 431
  • Amiga Rulez !!
    • Gigatron Shader Network Demo
Re: Tone Generator
« Reply #2 on: May 20, 2025, 12:28:45 pm »
Hi,

Thank you, I’ll study your recommendations and test the buffer mirroring approach. This could really optimize my code  ,  for now the latest version .

Regards Gtr
« Last Edit: May 20, 2025, 12:37:53 pm by Gigatron »
Coding faster than Light !  https://www.youtube.com/@gtrgtr9505

JollyHong

  • New Member
  • *
  • Posts: 11
Re: Tone Generator
« Reply #3 on: July 27, 2026, 10:31:25 am »
If you are writing or testing tone generation code in Lazarus, it’s really helpful to compare your output against a reference tone to verify frequencies, sample rates, and waveforms (sine, square, triangle).
When testing audio buffers or troubleshooting sound output, I usually drop a quick online generator like https://frequencylama.com/ into a browser tab to quickly double-check frequencies by ear alongside what the program is producing.
Are you generating PCM samples manually in code, or using an external library like BASS or PortAudio?

CM630

  • Hero Member
  • *****
  • Posts: 1766
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: Tone Generator
« Reply #4 on: July 27, 2026, 01:21:12 pm »
Did it occur to you that for a pure sine in audio you just need half the wave and mirror that for the lower part? Or at most one complete sine cycle.
Or maybe even quarter a wave ... though I wonder if the maximum of the sine will be fine.

You should also generate the pre-compiled sine waves depending on the sample rate. Adjusting for note value can simply be done with the twelft root of two (~1.05 * note value)
I have (always) thought it is 6 %, not 5 %, since my father told me so in the previous millennium. I think he did not lie.
2^(1/12) = 1,059463094


BTW, I also have played with these:
https://sourceforge.net/p/lazmer/code/HEAD/tree/trunk/misc/functiongenerator.pas (the package itself runs only in Lazarus Trunk).
Лазар 4,8 32 bit (sometimes 64 bit); FPC3,2,2

Thaddy

  • Hero Member
  • *****
  • Posts: 19607
  • Glad to be alive.
Re: Tone Generator
« Reply #5 on: July 27, 2026, 06:20:06 pm »
Yes. Correct. I usually use the 12th root of two as a constant single.
This is fine in practice from middle C, although I sometimes code per two octaves to avoid any noticeable drift.
« Last Edit: July 27, 2026, 06:24:36 pm by Thaddy »
Any "programmer" that knows only one programming language is not a programmer

Fred vS

  • Hero Member
  • *****
  • Posts: 3957
    • StrumPract is the musicians best friend
Re: Tone Generator
« Reply #6 on: July 27, 2026, 06:42:13 pm »
Hello.

There is also WavvieW, the signal producer and analyzer, included sine and white noise, with all pascal code.

It uses MSEgui widgetset.
« Last Edit: July 27, 2026, 06:43:55 pm by Fred vS »
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

Bart

  • Hero Member
  • *****
  • Posts: 5757
    • Bart en Mariska's Webstek
Re: Tone Generator
« Reply #7 on: July 27, 2026, 06:44:10 pm »
Long, long time ago I programmed a piano like program (using Graph unit) that generated tones (using 12th root of 2 rule) using good old Sound() in TP.
This reminds me of that undertaking...

Bart

 

TinyPortal © 2005-2018