Skip to content

dac.headphone_volume seems to have no effect #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
todbot opened this issue May 2, 2025 · 4 comments
Closed

dac.headphone_volume seems to have no effect #5

todbot opened this issue May 2, 2025 · 4 comments
Assignees

Comments

@todbot
Copy link

todbot commented May 2, 2025

I'm not hearing any change when adjusting the dac.headphone_volume property.

Below is my variation of tlv320_simpletest.py running on Adafruit CircuitPython 10.0.0-alpha.3 on 2025-05-02; Raspberry Pi Pico with rp2040. The perceived volume coming out of the headphones is constant

import time, math, time
import ulab.numpy as np
import board, busio, audiobusio, audiocore, audiomixer
import adafruit_tlv320 

SAMPLE_RATE = 44100
CHANNEL_COUNT = 1
BUFFER_SIZE = 2048

i2c_sda_pin, i2c_scl_pin = board.GP16, board.GP17
i2s_bck_pin, i2s_lck_pin, i2s_dat_pin = board.GP20, board.GP21, board.GP22

i2c = busio.I2C(scl=i2c_scl_pin, sda=i2c_sda_pin)
audio = audiobusio.I2SOut(bit_clock=i2s_bck_pin, word_select=i2s_lck_pin, data=i2s_dat_pin)
dac = adafruit_tlv320.TLV320DAC3100(i2c)
dac.configure_clocks(sample_rate=SAMPLE_RATE, bit_depth=16)
dac.headphone_output = True
dac.headphone_volume = -35  # dB, does not seem to work? 

mixer = audiomixer.Mixer(sample_rate=SAMPLE_RATE, channel_count=CHANNEL_COUNT, buffer_size=BUFFER_SIZE)
audio.play(mixer)

# generate a sine wave
tone_volume = 0.5
frequency = 440
length = SAMPLE_RATE // frequency
sine_wave = np.array(np.sin(np.linspace(0, 2*np.pi, length, endpoint=False))
                     * tone_volume * 32767, dtype=np.int16)
sine_wave_sample = audiocore.RawSample(sine_wave, sample_rate=SAMPLE_RATE)

mixer.voice[0].play(sine_wave_sample, loop=True)

while True:
    for db in range(-60, 0, 5):
        print("setting headphone db:", db)
        dac.headphone_volume = db
        time.sleep(0.5)
@todbot
Copy link
Author

todbot commented May 2, 2025

From looking at the values from the Table 6-24 in the datasheet, I think the setter function in headphone_volume should be something like: gain = int(-2*db)

@BlitzCityDIY BlitzCityDIY self-assigned this May 2, 2025
@BlitzCityDIY
Copy link
Contributor

thanks @todbot i will check this out on monday

@BlitzCityDIY
Copy link
Contributor

so i tried a few things, and had the most success if i change the DAC volume value instead of the headphone volume value. i'll update the library to have a function for that and tag you on the PR for testing if that's okay. one thing that is tricky with this DAC is the order of operations for the multiple gain stages

@BlitzCityDIY
Copy link
Contributor

updated examples and readme to use dac_volume instead with #6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants