Gatsby25
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)
        self.playSoundFile(volumeLevel)

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

        pg.mixer.music.play()

        # Check if playback has finished
        while pg.mixer.music.get_busy():
            clock.tick(30)

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

        print(mp3s)

        for x in mp3s:
            try:
                self.setup_MP3(x)
                time.sleep(.25)
            except KeyboardInterrupt:
                pg.mixer.music.fadeout(1000)
                pg.mixer.music.stop()
                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())
        self.set_volume(volume)

    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
            else:
                samples[time] = -amplitude
        return samples

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

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


Return to “Beginners”