Log in

View Full Version : Why SOUND statement produces loud and clear sound, but FREQOUT - does not?



CuriousOne
- 1st May 2026, 13:26
Hello :)
AI has no answer.

even at 64mhz, FREQUOUT provides very low volume, distorted audio, while SOUND, even on 4Mhz 628A chip, produces loud and clear sound.
This is not chip or hardware issue - tested on numerous 12F-16F-18F devices. This is PBP issue as it seems. Can we fix it somehow?

Ioannis
- 1st May 2026, 18:06
Because FREQOUT is using PWM with the ability to output two tones at the same time, so filtering is required to produce a, relatively clean, sinewave-looking tone.

On the otjher hand, SOUND produces only one, rectagular TTL signal, with no filtering. So yes the amplitude is very different.

But you did not showed how you use the output signal, what is your circuit.

Ioannis

CuriousOne
- 7th May 2026, 06:14
Sorry for late reply :)
All I need is for it to play loud and clear.
I've added extra RC filtering circuit, as shown in manual, also I have external transistor amplifying the sound, but output is still not sounding good.

By the way, claude excells in generating PBP code from the uploaded midi files - the code below plays the some cartoon music, converted from the midi file, with 2 voice polyphony using FREQOUT statement:



Main:
FREQOUT SPEAKER, 125, 523
PAUSE 125
FREQOUT SPEAKER, 125, 261, 523
PAUSE 125
FREQOUT SPEAKER, 125, 523
PAUSE 125
FREQOUT SPEAKER, 125, 261, 587
PAUSE 125
FREQOUT SPEAKER, 125, 659
PAUSE 125
FREQOUT SPEAKER, 125, 261, 659
PAUSE 125
FREQOUT SPEAKER, 125, 523
PAUSE 125
FREQOUT SPEAKER, 125, 261, 659
PAUSE 125
FREQOUT SPEAKER, 250, 783
FREQOUT SPEAKER, 125, 329, 783
PAUSE 125
FREQOUT SPEAKER, 250, 659
FREQOUT SPEAKER, 125, 329, 659
PAUSE 125
FREQOUT SPEAKER, 250, 783
FREQOUT SPEAKER, 125, 329, 783
FREQOUT SPEAKER, 250, 783
PAUSE 125
FREQOUT SPEAKER, 125, 329
PAUSE 125
FREQOUT SPEAKER, 125, 698
PAUSE 125
FREQOUT SPEAKER, 125, 293, 698
PAUSE 125
FREQOUT SPEAKER, 125, 698
PAUSE 125
FREQOUT SPEAKER, 125, 293, 659
PAUSE 125
FREQOUT SPEAKER, 125, 587
PAUSE 125
FREQOUT SPEAKER, 125, 293, 587
PAUSE 125
FREQOUT SPEAKER, 125, 698
PAUSE 125
FREQOUT SPEAKER, 125, 293, 880
FREQOUT SPEAKER, 125, 880
FREQOUT SPEAKER, 250, 783
FREQOUT SPEAKER, 125, 195, 783
PAUSE 125
FREQOUT SPEAKER, 250, 698
FREQOUT SPEAKER, 125, 195, 698
PAUSE 125
FREQOUT SPEAKER, 250, 783
FREQOUT SPEAKER, 125, 195, 783
FREQOUT SPEAKER, 250, 783
PAUSE 125
FREQOUT SPEAKER, 125, 195
PAUSE 125
FREQOUT SPEAKER, 250, 659
FREQOUT SPEAKER, 125, 261, 659
FREQOUT SPEAKER, 125, 659
PAUSE 250
FREQOUT SPEAKER, 125, 261, 698
FREQOUT SPEAKER, 125, 698
FREQOUT SPEAKER, 250, 783
FREQOUT SPEAKER, 125, 261, 783
PAUSE 125
FREQOUT SPEAKER, 250, 659
FREQOUT SPEAKER, 125, 261, 659
PAUSE 125
FREQOUT SPEAKER, 125, 880
PAUSE 125
FREQOUT SPEAKER, 125, 220, 880
PAUSE 125
FREQOUT SPEAKER, 125, 880
PAUSE 125
FREQOUT SPEAKER, 125, 220, 987
PAUSE 125
FREQOUT SPEAKER, 250, 1046
FREQOUT SPEAKER, 125, 220
PAUSE 125
FREQOUT SPEAKER, 250, 880
FREQOUT SPEAKER, 125, 220, 880
PAUSE 125
FREQOUT SPEAKER, 125, 1174
PAUSE 125
FREQOUT SPEAKER, 125, 293, 1174
PAUSE 125
FREQOUT SPEAKER, 250, 1174
FREQOUT SPEAKER, 125, 293, 1046
PAUSE 125
FREQOUT SPEAKER, 250, 987
FREQOUT SPEAKER, 125, 195, 783
FREQOUT SPEAKER, 125, 783
FREQOUT SPEAKER, 250, 987
FREQOUT SPEAKER, 125, 195, 1174
FREQOUT SPEAKER, 125, 1174
FREQOUT SPEAKER, 250, 1046
FREQOUT SPEAKER, 125, 261, 1046
FREQOUT SPEAKER, 125, 1046
FREQOUT SPEAKER, 250, 987
FREQOUT SPEAKER, 125, 261, 987
FREQOUT SPEAKER, 125, 987
FREQOUT SPEAKER, 250, 880
FREQOUT SPEAKER, 125, 220, 880
FREQOUT SPEAKER, 375, 880
FREQOUT SPEAKER, 125, 220
PAUSE 125
FREQOUT SPEAKER, 250, 698
FREQOUT SPEAKER, 125, 293, 698
FREQOUT SPEAKER, 250, 698
PAUSE 125
FREQOUT SPEAKER, 125, 293, 783
FREQOUT SPEAKER, 125, 783
FREQOUT SPEAKER, 250, 880
FREQOUT SPEAKER, 125, 293
PAUSE 125
FREQOUT SPEAKER, 250, 1046
FREQOUT SPEAKER, 125, 293, 1046
PAUSE 125
FREQOUT SPEAKER, 250, 987
FREQOUT SPEAKER, 125, 195, 987
PAUSE 125
FREQOUT SPEAKER, 250, 880
FREQOUT SPEAKER, 125, 195, 880
PAUSE 125
FREQOUT SPEAKER, 250, 987
FREQOUT SPEAKER, 125, 195, 987
PAUSE 125
FREQOUT SPEAKER, 250, 1174
FREQOUT SPEAKER, 125, 195, 1174
PAUSE 125
FREQOUT SPEAKER, 250, 1318
FREQOUT SPEAKER, 125, 261, 1174
PAUSE 125
FREQOUT SPEAKER, 250, 1046
FREQOUT SPEAKER, 125, 261, 1046
FREQOUT SPEAKER, 125, 1046
PAUSE 250
FREQOUT SPEAKER, 125, 261, 1046
PAUSE 125
FREQOUT SPEAKER, 125, 1046
PAUSE 125
FREQOUT SPEAKER, 125, 261, 987
PAUSE 125
FREQOUT SPEAKER, 250, 1046
FREQOUT SPEAKER, 125, 220, 987
FREQOUT SPEAKER, 125, 987
FREQOUT SPEAKER, 250, 880
FREQOUT SPEAKER, 125, 220, 880
FREQOUT SPEAKER, 250, 880
PAUSE 125
FREQOUT SPEAKER, 125, 220, 523
FREQOUT SPEAKER, 125, 523
FREQOUT SPEAKER, 250, 587
FREQOUT SPEAKER, 125, 220, 659
FREQOUT SPEAKER, 125, 659
FREQOUT SPEAKER, 250, 698
FREQOUT SPEAKER, 125, 293, 698
FREQOUT SPEAKER, 125, 698
FREQOUT SPEAKER, 250, 698
FREQOUT SPEAKER, 125, 293, 698
FREQOUT SPEAKER, 250, 698
PAUSE 125
FREQOUT SPEAKER, 125, 293, 659
FREQOUT SPEAKER, 125, 659
FREQOUT SPEAKER, 250, 698
FREQOUT SPEAKER, 125, 293, 659
FREQOUT SPEAKER, 125, 659
FREQOUT SPEAKER, 125, 587
PAUSE 125
FREQOUT SPEAKER, 250, 195
FREQOUT SPEAKER, 125, 293
PAUSE 125
FREQOUT SPEAKER, 125, 195, 293
FREQOUT SPEAKER, 250, 195
PAUSE 125
FREQOUT SPEAKER, 125, 195, 293
FREQOUT SPEAKER, 125, 195
FREQOUT SPEAKER, 125, 195, 293
PAUSE 125
FREQOUT SPEAKER, 250, 195
PAUSE 500


