Menu | JAQForum Ver 19.10.27 |
Forum Index : Microcontroller and PC projects : Putting PLAY LOAD SOUND to work.
For sound effects MOD is excellent but resource hungry, sawtooth, triangle and square waves only have odd** harmonics so are not the most pleasant. Sine is dull. PLAY LOAD SOUND can provide any mix of harmonics so here is a program that goes up to the eighth harmonic. ** In physics and mathematics the fundamental frequency is also the first harmonic as the equations are a little simpler that way while many musicians regard 2 x fundamental as the first. This uses the latter as I had trouble with PLAY LOAD SOUND accepting OPTION BASE 1. It generates 3 test tones that you can easily modify to your own preference. Changing the phase shifts makes a difference to the shape of the wave on the scope but to me, little difference to the sound. ' Print "Create array for PLAY LOAD SOUND v02.bas" Option base 0 Dim integer PLS01(1023), harmonics01(1,7) = (1000,0, 500,-90, 0,0, 400,-45, 0,0 ,300,-30, 0,0, 200,-12) Dim integer PLS02(1023), harmonics02(1,7) = (1000,0, 0,0, 0,0, 0,0, 0,0 ,0,0, 0,0, 0,0) Dim integer PLS03(1023), harmonics03(1,7) = (0,0, 1000,0, 0,0, 0,0, 0,0 ,0,0, 0,0, 0,0) 'harmonics array has 8 pairs of amplitude & phase shift (degrees - of fundamental F) values ' the first is the fundamental followed by the harmonics in ascending order. Print "Wait a while for the arrays to be calculatyed" :Print PLS_maker PLS01(), harmonics01() :Print "array 01 filled" PLS_maker PLS02(), harmonics02() :Print "array 02 filled" PLS_maker PLS03(), harmonics03() :Print "array 03 filled" :Print Play LOAD SOUND PLS01() Play SOUND 1, B, U, 440, 25 Print "array 01 now playing, even harmonics" Pause 5000 Play STOP Play LOAD SOUND PLS02() Play SOUND 2, B, U, 440, 25 Print "array 02 now playing, fundamental only" Pause 5000 Play STOP Play LOAD SOUND PLS03() Play SOUND 3, B, U, 440, 25 Print "array 03 now playing, fundamental supressed - 2nd harmonic only" Print :Print "Press ctrl-C to stop" Do 'Your program here Loop Sub PLS_maker PLSout%(), harmonics%() Local float PLS(4095), minin, maxin Local integer PLSs(4095), n, m For m=0 To 7 ' Print m, harmonics%(0,m),, harmonics%(1,m)/180 For n=0 To 4095 Inc PLS(n), harmonics%(0,m) * Sin(n * (m+1) * Pi/2023 + Pi*harmonics%(1,m)/180) Next Next Math WINDOW PLS(), 0, 4095, PLSs(), minin, maxin ' Print "Peak to Peak value "; maxin - minin Memory PACK PLSs(), PLSout%(), 4096, 16 End Sub Set the first amplitude to any convenient value such as 100 or 1000 to make it easier to see the relative size of the others. Math WINDOW will resize the P-P amplitude in the final array to the max. allowed. Edited 2024-07-25 15:03 by phil99 |
||||||
Nice work phill, This is a good example how to use the command. I never played with it before,so this is a teaser. Volhout |
||||||
Hey Phil, you just reminded me of a post that I forgot to make. A short while ago, a couple of our members were lamenting about having the skills and resources but no projects. I don't have the time or I would be doing something that is definitely needed by solo musicians and other performers who can no-longer justify a full band (cost/floor-space/drunken-drummers, etc.). Backing tracks have been around forever but are regarded by many as sounding tacky. However, professional-quality tracks are available where the instruments are separated and become their own audio files. This is much better because a performer might only need/want the bass/drums and maybe a backing guitar/vocals etc. So. We download these individual tracks and using a DAW (digital audio workstation) such as Audacity (I prefer the new, totally free Cakewalk), we create/modify these files to suit our performance and eventually mix-down to mp3. The problem is that; the mix is fixed. It might sound great at one venue but not-so-great at another. Ideally we want to be able to adjust the levels of each "instrument" to suit the venue's acoustics. I'm thinking about a PicoMite-per-track (I guess there is the option of two mono tracks-per-PicoMite). Maybe a pot or a rotary encoder to set levels, etc. Pretty certain that for the duration of a song, wouldn't be a problem to keep the PicoMites in sync. Most musicians are luddites who don't want to be messing around with tablets and laptops on stage. Wish I had the time ![]() One source of backing tracks It takes a bit of time to download each instrument individually but this flexibility is great because the results can be much more realistic. Edit: Oops, maybe this isn't quite on-topic. Edited 2024-07-25 15:46 by PhenixRising |
||||||
A much simpler way to get even harmonics:- > freq = 440 > Play SOUND 1, B, S, freq, 25 : Play SOUND 2, B, T, freq*2, 15 > DOH! |
||||||
@twofingers has been using Play LOAD SOUND for sound samples of many cycles in a shaped envelope. Here is a rudimentary program to make some of your own. Print "PLAY LOAD SOUND envelope generator for multi cycle waveforms" ' You may need to use OPTION CONTINUATION LINES ON to fit the longest line ' Up to 4 simultaneous tones can be produced. For less than 4 tones set 0 amplitude for the excess. ' The sound frequency is the number of cycles/sample * PLAY SOUND frequency. ' Keep sound frequencies below 22kHz to avoid aliasing, unless you want that effect. Dim float wave1=500, wave2=1000, wave3=1500, wave4=2000, waveS 'set the number of cycles/sample array Dim float amp1=2048, amp2=1200, amp3=500, amp4=900 'amplitude of each wave (%) Option BASE 0 Dim integer envelope(4095), sample(1023), n Dim Float minin, maxin 'main For n = 0 To 4095 'Choose constant frequency or a frequency sweep, or add your own. 'generate the basic waveform - constant tone, multi frequency ' envelope(n) = amp1*Sin(2*Pi*wave1/4096*n)+ amp2*Sin(2*Pi*wave2/4096*n)+ amp3*Sin(2*Pi*wave3/4096*n)+ _ 'amp4*Sin(2*Pi*wave4/4096*n) 'rising frequency sweep, using wave1 and wave2 for the frequency range - Wail. ' waveS = wave1 + (wave2 - wave1) * (n+1) / 4096 : envelope(n) = amp1*Sin(2*Pi*waveS/4096*n) 'falling frequency sweep, using wave1 and wave2 for the frequency range - Dive. waveS = wave2 - (wave2 - wave1) * (n+1) / 4096 : envelope(n) = amp1*Sin(2*Pi*waveS/4096*n) ' Enable one (or none) of the following envelope shapes, or add your own. envelope(n) = envelope(n) * -(Cos(2*Pi*n/4096)-1) 'Bleep ' envelope(n) = envelope(n) * n / 4095 'Yip ' envelope(n) = envelope(n) * (1 - n / 4095) 'Ding Next 'Math V_Print envelope() Math WINDOW envelope(), 0, 4095, envelope(), minin, maxin 'scale to fit Play Load Sound range Memory Pack envelope(), sample(), 4096, 16 'pack into 16 bit samples Play Load Sound sample() Play Sound 1, "M", U, 2, 25 'Adjust the parameters and move into your program 'Print minin, maxin Do 'Your program here Loop |
||||||
Don't triangle waves only have both odd and even harmonics? Square are only odd. Sawtooth is a mix with the odd/even ratio variable. Sine are simply all integer multiples of the fundamental. Well, I thought so.... :) |
||||||
HI Phil, A late reaction at your previous post. There are a few issues with PLAY SOUND that makes it less attractive: 1/ It starts when you execute the basic command (doh..) but that makes it difficult to synchronize 2 waveforms. i.e. PLAY SOUND 1,L,S,fr,25 and PLAY SOUND 2,L,Q,fr,25 do not synchronize the square wave and the sine wave. 2/ Above example 1/ adds the amplitudes of the 2 waveforms, but when you do this: PLAY SOUND 1,L,S,fr,25 and PLAY SOUND 2,L,S,fr,25 you do not get an amplitude of 50%... I still have to play with PLAY LOAD SOUND.. Volhout |
||||||
Hi Mick and Harm, Triangle and square both only have odd harmonics and their amplitudes are even the same, decreasing with frequency in the same manner as a capacitor discharging. the only difference is their phases. For a square wave they all start rising from zero at the same time. For a triangle every alternate harmonic is inverted. A sinewave has only the fundamental. Adding any harmonics changes the shape. 1/ Synchronization is tricky, but you can get fairly close with a pause between them and adjusting by trial and error. Change the CPU speed and you may have to start again. 2/ The net amplitude depends on how the phases line up. If the frequencies differ by 1 Hz the amplitude will reach 50% once a second. |
||||||
Hi Phil, Can you synchronize with PLAY PAUSE - PLAY RESUME ? Volhout |
||||||
I haven't tried that, will have to give it a go. |
||||||
No: all it does is disable the interrupt leaving the state of all sound phase calculators untouched |
||||||
Thanks Phil. It's many years since I looked at waveforms and harmonics. I think I was reading the new magazine "Practical Electronics" around then! Probably mid 1960s... I've only really had any interest in sine and square since then, for occasional audio tests. |
||||||
1kHz close to synchronized, almost 50%. PicoMite MMBasic RP2040 Edition V6.00.02 OPTION CPUSPEED (KHz) 200000 > Play Sound 1, L, S, 1000, 25 : pause 0.85 : Play Sound 2, L, S, 1000, 25 > Close to 180°, almost 0%. > > Play Sound 1, L, S, 1000, 25 : pause 1.32 : Play Sound 2, L, S, 1000, 25 > Edited 2025-07-07 23:18 by phil99 |
||||||
![]() |
The Back Shed's forum code is written, and hosted, in Australia. |