## sound in APL

Learning APL or new to Dyalog? Ask "silly" questions here, without fear...

### Re: sound in APL

Hi Gil,

I got your namespace into my workspace, and the mono example produces a 3-second tone at 440 Hz. Great! I still have one more question: how do I set up two different vectors of samples to play in stereo? I'm looking at your example where you "modulate previous wave across 2 channels" but I don't immediately see how to modify that for two different vectors.

-Stu
Stu

Posts: 97
Joined: Thu Dec 31, 2015 1:30 am

### Re: sound in APL

The Wave function takes a matrix of one or two columns for the channels and the rows are the samples.
The optional second argument is the sample rate and the third is the bit resolution of each sample.

You can pass it samples in the range [¯1,1] in which case it will map them to the given sample resolution.
gil

Posts: 68
Joined: Mon Feb 15, 2010 12:42 am

### Re: sound in APL

Got stereo working. Thanks!

It wasn't immediately obvious to me that the sample rate was an optional argument to Wave. I saw that it appeared to be hardwired (8192) there, which I thought was a bad idea. But you've taken care of that, so now I'm completely happy with what I've got. Now I can translate my Matlab functions into Dyalog (filters, reverbs, phasing and flanging, tremolo and vibrato, etc.).
Stu

Posts: 97
Joined: Thu Dec 31, 2015 1:30 am

### Re: sound in APL

Here's a phone dialer I made using your Sound namespace. I can actually dial numbers with it if I hold the handset up to one of my computer's loudspeakers.

DTMF keys;⎕IO;i;key;tone

⍝ A Touch Tone phone dialer

⍝ Inputs:
⍝ keys = numeric vector containing the desired phone number
⍝ sr = sample rate (global variable)

⎕IO←0
tone←⍬
:For i :In ⍳⍴keys
key←keys[i]
:Select key
:Case 1
tone←tone,(0.5×(697 sine 0.3)+1209 sine 0.3),delay 0.02
:Case 2
tone←tone,(0.5×(697 sine 0.3)+1336 sine 0.3),delay 0.02
:Case 3
tone←tone,(0.5×(697 sine 0.3)+1477 sine 0.3),delay 0.02
:Case 4
tone←tone,(0.5×(770 sine 0.3)+1209 sine 0.3),delay 0.02
:Case 5
tone←tone,(0.5×(770 sine 0.3)+1336 sine 0.3),delay 0.02
:Case 6
tone←tone,(0.5×(770 sine 0.3)+1477 sine 0.3),delay 0.02
:Case 7
tone←tone,(0.5×(852 sine 0.3)+1209 sine 0.3),delay 0.02
:Case 8
tone←tone,(0.5×(852 sine 0.3)+1336 sine 0.3),delay 0.02
:Case 9
tone←tone,(0.5×(852 sine 0.3)+1477 sine 0.3),delay 0.02
:Case 0
tone←tone,(0.5×(941 sine 0.3)+1336 sine 0.3),delay 0.02
:Else
→0
:EndSelect
:EndFor
sound tone

sound x
Sound.{SND_MEMORY PlaySound Wave ⍵}⍪x

z←freq sine dur;⎕IO;n;t
n←⌈sr×dur
t←(⍳n)÷sr
z←1○freq×t×○2

z←delay dur
z←(⌈dur×sr)⍴0
Stu

Posts: 97
Joined: Thu Dec 31, 2015 1:30 am

### Re: sound in APL

Very nice

Here's an alternative solution using d-fns and matrices to represent the keys pressed:

dtmf←{
⍝ ⍵ ←→ vector of numbers [0,9]
⍝ ⍺ ←→ sample rate Hz
⎕IO←0
⍺←8192
tones←↑(⊂941 1336),,697 770 852∘.,1209 1336 1477 ⍝ 0-9
keys←0.3 ⍺ sine⍉(⊂⍵)⌷tones
sound,(⌈⍺×0.5)↑[1]keys
}
sine←{
⍝ ⍺ ←→ (duration seconds) (sample rate Hz)
⍝ ⍵ ←→ sound frequencies (Hz)
⍝ ← ←→ vector of samples of the mixed sine waves
dur sr←⍺
n←⌈sr×dur
t←(⍳n)÷sr
z←+⌿1○⍵∘.×t×○2
z÷≢⍵
}

Looking forward to see the other audio tools. Would be great to put them in a library on GitHub, I would like to contribute and I'm sure others would to.
gil

Posts: 68
Joined: Mon Feb 15, 2010 12:42 am

### Re: sound in APL

I like yours better! I'm still learning how to code in a contemporary style. So, if I start contributing audio tools, someone--such as yourself--will have to translate my code into that style. At least until I get up to speed. When I originally learned APL, there were no nested arrays, and the number of primitives was smaller. Also no OO. Also, the PC and Mac hadn't been invented. The first computer I wrote programs for had less processing power than the microprocessor in your microwave oven. Etc., etc.
Stu

Posts: 97
Joined: Thu Dec 31, 2015 1:30 am

### Re: sound in APL

Did you ever find a solution with just using linux?
larry316

Posts: 16
Joined: Thu Dec 01, 2016 4:28 pm

Previous