see-say-math-tables-mp3 using CPB + TFT Gizmo

CircuitPython on hardware including Adafruit's boards, and CircuitPython libraries using Blinka on host computers.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
User avatar
V2man
 
Posts: 704
Joined: Mon Dec 03, 2018 12:38 am

see-say-math-tables-mp3 using CPB + TFT Gizmo

Post by V2man »

@ jepler My mp3 py examples are based on say-temp.py by Mike Barela for Adafruit Industries found here https://learn.adafruit.com/make-it-talk/numbers You may find them useful in testing noise.
I made the 0.mp3, 1.mp3, 2.mp3 ... using https://text-to-speech-demo.ng.bluemix.net/
I don't know how I can send you a zip file containing the mp3 audio files. It is about 450 KB in size.
Wav audio files from the Make It talk example sound much better than the mp3 files I created!
You may want to make your own equivalent mp3 audio files.
Here's my mp3 example:

Code: Select all

# see-say-math-tables-mp3.py
# Uses mp3 audio files
# Based wav version say-temp.py by Mike Barela
# for Adafruit Industries, MIT License

import time
import board
import microcontroller
import analogio
import displayio
import terminalio
import audiomp3
from time import sleep
from digitalio import DigitalInOut, Direction, Pull
from adafruit_display_text import label
# from adafruit_bitmap_font import bitmap_font
from adafruit_gizmo import tft_gizmo

try:
    from audiocore import WaveFile
except ImportError:
    from audioio import WaveFile

try:
    from audioio import AudioOut
except ImportError:
    try:
        from audiopwmio import PWMAudioOut as AudioOut
    except ImportError:
        pass  # not always supported by every board!

display = tft_gizmo.TFT_Gizmo()
font = terminalio.FONT
text_group = displayio.Group(max_size=15, scale=2, x=0, y=0)
space_15 = "               "
# Create the text labels
text_area1 = label.Label(font, text=space_15, color=0x00FF00)
# append the labels to a group
text_group.append(text_area1)
display.show(text_group)

maxnum = 10
step = 1
D1 = board.BUTTON_A
D2 = board.BUTTON_B

# Button A setup (Addition)
button_a = DigitalInOut(D1)
button_a.direction = Direction.INPUT
button_a.pull = Pull.DOWN
# Button B setup (Multiplication)
button_b = DigitalInOut(D2)
button_b.direction = Direction.INPUT
button_b.pull = Pull.DOWN

# Audio playback object and helper to play a full file
aout = AudioOut(board.SPEAKER)

# Play a mp3 file
def play_file(mp3file):
    mp3file = "/numbers_mp3/" + mp3file
    print("Playing", mp3file)
    with open(mp3file, "rb") as data:
        mp3 = audiomp3.MP3Decoder(data)
        aout.play(mp3)
        while aout.playing:
            pass