GOTO Main ' Loop forever

Ioannis
- 7th May 2026, 20:17
As posted above, the two commands work completely different so one cannot compare them.

They were designed for different tasks.

What exactly is your need?

If you need one tone only, use SOUND and get clear loud TTL wave form of 0-5 volt amplitude. Otherwise you need a lot of amplification. Just a transistor is not enough.

You did not showed how you connected that transistor. For FREQOUT I guess you need more than a transistor as the filtering is making the square wave look more like a sinus waveform. From 0-5 volt you are getting a sinus of mVolts. So your transistor amplifier should have a lot of gain, and work as a linear amplifier, not like a switch (which makes a lot of distortion).

Ioannis

CuriousOne
- 10th May 2026, 09:57
I need to play music, standard musical notation.
For FREQOUT you can specify frequencies and they do work.
For SOUND you can not specify frequency, so can't use standard musical notations.
I'm considering writing own "library" for musical note playback.

Ioannis
- 11th May 2026, 11:19
The FREQOUT is the way to go.

You did not post the circuit of transistor amplifier. I suppose you did add a good bias to the transistor, right?

The audio output after the good filtering I suppose will be of some mVolts, so good amplification is needed. Don't expect to drive directly a loudspeaker but use say an LM386 chip amp or bigger for more watts.

