Posts: 26
Joined: Wed Jun 09, 2021 10:21 pm

Audio - workaround for pigpiod using PCM?

Sun Jun 20, 2021 8:07 am

Hello. I'm currently using this I2S 3W Amplifier with this 3W 4 Ohm Speaker. I connected the amplifier using GPIO18, 19, and 21. Then, I started using pigpiod and I'm not able to use those pins anymore. I read that PWM pins can be used to output audio, but I'm not sure where to start. I do have some LM386 amplifier chips around. I am also already using the PWM0 pin for some LED strips, so redirecting pigpiod from PCM to PWM won't be an option.

Two options that I am thinking of:
  1. Interface the speaker with an amplifier and a filter using the PWM1 pin. Is this possible?
  2. Pricier option: Buy a different kind of LED strip that interfaces with SPI instead of the pin, use SPI pins to interface, and then redirect pigpiod from PCM to PWM in order to get audio output from PCM
Any suggestions? My code if that's necessary:

Code: Select all

import sys
import pygame as pg
import os
import time

class Speaker:
    def speakerPlay(self, volumeLevel):
        freq = 44100  # audio CD quality
        bitsize = -16  # unsigned 16 bit
        channels = 2  # 1 is mono, 2 is stereo
        buffer = 2048  # number of samples (experiment to get right sound)

        pg.mixer.init(freq, bitsize, channels, buffer)

    def setup_MP3(self, music_file):
        clock = pg.time.Clock()
            print("Music file {} loaded!".format(music_file))
        except pygame.error:
            print("File {} not found! {}".format(music_file, pg.get_error()))

        # Check if playback has finished

    def playSoundFile(self, volumeLevel):
        print("Playing at volume: " + str(volumeLevel)+ "\n")
        mp3s = []
        for file in os.listdir("."):
            if file.endswith(".mp3"):


        for x in mp3s:
            except KeyboardInterrupt:
                raise SystemExit

Code: Select all

from array import array
from time import sleep

import pygame
from pygame.mixer import Sound, get_init, pre_init

class Note(Sound):

    def __init__(self, frequency, volume=.1):
        self.frequency = frequency
        Sound.__init__(self, self.build_samples())

    def build_samples(self):
        period = int(round(get_init()[0] / self.frequency))
        samples = array("h", [0] * period)
        amplitude = 2 ** (abs(get_init()[1]) - 1) - 1
        for time in range(period):
            if time < period / 2:
                samples[time] = amplitude
                samples[time] = -amplitude
        return samples

    def playTone(self, freq_value, volume_level):
        pre_init(44100, -16, 1, 1024)
        Note(freq_value, volume_level).play(-1)
        return self

    def runTone(self):
        freq_value = 440
        volume_level = 0.1
        playTone(freq_value, volume_level)
        return self

Return to “Beginners”