B4J Question Sound generator: 2 channels

peacemaker

Expert
Licensed User
Longtime User
Hi, All

Any suggestion how to make a sound generator of 2 channels: one sine wave to the left channel, another sine wave to second channel ?
If non-crossplatform solution, then for Windows.
 

kimstudio

Active Member
Licensed User
Longtime User
I would say the key takeaway here is to fix the sampling rate of audio device output and generate sound samples according to different frequency with this sampling rate, instead of manipulating audio output sampling rate to generate a desired frequency.

It helps to check that the sin is generated correctly
How is it checked, is there a tuner by your side? :)
 
Upvote 0

kimstudio

Active Member
Licensed User
Longtime User
Yes. But I do know what A440 sounds like, or pretty close at least.
Forgot that you have a musician ear. I can't distinguish audio frequency by ear, only sometimes can recognize simple intervals.

To check frequency, I think I will save the wave generated and plot it out to observe the length of a sine cycle and do FFT to get the frequency, just like a tuner app realization. A little bit off topic but still related to generate correct sound.
 
Upvote 0

emexes

Expert
Licensed User
To check frequency, I think I will save the wave generated and plot it out to observe the length of a sine cycle and do FFT to get the frequency, just like a tuner app realization.

Why not just use a tuning app directly? (on a spare phone, presumably)

If you save the wave generated, then there's no 100% guarantee that the sample rate you think the file is saved with, matches the actual sample rate that the Android phone played it at. 99%, yes. Bet-your-life-on-it, no.

I have a vague recollection that one of the music recognition apps (not Shazam - something else) would recognise and identify tones and single notes.
 
Upvote 0

kimstudio

Active Member
Licensed User
Longtime User
If you save the wave generated, then there's no 100% guarantee that the sample rate you think the file is saved with, matches the actual sample rate that the Android phone played it at. 99%, yes. Bet-your-life-on-it, no.
Just for debugging... as the program is written by me then I definitely know what sampling rate it is and before playing I will save the generated wave samples in a binary file and open it on Windows by Matlab or Python and plot them, and check whether generate function is correct or not.

Most of Tuner apps only detect frequency and they don't care what kind of waveshape is, so it is hard to check whether it is a perfect sine wave generated.

I have a vague recollection that one of the music recognition apps (not Shazam - something else) would recognise and identify tones and single notes.
Yes, I also know some single note transcription apps, kind of like make music score by combining tunes detected by a tuner every 10ms. But multiple notes one is one of the hardest missions to achieve and has never been perfect. Some apps can recognize chords with very nice performance though.
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
If to have the half-period phase shift - how better to generate ?
Something like:

B4X:
Public Sub generateSinWav(Freq As Int, Dur As Double, MaxVal As Double, DegreePhaseShift As Int) As Short()
    Dim Arr(Dur * SampleRateInHz) As Short
    Dim SampleInterval As Double = SampleRateInHz  / Freq
  
    For i = 0 To Arr.Length - 1
        Dim shift As Double = DegreePhaseShift / 180 * cPI
        Dim Angle As Double = (2 * cPI * i + shift) / SampleInterval
        Arr(i) = Sin(Angle) * MaxVal
    Next
    Return Arr
End Sub

Dim L() As Short = generateSinWav(Freq1,Dur,Level1 * 32767, 0)
Dim R() As Short = generateSinWav(Freq2,Dur,Level2 * 32767, 180)
?
 
Upvote 0

emexes

Expert
Licensed User
If to have the half-period phase shift - how better to generate ?

I would just have a wavetable of a single sine wave (or square, or triangular, or sawtooth, etc) say 256 elements long (but if you're under pressure for accuracy, then any power of two that you've got the memory available to handle) and then index into that using a phase accumulator that is right-shifted by however many fraction bits are in your phase accumulator. Gets rid of those expensive sin() functions and, if you're ok with being off by a bee's dick of a Hz for frequencies that don't divide perfectly into 11289600 Hz, then you can use integer math everywhere, except maybe for the when you're working out the phase increment per sample period.

Hang on... half-period phase shift of what relative to what? Unless the ratio of the frequencies is integral, then there is no obvious reference point.

It'd help to know what the multi-tone sound is for... DTMF? A dial tone? An air-raid warning system? Mosquito repellant? Police siren?
 
Upvote 0

kimstudio

Active Member
Licensed User
Longtime User
Phase shift:

B4X:
Public Sub generateSinWav(Freq As Int, Dur As Double, MaxVal As Double, DegreePhaseShift As Int) As Short()
    Dim Arr(Dur * SampleRateInHz) As Short
    Dim SampleInterval As Double = SampleRateInHz  / Freq
    
    Dim shift As Double = DegreePhaseShift / 180 * cPI
    For i = 0 To Arr.Length - 1
        Dim Angle As Double = (2 * cPI * i) / SampleInterval - shift '!
        Arr(i) = Sin(Angle) * MaxVal
    Next
    Return Arr
End Sub

Dim L() As Short = generateSinWav(Freq1,Dur,Level1 * 32767, 0)
Dim R() As Short = generateSinWav(Freq2,Dur,Level2 * 32767, 180)
 
Upvote 0

emexes

Expert
Licensed User
or for mouses, rats...

is that really possible... i mean is it "works" something like that ?

Yes, according to the ads. Something about the frequency emitted by female mosquitoes driving competitors away, I think they were claiming.

Similar device for rodents that plugs into powerpoint to generate electromagnetic frequencies that said rodents supposedly dislike - maybe it resonates in their eardrums or something, like US Embassy staff have been complaining about.
 
Upvote 0

emexes

Expert
Licensed User
Upvote 0

emexes

Expert
Licensed User
Yes. Just school generator tool ?
For physics experiments.

Google Direct Digital Synthesis (DDS) and I'm sure there'll be a clear explanation somewhere about generating waveforms to suit your purpose. It can be done in hardware with a fixed oscillator, and accumulator, and wavetable (eg 256 words of memory), and digital-to-analog converter (DAC). To replicate the process in software is straightforward, as long as you're doing it in batches of multiple samples, and not sample-by-single-sample off an timer running at 44100 ticks per second.

Other key words to search for would be: phase angle accumulator waveform table sample digital analog
 
Upvote 0

kimstudio

Active Member
Licensed User
Longtime User
An application for this is:

Binaural Beats

"Binaural beats are a perception of sound created by your brain. If you listen to two tones, each at a different frequency and each in a different ear, your brain creates an additional tone you can hear. This third tone is called a binaural beat.

Preliminary research suggests that binaural beats can help you sleep better. A study using binaural beats at a delta frequency of 3 Hz showed that these beats induced delta activity in the brain."
 
Upvote 0

Magma

Expert
Licensed User
Longtime User
Maybe I was wrong about it being electromagnetic: a large and (mostly-)reputable hardware chain here is selling these, so they must be legit, right???

https://www.bunnings.com.au/ratsak-repel-ultrasonic-rodent-repeller-mini_p0037847
1669889065890.png
 
Upvote 0
Top