Ioannis

CuriousOne
- 12th May 2026, 15:10
I even tried to connect external amplifier.

Freqout has way too many harmonics, at frequencies close to the original sound, so unfortunately, not usable.

CuriousOne
- 12th May 2026, 15:59
Here's the code, generated by Claude, which does what I need. While it contains only one active, the idea is clear and code can be modified for wider coverage:



ty;----[16F1937 Hardware Configuration]-------------------------------------------
#IF __PROCESSOR__ = "16F1936"
#DEFINE MCU_FOUND 1
#CONFIG
cfg1 = _FOSC_INTOSC ; INTOSC oscillator: I/O function on CLKIN pin
cfg1&= _WDTE_OFF ; WDT disabled
cfg1&= _PWRTE_OFF ; PWRT disabled
cfg1&= _MCLRE_OFF ; MCLR/VPP pin function is digital input
cfg1&= _CP_ON ; Program memory code protection is enabled
cfg1&= _CPD_OFF ; Data memory code protection is disabled
cfg1&= _BOREN_OFF ; Brown-out Reset disabled
cfg1&= _CLKOUTEN_OFF ; CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin
cfg1&= _IESO_ON ; Internal/External Switchover mode is enabled
cfg1&= _FCMEN_ON ; Fail-Safe Clock Monitor is enabled
__CONFIG _CONFIG1, cfg1


cfg2 = _WRT_OFF ; Write protection off
cfg2&= _VCAPEN_OFF ; All VCAP pin functionality is disabled
cfg2&= _PLLEN_OFF ; 4x PLL disabled
cfg2&= _STVREN_ON ; Stack Overflow or Underflow will cause a Reset
cfg2&= _BORV_19 ; Brown-out Reset Voltage (Vbor), low trip point selected.
cfg2&= _LVP_OFF ; High-voltage on MCLR/VPP must be used for programming
__CONFIG _CONFIG2, cfg2


#ENDCONFIG


#ENDIF


;----[Verify Configs have been specified for Selected Processor]----------------
; Note: Only include this routine once, after all #CONFIG blocks
#IFNDEF MCU_FOUND
#ERROR "No CONFIGs found for [" + __PROCESSOR__ +"]"
#ENDIF


'nixie led clock electronics direct drive





