sound with Dyalog APL

General APL language issues

sound with Dyalog APL

Postby Yves on Thu Mar 30, 2023 5:26 pm

Dear All,
How to stress the speaker of computer and generate the beep with a frequency defined ?
to start, i dont search necessarly tu produce sound of specific instrument.
my personnal goal is to play persian music, with quater of tone.
i suppose indian music is not very different.
For chinese and japanese, scale of tune are not similare of occidental music.
korean too.
but range of frequency are the same.
all yours help are welcome.
Yves
Yves
 
Posts: 39
Joined: Mon Nov 30, 2015 11:33 am

Re: sound with Dyalog APL

Postby StefanoLanzavecchia on Fri Mar 31, 2023 6:36 am

A starting point could be to check this message on this same forum: https://forums.dyalog.com/viewtopic.php?f=12&t=1044&start=10
The code, especially the one in the github repo mentioned further down the thread, looks ready to be used, though I have not tried it.
I have had success with similar techniques in the past but I seem to have misplaced my workspaces.
I have also experimented with NAudio (https://github.com/naudio/NAudio) but I had to write some glue code in C# which makes the whole business a bit tricky.

Another approach could be to communicate with an external synthesizer: I know that in a lot of projects people gravitate towards SuperCollider which, among the other ways, can be driven from external programs using a simple TCP based protocol called OSC. https://en.wikipedia.org/wiki/Open_Sound_Control
https://doc.sccode.org/Guides/OSC_communication.html

This is a far cry from the simplicity of []SOUND in APL*PLUS but will give you much more in the long term.

As far as computing the frequencies... Well, as you know, in equal temperament, the audible range is divided in octaves, each octave being a doubling in frequency. Conventionally, a certain note (A of the 4th octave, aka A4) is said to have frequency 440Hz. A5 will be 880Hz. How about the 12 semitones in between? Each one is obtained by multiplying (https://en.wikipedia.org/wiki/Equal_temperament) the previous by the 12th root of 2 which is approximately 1.059463. So A#4 will have frequency 466.16, B4 will vibrate at 493.88 and so on.

I hardly know anything about other tuning systems but I assume they will all have their mathematical rules which have been extensively studied, so that should be easy.

Best of luck and if you come up with something cool, I'd love to hear what you have done!
User avatar
StefanoLanzavecchia
 
Posts: 113
Joined: Fri Oct 03, 2008 9:37 am

Re: sound with Dyalog APL

Postby ray on Mon Apr 24, 2023 3:01 pm

Stefano in his response included a link to some work I did some years back. I have now updated the work from dealing with one ".WAV" file per TONE, to a single ".WAV" files with whole tunes including cords in stereo.

The new code has a function to create sine waves at any (audible) frequency, albeit from a function for western scales with 12 semi-tones per octave. Quarter tones should however be no problem.

My code is available on https://github.com/RayCannon/Wave

The main difficulty I have had was relating to unwanted "Snaps and Pops" that occurred when changing the notes/codes part way thru a wave-length. These discontinuities resulted in the unwanted noise. I have managed to reduce most of the discontinuities, but not completely.
TestTune can be run to produce small "keep" files that Dyalog's "Chart Wizard" can display showing how effective my efforts have been.

Here is the result of TestTune creating a file with equal notes of silence, 20.8Hz, silence, 66.666Hz, 28.27433Hz, silence and 50.7Hz.(My code automatically adds a trailing silence to prevent a final "pop at the music's end.)
keep.png
Data from TestTune


While here is a close-up of the change 66.666Hz and 28.27433Hz.
Attachments
keep4.png
Details from Keep.png
Ray Cannon
Please excuse any smelling pisstakes.
User avatar
ray
 
Posts: 238
Joined: Wed Feb 24, 2010 12:24 am
Location: Blackwater, Camberley. UK

Re: sound with Dyalog APL

Postby StefanoLanzavecchia on Mon Apr 24, 2023 3:41 pm

To remove the clicks, which are due to discontinuities in the sample waveform, I recommend fading all the notes: fade in (less important because you always start from 0), and fade out (so that the last sample of each note is a 0). You can simply apply a linear ramp which goes from 1 to 0 in the last fraction of millisecond. Example of (Python) code in exactly the same context: https://stackoverflow.com/a/42310911.
You can also apply a low pass filter to your signal (which is the same as integrating) because the filter adds "inertia", so to speak, to the original signal, removing the discontinuities. A simple digital low pass filter can be implemented as a moving average of the signal.
User avatar
StefanoLanzavecchia
 
Posts: 113
Joined: Fri Oct 03, 2008 9:37 am

Re: sound with Dyalog APL

Postby ray on Wed May 10, 2023 3:21 pm

Thanks for the information Stefano. I have solved the problem of clicks and pops by the used of overlapping voices.

My latest code is now available on GitHub at https://github.com/RayCannon/Toccata which now contains the the code to produce the full Toccata from Bach's Toccata and Fugue in D-minor.

Since WAV files are huge, (Toccata.wav is 36Mb) the GitHub has an MP3 version (produced from the WAV version) at only 2Mb.

Within the code, I have augmented the simple sine waves with multiple harmonics with varying loudness levels. These harmonics attempt to simulate various organ pipes characteristics, such as "Closed pipes" having mainly odd harmonic numbers unlike "Open pipes" having both odd and even. In some pipes, the fundamental is not the loudest harmonic, which greatly surprised me!
Code: Select all
 (harmonics volumes)←loud SelectPipe pipe;Hnums;ratios;powers
 ⍝ set up the harmonics for any "set of pipes"
 ⍝ See http://www.colinpykett.org.uk/observations-on-organ-pipe-sounds-frequency-spectra.htm#Refs
 :Select pipe
 :Case 'Q' ⍝ Silence
     harmonics←,1
     volumes←,loud
 :Case 'D' ⍝ Open Diapason    both odd and even harmonics
     ratios←1 4 8 8 8 8 10 32 64
     harmonics←1+⍳≢ratios
     powers←2×harmonics
     volumes←loud÷powers{⍵,(≢⍵)↓⍺}ratios
 :Case 'F' ⍝ closed Flute  mainly odd numbered harmonics
     ratios←1 8 2 16 8 32
     harmonics←1+⍳≢ratios
     powers←2×harmonics
     volumes←loud÷powers{⍵,(≢⍵)↓⍺}ratios
 :Case 'R' ⍝ Reed pipe
     ratios←1.5 1.6 1 2.2 2.5 3 6 12 24 48
     harmonics←1+⍳≢ratios
     powers←2×harmonics
     volumes←loud÷powers{⍵,(≢⍵)↓⍺}ratios
 :Case 'S' ⍝ open String Pipe
     ratios←(3⍴1.5),1,(⌽2+2÷1+⍳6),(8+4×⍳8)
     harmonics←1+⍳≢ratios
     powers←2×harmonics
     volumes←loud÷powers{⍵,(≢⍵)↓⍺}ratios
 :Else
     ratios←1+⍳11
     harmonics←1+⍳≢ratios
     powers←2×harmonics
     volumes←loud÷powers{⍵,(≢⍵)↓⍺}ratios
 :End

The values returned by "SelectPipe" are <harmonics> and <volumes>.
<harmonics> is multiplied with the fundamental frequency to produce the frequency of all the harmonics being produced. The resulting samples values are then multiplied by the <volumes> to adjust the relative loudness of each harmonic.

In addition to the harmonics, I have added reverberation, to attempt to simulate a large hall. (60db drop after 2 seconds.)
Code: Select all
Echo;a;i;⎕IO;delay;j;echo;delayecho
 ⍝ aim for 2 seconds for reverb to drop by 60db
 ⎕IO←1
 j←2
 :For i :In 2↓2*⍳8         ⍝ 6 steps each of 10 db loss
     echo←G.(left+right)÷i ⍝ an echo is in mono but reduced in amplituide
     j+←1                  ⍝ shorten the delay each time round the loop seems to help
     delay←-⌊G.rate÷j      ⍝ initial delay 1/3 of a second
     delayecho←delay⌽echo  ⍝ add a delay
     G.left+←delayecho     ⍝ add the echo to the initial sound
     G.right+←delayecho    ⍝ to both channels
 :End

Note that the original left and right channels end in extensive silence, so the delay is produced by rotation of some of this silence from the end to the start.

I have attempted to add the MP3 file to this reply, but the forum limits file sizes:
"The file is too big, maximum allowed size is 256 KiB."
Ray Cannon
Please excuse any smelling pisstakes.
User avatar
ray
 
Posts: 238
Joined: Wed Feb 24, 2010 12:24 am
Location: Blackwater, Camberley. UK

Re: sound with Dyalog APL

Postby ray on Wed May 10, 2023 3:41 pm

Out of interest, here are some ChartWizard plots of various "pipes"
PipeS.png
open String Pipe

PipeR.png
Reed pipe

PipeF.png
closed Flute
Ray Cannon
Please excuse any smelling pisstakes.
User avatar
ray
 
Posts: 238
Joined: Wed Feb 24, 2010 12:24 am
Location: Blackwater, Camberley. UK


Return to Language

Who is online

Users browsing this forum: Google [Bot] and 1 guest