# Function should take an integer -299 to 299 and say it
# Assumes mp3 files are available for the numbers
def say_num(n):
    if n < 0:
        play_file("negative.mp3")
        n = - n
    if n >= 200:
        play_file("200.mp3")
        n = n - 200
    elif n >= 100:
        play_file("100.mp3")
        n = n - 100
    if (n >= 0 and n < 20) or n % 10 == 0:
        play_file(str(n) + ".mp3")
    else:
        play_file(str(n // 10) + "0.mp3")
        n = n - ((n // 10) * 10)
        play_file(str(n) + ".mp3")


while True:

    if button_a.value:
        # play_file("addition.mp3")
        for n in range(1, maxnum + 1, step):
            # Set the locations
            text_area1.x = 20
            text_area1.y = 0
            for m in range(0, maxnum + 1, step):
                text_area1.y += 10
                sum = n + m
                text_area1.text = "%2.0f" % m + "  + " + "%2.0f" % n + "  = " + "%2.0f" % sum
                say_num(m)
                play_file("plus.mp3")
                say_num(n)
                play_file("is.mp3")
                say_num(sum)
                time.sleep(0.5)

    if button_b.value:
        # play_file("Multiplication.mp3")
        for n in range(1, maxnum + 1, step):
            # Set the locations
            text_area1.x = 20
            text_area1.y = 0
            for m in range(0, maxnum + 1, step):
                text_area1.y += 10
                prod = n * m
                text_area1.text = "%2.0f" % m + "  x " + "%2.0f" % n + "  = " + "%2.0f" % prod
                say_num(m)
                play_file("times.mp3")
                say_num(n)
                play_file("is.mp3")
                say_num(prod)
                time.sleep(0.5)
    time.sleep(0.01)
    text_area1.text = " "
For CPB only. Here's another much shorter mp3 example. maxnum and countby variables control length of audio

Code: Select all

# say-number-mp3.py
# Based wav version say-temp.py by Mike Barela
# for Adafruit Industries, MIT License

import time
import board
import audiomp3
from digitalio import DigitalInOut, Direction, Pull

try:
    from audiocore import WaveFile
except ImportError:
    from audioio import WaveFile

try:
    from audioio import AudioOut
except ImportError:
    try:
        from audiopwmio import PWMAudioOut as AudioOut
    except ImportError:
        pass  # not always supported by every board!

maxnum = 10
countby = 2
D1 = board.BUTTON_A
D2 = board.BUTTON_B

# Button A setup (CountDown)
button_a = DigitalInOut(D1)
button_a.direction = Direction.INPUT
button_a.pull = Pull.DOWN
# Button B setup (CountUp)
button_b = DigitalInOut(D2)
button_b.direction = Direction.INPUT
button_b.pull = Pull.DOWN

# Audio playback object and helper to play a full file
aout = AudioOut(board.SPEAKER)

# Play a mp3 file
def play_file(mp3file):
    mp3file = "/numbers_mp3/" + mp3file
    print("Playing", mp3file)
    with open(mp3file, "rb") as data:
        mp3 = audiomp3.MP3Decoder(data)
        aout.play(mp3)
        while aout.playing:
            pass

# Function should take an integer -299 to 299 and say it
# Assumes mp3 files are available for the numbers
def say_num(n):
    if n < 0:
        play_file("negative.mp3")
        n = - n
    if n >= 200:
        play_file("200.mp3")
        n = n - 200
    elif n >= 100:
        play_file("100.mp3")
        n = n - 100
    if (n >= 0 and n < 20) or n % 10 == 0:
        play_file(str(n) + ".mp3")
    else:
        play_file(str(n // 10) + "0.mp3")
        n = n - ((n // 10) * 10)
        play_file(str(n) + ".mp3")


while True:

    if button_a.value:
        play_file("CountDownBegin.mp3")
        for n in range(maxnum, -1, -countby):
            say_num(n)
        play_file("blastoff.mp3")

    if button_b.value:
        play_file("CountUpBegin.mp3")
        for n in range(0, maxnum + 1, countby):
            say_num(n)
        play_file("MaximumCount.mp3")
    time.sleep(0.01)

User avatar
siddacious
 
Posts: 407
Joined: Fri Apr 21, 2017 3:09 pm

Re: see-say-math-tables-mp3 using CPB + TFT Gizmo

Post by siddacious »

Please reply or update your post with additional context so that we can understand exactly what you're asking. We answer and are asked many questions every day so it's easy to lose the thread. Thanks!

User avatar
V2man
 
Posts: 704
Joined: Mon Dec 03, 2018 12:38 am

Re: see-say-math-tables-mp3 using CPB + TFT Gizmo

Post by V2man »

Maybe someone will find this or parts of it useful in their project.
Adafruit CircuitPython 5.0.0-beta.4 on 2020-01-22;
Adafruit Circuit Playground Bluefruit with nRF52840
Jan. 8, 2020 libraries

@ Jepler is studying audio noise.
The examples posted recite mp3 audio files. I chose mp3 instead of wave files because of their size.
I do think mp3 files sound more distorted than wav files.
I already stated how I made them. I used American English (en-US): LisaV3 (female, enhance dnn)
Unzip the attached file using 7- Zip and copy the directory, numbers_mp3 to a CPB CIRCUITPY drive.
Copy one of the py examples I already submitted and rename it code.py on the CIRCUITPY drive.
These are libraries I have under the CIRCUITPY/lib.
see-say-lib.PNG
see-say-lib.PNG (11.24 KiB) Viewed 196 times
Press button A or button B on CPB and listen while the mp3 audio files are being recited using a probe with Spectrum Analyzer and speaker connected to a TFT Gizmo or Crickit audio connector .
Maybe this setup or something similar will help you investigate audio distortion and noise. Create a setup for comparing the quality of playing wav versus mp3 files.
Maybe you do not want to use synthesized audio as a source and prefer to make your own where you have better control on how it was made.
Attachments
numbers_mp3.7z
(425.98 KiB) Downloaded 6 times
Last edited by V2man on Fri Jan 31, 2020 11:50 pm, edited 3 times in total.

User avatar
V2man
 
Posts: 704
Joined: Mon Dec 03, 2018 12:38 am

Re: see-say-math-tables-mp3 using CPB + TFT Gizmo

Post by V2man »

I'd like to know is there a better way I could have made the audio mp3 files?
Process them through Audacitiy's effects of amplify and noise reduction to improve sound?
Do mp3 files sound more distorted than wav files in a CPB + TFT Gizmo setup?
What is product number for a connector/cable that will fit in the TFT Gizmo JST audio connector?
What speakers do you recommend to connect to the TFT Gizmo for best sound?

User avatar
V2man
 
Posts: 704
Joined: Mon Dec 03, 2018 12:38 am

Re: see-say-math-tables-mp3 using CPB + TFT Gizmo

Post by V2man »

@ jepler
CPB + Crickit. Crickit audio out is connected to an external speaker.
Crickit power is supplied by 5V 2A switching AC/DC adapter https://www.adafruit.com/product/276
Recorded the attached audio wav clip using Audacity. This will give you a close idea of the noise I am hearing.
I renamed see-say-clip.wav as see-say-clip.txt and uploaded to get around the limitation of not allowing the extension wav on your forum site.
To listen to the clip see-say-clip.txt must be renamed back to see-say-clip.wav.
How can I reduce the background noise?
Attachments
see-say-clip.txt
(977.74 KiB) Downloaded 4 times

User avatar
V2man
 
Posts: 704
Joined: Mon Dec 03, 2018 12:38 am

Re: see-say-math-tables-mp3 using CPB + TFT Gizmo

Post by V2man »

Same CPB + Crickit setup as before. Turning down potentiometer on Crickit board helps.
Below is a py file that will play mp3 when button A is pressed and wav when button B is pressed on CPB board.
The attached zip files contain individual mp3 files in the num0-1-mp3 directory (221 KB) and individual wav files in num0-100-wav (1.3 MB) directory.
It's the clicking sounds after each number is recited that I don't like!
Playing mp3 or wav files produces about the same background noise and clicking.
So mp3 does not sound any worse than wav file. I was wrong!
Is there anything I can do to reduce the clicking and background sounds?

Code: Select all

# say-number-wav-mp3.py
# Based wav version say-temp.py by Mike Barela
# for Adafruit Industries, MIT License

import time
import board
import audiomp3
from digitalio import DigitalInOut, Direction, Pull

try:
    from audiocore import WaveFile
except ImportError:
    from audioio import WaveFile
 
try:
    from audioio import AudioOut
except ImportError:
    try:
        from audiopwmio import PWMAudioOut as AudioOut
    except ImportError:
        pass  # not always supported by every board!
        
maxnum = 100
countby = 2
D1 = board.BUTTON_A
D2 = board.BUTTON_B

# Button A setup (Count using wav files)
button_a = DigitalInOut(D1)
button_a.direction = Direction.INPUT
button_a.pull = Pull.DOWN
# Button B setup (Count using mp3 files)
button_b = DigitalInOut(D2)
button_b.direction = Direction.INPUT
button_b.pull = Pull.DOWN

# Audio playback object and helper to play a full file

def play_mp3file(mp3file):
    mp3file = "/num0-100-mp3/" + mp3file
    print("Playing ", mp3file)
    with open(mp3file, "rb") as mp3:
        with AudioOut(board.SPEAKER) as audio:
            mp3 = audiomp3.MP3Decoder(mp3)
            audio.play(mp3)
            while audio.playing:
                pass

                
def play_wavfile(wavfile):
    wavfile = "/num0-100-wav/" + wavfile
    print("Playing ", wavfile)
    wave_file = open(wavfile, "rb")
    with WaveFile(wave_file) as wave:
        with AudioOut(board.SPEAKER) as audio:
            audio.play(wave)
            while audio.playing:
                pass

# Function should take an integer -299 to 299 and say it
# Assumes mp3 files are available for the numbers
def say_num(type, n):
    if type == "mp3":
        if n < 0:
            play_mp3file("negative." + type)
            n = - n
        if n >= 200:
            play_mp3file("200." + type)
            n = n - 200
        elif n >= 100:
            play_mp3file("100." + type)
            n = n - 100
        if (n >= 0 and n < 20) or n % 10 == 0:
            play_mp3file(str(n) + "." + type)
        else:
            play_mp3file(str(n // 10) + "0." + type)
            n = n - ((n // 10) * 10)
            play_mp3file(str(n) + "." + type)
    else:
        if n < 0:
            play_wavfile("negative." + type)
            n = - n
        if n >= 200:
            play_wavfile("200." + type)
            n = n - 200
        elif n >= 100:
            play_wavfile("100." + type)
            n = n - 100
        if (n >= 0 and n < 20) or n % 10 == 0:
            play_wavfile(str(n) + "." + type)
        else:
            play_wavfile(str(n // 10) + "0." + type)
            n = n - ((n // 10) * 10)
            play_wavfile(str(n) + "." + type)

while True: 

    if button_a.value:
        print("Count using mp3 files")
        type = "mp3"
        for n in range(0, maxnum + 1, countby):
            say_num(type, n)
    time.sleep(0.01)    
        
    if button_b.value:
        print("Count using wav files")
        type = "wav"
        for n in range(0, maxnum + 1, countby):
            say_num(type, n)
    time.sleep(0.01)   
    
    time.sleep(1) 
Attachments
num0-100-wav.7z
(609.67 KiB) Downloaded 5 times
num0-100-mp3.7z
(212.54 KiB) Downloaded 5 times

User avatar
jepler1
 
Posts: 50
Joined: Mon Oct 28, 2013 4:16 pm

Re: see-say-math-tables-mp3 using CPB + TFT Gizmo

Post by jepler1 »

Thanks for explaining what your code samples are trying to demonstrate, that's helpful. I will try your code on Monday. I have a CPB and TFT Gizmo. I do not have a crickit, so I'll be using the on-board speaker instead.

In my experience, there is always some additional hiss on the CBP's audio outputs that was not present on CPX. Unfortunately, I think this is unavoidable because the CPB does not have a true analog output; it uses PWM, and the way we use it gives only about 7 to 8 bits of audio fidelity, vs 12 bits on the CPX. In my experiments on a breadboard with an audio amplifier breakout, I found that adding an "RC filter" could help somewhat; but this is difficult or impossible in the case that you're bolting your CPB to a crickit or using the onboard speaker.

Pops at the start or end of playback could be due to a missing feature in CPB's audio which is to gradually ramp up from 0v to the middle of the audio scale at the start of playback, and back down to 0v at the end(?). We'd like to add this at some point, it just hasn't happened yet.

User avatar
V2man
 
Posts: 704
Joined: Mon Dec 03, 2018 12:38 am

Re: see-say-math-tables-mp3 using CPB + TFT Gizmo

Post by V2man »

@ jeplar Thanks for looking into this! You should really evaluate with an external speaker as well using the TFT Gizmo audio out to be fair. I need to find what JST connector fits on that output so I can try it.
Attached is a mp3 sample, say-clip-mp3.txt and a wav sample, say-clip-wav.txt in mp3 format to listen to. Download and rename the*.txt to *.mp3 to listen to the recordings.
Its better if you load the code and directories and listen on a CPB + TFT Gizmo or/and CPB + Crickit + external speaker setup.

The *.7z files when extracted create an extra level of directories.
The code looks for audio files directly under CIRCUITPY\num0-100-mp3 and CIRCUITPY\num0-100-wav directories not CIRCUITPY\num0-100-mp3\num0-100-mp3.....
I had to remove some libraries to make room for mp3 and wav files on CIRCUITPY.
[color][/b]
Here are the libraries I have installed now:
reduced_libraries.PNG
reduced_libraries.PNG (10.64 KiB) Viewed 156 times
Attachments
say-clip-wav.txt
(40.7 KiB) Downloaded 4 times
say-clip-mp3.txt
(44.31 KiB) Downloaded 4 times

User avatar
V2man
 
Posts: 704
Joined: Mon Dec 03, 2018 12:38 am

Re: see-say-math-tables-mp3 using CPB + TFT Gizmo

Post by V2man »

@ jepler Here's a version that turns on the CPB on board speaker on CPB + TFT Gizmo setup.
Now in addition to the background and clicking sounds I already mentioned it sounds like a tea kettle whistling on the stove.
I still recommend using an external speaker and turning off the on board speaker with spkrenable.value = False.
It works with the directories num0-100-mp3 and num0-100-wav that you already have.

Code: Select all

# see-say-count-mp3-wav.py
# Based wav version say-temp.py by Mike Barela
# for Adafruit Industries, MIT License

# import time
import board
# import microcontroller
# import analogio
import displayio
import terminalio
import audiomp3
import digitalio
from time import sleep
# from digitalio import DigitalInOut, Direction, Pull
from adafruit_display_text import label
# from adafruit_bitmap_font import bitmap_font
from adafruit_gizmo import tft_gizmo

try:
    from audiocore import WaveFile
except ImportError:
    from audioio import WaveFile

try:
    from audioio import AudioOut
except ImportError:
    try:
        from audiopwmio import PWMAudioOut as AudioOut
    except ImportError:
        pass  # not always supported by every board!

# Enable the speaker
spkrenable = digitalio.DigitalInOut(board.SPEAKER_ENABLE)
spkrenable.direction = digitalio.Direction.OUTPUT
spkrenable.value = True

display = tft_gizmo.TFT_Gizmo()
font = terminalio.FONT
text_group = displayio.Group(max_size=15, scale=4, x=0, y=0)
space_15 = "               "
# Create the text labels
text_area1 = label.Label(font, text=space_15, color=0x00FF00)
text_area2 = label.Label(font, text=space_15, color=0xFFFF00)
# append the labels to a group
text_group.append(text_area1)
text_group.append(text_area2)
display.show(text_group)
text_area1.x = 20
text_area1.y = 30
text_area2.x = 20
text_area2.y = 10

maxnum = 100
countby = 2
D1 = board.BUTTON_A
D2 = board.BUTTON_B

# Button A setup (Count using wav files)
button_a = digitalio.DigitalInOut(D1)
button_a.direction = digitalio.Direction.INPUT
button_a.pull = digitalio.Pull.DOWN
# Button B setup (Count using mp3 files)
button_b = digitalio.DigitalInOut(D2)
button_b.direction = digitalio.Direction.INPUT
button_b.pull = digitalio.Pull.DOWN

# Audio playback object and helper to play a full file

def play_mp3file(mp3file):
    mp3file = "/num0-100-mp3/" + mp3file
    print("Playing ", mp3file)
    with open(mp3file, "rb") as mp3:
        with AudioOut(board.SPEAKER) as audio:
            mp3 = audiomp3.MP3Decoder(mp3)
            audio.play(mp3)
            while audio.playing:
                pass

def play_wavfile(wavfile):
    wavfile = "/num0-100-wav/" + wavfile
    print("Playing ", wavfile)
    wave_file = open(wavfile, "rb")
    with WaveFile(wave_file) as wave:
        with AudioOut(board.SPEAKER) as audio:
            audio.play(wave)
            while audio.playing:
                pass

# Function should take an integer -299 to 299 and say it
# Assumes mp3 files are available for the numbers
def say_num(type, n):
    if type == "mp3":
        if n < 0:
            play_mp3file("negative." + type)
            n = - n
        if n >= 200:
            play_mp3file("200." + type)
            n = n - 200
        elif n >= 100:
            play_mp3file("100." + type)
            n = n - 100
        if (n >= 0 and n < 20) or n % 10 == 0:
            play_mp3file(str(n) + "." + type)
        else:
            play_mp3file(str(n // 10) + "0." + type)
            n = n - ((n // 10) * 10)
            play_mp3file(str(n) + "." + type)
    else:
        if n < 0:
            play_wavfile("negative." + type)
            n = - n
        if n >= 200:
            play_wavfile("200." + type)
            n = n - 200
        elif n >= 100:
            play_wavfile("100." + type)
            n = n - 100
        if (n >= 0 and n < 20) or n % 10 == 0:
            play_wavfile(str(n) + "." + type)
        else:
            play_wavfile(str(n // 10) + "0." + type)
            n = n - ((n // 10) * 10)
            play_wavfile(str(n) + "." + type)

while True:

    if button_a.value:
        text_area2.text = "MP3"
        print("Count using mp3 files")
        type = "mp3"
        for n in range(0, maxnum + 1, countby):
            text_area1.text = "%3.0f" % n
            say_num(type, n)
    sleep(0.01)

    if button_b.value:
        text_area2.text = "WAV"
        print("Count using wav files")
        type = "wav"
        for n in range(0, maxnum + 1, countby):
            text_area1.text = "%3.0f" % n
            say_num(type, n)
    sleep(0.01)

    sleep(1)
    text_area1.text = " "
    text_area2.text = " "

User avatar
V2man
 
Posts: 704
Joined: Mon Dec 03, 2018 12:38 am

Re: see-say-math-tables-mp3 using CPB + TFT Gizmo

Post by V2man »

@ jepler
If you were interested in comparing sound quality of mp3 versus wav you would start out with audio wav files. The audio mp3 files would then be made by converting the wav files you already made into mp3 format. The files I uploaded are from two different synthesized sources.

User avatar
V2man
 
Posts: 704
Joined: Mon Dec 03, 2018 12:38 am

Re: see-say-math-tables-mp3 using CPB + TFT Gizmo

Post by V2man »

Comment out the last sleep(1) statement if you want the pushbuttons on the CPB to be more responsive. Otherwise you have to hold the pushbutton for at least 1.02 seconds before the count will begin.

User avatar
jepler1
 
Posts: 50
Joined: Mon Oct 28, 2013 4:16 pm

Re: see-say-math-tables-mp3 using CPB + TFT Gizmo

Post by jepler1 »

Here's what I learned in my investigations. I used an nRF52840 Feather and a PAM8302A audio amplifier breakout.
  • I tested by playing 1 second of silence in a looping RawSample followed by 1 second of not playing. This eliminates any questions about whether it's due to the specific content of the audio sample, or the type of audio sample.
  • Indeed, there's no ramp up when a PWMAudioOut object is created. This can lead to a substantial pop.
  • When an audio sample stops playing, instead of retaining the last PWM value, the pin begins to "float". With my setup, a PAM8302A breakout with a RC filter on the input, and a 1 second pause between audio samples, this produces a small tick at the start and end of a sample. But with other setups, the pop could be more substantial.
  • Introducing a mixer object, which will keep the PWMAudioOut fed with audio samples, can substantially eliminate this second kind of pop.
  • I performed most tests with the "A-" pin of the PAM8302A breakout floating. However, connecting this pin to GND substantially increased the amplitude of the "ticks" to be full "pop"s. I think that grounding A- is "more like" the setup on the crickit, based on comparing the schematics.
  • The schematic of the PAM8302A breakout states "For the best power on/off pop performance, the amplifier should be set in the shutdown mode prior to power on/off operation (set SD low to power down the amp)". However, on the crickit, it is not possible for user code to control the SD pin of the amplifier.
Here's my testing code, non-mixer version:

Code: Select all

import array
import audiocore
import audiomixer
import audiopwmio
import board
import digitalio
import time

if hasattr(board, 'SPEAKER_ENABLE'):
    se = digitalio.DigitalInOut(board.SPEAKER_ENABLE)
    se.switch_to_output(True)
s = audiopwmio.PWMAudioOut(board.A0)

data = array.array('b', [0]) * 256
sample = audiocore.RawSample(data)
mx = audiomixer.Mixer(channel_count=1, bits_per_sample=8, samples_signed=True, sample_rate=8000)

while True:
    s.play(sample, loop=True)
    time.sleep(1)
    s.stop()
    time.sleep(1)
Here's what I plan to do, though due to other commitments I won't start right away:
  • First, add ramp up and down code. Together with the mixer workaround, this should allow elimination of pops
  • Second, add code to continue driving the PWM output with "50%" when not playing a sample. This will allow removal of the mixer workaround

User avatar
V2man
 
Posts: 704
Joined: Mon Dec 03, 2018 12:38 am

Re: see-say-math-tables-mp3 using CPB + TFT Gizmo

Post by V2man »

@ jepler Thanks I look forward to trying your fix.
Would it not have been better to have a differential audio interface through the audio process?
Did you tryout the last code I submitted on a CPB + TFT Gizmo setup?
Last edited by V2man on Mon Feb 03, 2020 2:41 pm, edited 1 time in total.

User avatar
V2man
 
Posts: 704
Joined: Mon Dec 03, 2018 12:38 am

Re: see-say-math-tables-mp3 using CPB + TFT Gizmo

Post by V2man »

@ jepler
After substituting SPEAKER for A0 in this statement, s = audiopwmio.PWMAudioOut(board.A0) of your code. I saved the code to my CPB + TFT Gizmo + on board speaker setup. I can hear a repetitive pattern of a tea kettle whistle, click sound followed by silence. This is similar but much worse than what I am hearing with my code of reciting numbers.
Your code demonstrates the problem very well and removes audio format from the question!

User avatar
V2man
 
Posts: 704
Joined: Mon Dec 03, 2018 12:38 am

Re: see-say-math-tables-mp3 using CPB + TFT Gizmo

Post by V2man »

@ jepler I don't understand how the noise can be reduced using the audio mixer.
How would I modify my mp3 code below using a mixer to get rid of noise and clicks?

Code: Select all

# cpb_mixer_mp3.py
import board
import digitalio
import audiocore
import audiomixer
import audiomp3
from time import sleep

try:
    from audiocore import WaveFile
except ImportError:
    from audioio import WaveFile

try:
    from audioio import AudioOut
except ImportError:
    try:
        from audiopwmio import PWMAudioOut as AudioOut
    except ImportError:
        pass  # not always supported by every board!

# Required for CircuitPlayground Express
speaker_enable = digitalio.DigitalInOut(board.SPEAKER_ENABLE)
speaker_enable.switch_to_output(value=True)
mp3file1 = "/num0-100-mp3/8.mp3"
mp3file2 = "/num0-100-mp3/100.mp3"
data1 = open(mp3file1, "rb")
data2 = open(mp3file2, "rb")
mp31 = audiomp3.MP3Decoder(data1)
mp32 = audiomp3.MP3Decoder(data2)

mixer = audiomixer.Mixer(voice_count=2, sample_rate=22050, channel_count=1,
                         bits_per_sample=16, samples_signed=True)
a = AudioOut(board.SPEAKER)

while True:
    print("playing")
    a.play(mixer)
    print(mp3file1)
    mixer.voice[0].play(mp31)
    while mixer.playing:
        sleep(2)
        print(mp3file2)
        mixer.voice[1].play(mp32)
        sleep(2)
    print("stopped")
    print(" ")
My mixer = audiomixer.Mixer(voice_count=2, sample_rate=22050, channel_count=1, bits_per_sample=16, samples_signed=True) is set to match my number mp3 files. Most mp3 music requires a sample_rate = 44100.
In your raw sample example your mx = audiomixer.Mixer(channel_count=1, bits_per_sample=8, samples_signed=True, sample_rate=8000)
Is there a way to make a data array of 16 bit zeroes?
CircuitPython will not allow data = array.array('b', [0]) * 65536.

Locked
Please be positive and constructive with your questions and comments.

Return to “Adafruit CircuitPython”