ADCON1=%11110011 'enable adc internal reference and justify
FVRCON=%11001111 'set internal reference to 4.096V
OSCCON=%11110000 'SET INTOSC TO 8MHZ
'ANSELE=%00000000 'enable adc input porte.2
ANSELA=%00000000 'disable ADC on A
ANSELB=%00000000 'disable ADC on B
'ANSELD=%00000000 'disable ADC on D
TRISC=%00000000 'set PORTC as output all
'TRISD=%00000000 'set PORTD as output all
TRISB=%00001111 'set PORTB as output all
TRISA=%00000000 'set PORTA 2 as input, others as output
TRISE=%00000000 'set PORTE as output all
WPUB=%00000000 'DISABLE B PULL UPS
OPTION_REG=%100000000
LCDCON=%00000000 'disable LCD controller pins
LCDSE0=%00000000
LCDSE1=%00000000
'LCDSE2=%00000000


DEFINE OSC 32




PORTA.3 = 0


' --- Variables ---
nCycle var WORD ' loop counter
i var BYTE ' note index


' ================================================== ==========
' Subroutine: PlayNote
' Parameters passed via variables (PBP3 style)
' nHalf = half-period in microseconds (WORD)
' nCount = number of cycles (WORD)
' ================================================== ==========
nHalf var WORD
nCount var WORD


GOSUB WaitStart ' small startup delay


' ================================================== ==========
' MAIN - play C4 through C5
' ================================================== ==========
Main:


' --- C4 261.63 Hz half=1911µs cycles=261 ---
nHalf = 1911 : nCount = 261 : GOSUB PlayNote : GOSUB Gap


' --- D4 293.66 Hz half=1703µs cycles=293 ---
nHalf = 1703 : nCount = 293 : GOSUB PlayNote : GOSUB Gap


' --- E4 329.63 Hz half=1516µs cycles=329 ---
nHalf = 1516 : nCount = 329 : GOSUB PlayNote : GOSUB Gap


' --- F4 349.23 Hz half=1432µs cycles=349 ---
nHalf = 1432 : nCount = 349 : GOSUB PlayNote : GOSUB Gap


' --- G4 392.00 Hz half=1276µs cycles=392 ---
nHalf = 1276 : nCount = 392 : GOSUB PlayNote : GOSUB Gap


' --- A4 440.00 Hz half=1136µs cycles=440 ---
nHalf = 1136 : nCount = 440 : GOSUB PlayNote : GOSUB Gap


' --- B4 493.88 Hz half=1012µs cycles=493 ---
nHalf = 1012 : nCount = 493 : GOSUB PlayNote : GOSUB Gap


' --- C5 523.25 Hz half=955µs cycles=523 ---
nHalf = 955 : nCount = 523 : GOSUB PlayNote : GOSUB Gap


PAUSE 2000 ' 2-second rest before repeating
GOTO Main


' ================================================== ==========
' PlayNote: toggles RC0 nCount times with nHalf µs delay
' ================================================== ==========
PlayNote:
FOR nCycle = 1 TO nCount
HIGH PORTA.3
PAUSEUS nHalf
LOW PORTA.3
PAUSEUS nHalf
NEXT nCycle
RETURN


' ================================================== ==========
' Gap: 50ms silence between notes (pin stays LOW)
' ================================================== ==========
Gap:
PORTA.3 = 0
PAUSE 50
RETURN


' ================================================== ==========
' WaitStart: 200ms power-on settle delay
' ================================================== ==========
WaitStart:
PAUSE 200
RETURN


END

Ioannis
- 14th May 2026, 12:51
I even tried to connect external amplifier.

Freqout has way too many harmonics, at frequencies close to the original sound, so unfortunately, not usable.

With FREQOUT you need good filtering.This removes any harmonics and smoothens the sound, with the consequence of lower audio level. Of course you need to calculate the filter knee, not just throw a cap and a resistor and get over with. Better use higher order filters with an opamp.

And then is a must to use normal Audio amplifiers. Not just a transistor in swithcing mode of operation. Sound is analog so you need good filtering and amplification.

The latest code you posted produces sharp rectangular pulses, so the harmonics are all over the place! This is what produces harmonics! But I guess it is appealing to the ears.

Ioannis