speech on Raspberry Pi: espeak-ng

Audio can be a bit dismal on a Raspberry Pi. Once you get a configuration that works, sometimes you’re not sure how you got there and you’ll do anything to keep that arcane setup going. It’s better than it was.

Speech synthesis or TTS adds an extra layer for potential failure. One of the popular Linux TTS systems, eSpeak, hasn’t seen much development in almost a decade and seems to only work through workarounds and hand-waving.

Thankfully, there’s a fork of eSpeak that is maintained: espeak-ng. Better yet, it’s packaged with Raspberry Pi OS and can be installed quite easily:

sudo apt install espeak-ng espeak-ng-data libespeak-ng-dev

In my simple tests, it output everything I expected of it.

eSpeak had a Python module that kinda worked, but espeak-ng’s is much more ambitious, and (mostly) does what it sets out to do. You can install it like this:

sudo pip3 install py-espeak-ng

py-espeak-ng has some documentation, but it’s still got some trial and error in getting it to work. The biggest issue that held me up was that the module needs to be initialized with a voice that espeak-ng already knows about. If you don’t specify a voice, or specify one that the system doesn’t know about, you won’t get any errors — but you won’t get any output, either.

Here’s a small Python example that you’ll probably want to try with no-one else within earshot. It repeats the same English phrase (a favourite of elocution teachers) in every English regional language that espeak-ng knows about. In addition, since I’m a dictionary nerd, it outputs phonetics too.

#!/usr/bin/python3
# -*- coding: utf-8 -*-
# an espeakng elocution lesson from scruss, 2020-07
#     I suffered this at school, now you get to as well!
# You will need to:
#     sudo apt install espeak-ng espeak-ng-data libespeak-ng-dev
#     sudo pip3 install py-espeak-ng

from espeakng import ESpeakNG
from time import sleep

# you have to initialize with a voice that exists
#   `espeak-ng --voices=en` will list English ones
esng = ESpeakNG(voice='en-gb')
esng.pitch = 32
esng.speed = 150

phrase = "Father's car is a Jaguar and pa drives rather fast. "\
    "Castles, farms and draughty barns, all go charging past."
print(phrase)
print()

for voice in esng.voices:
    if voice['language'].startswith('en-'):
        print('Using voice:', voice['language'],
              'for', voice['voice_name'], '-')
        esng.voice = voice['language']
        ipa = esng.g2p(phrase, ipa=2)
        print(voice['language'], 'phonetics:', ipa)
        esng.say(phrase, sync=True)
        print()
        sleep(0.1)

Be thankful you can’t hear the output. The IPA output, however, is a thing of beauty:

./espeakNG_test.py
Father's car is a Jaguar and pa drives rather fast. Castles, farms and draughty barns, all go charging past.

Using voice: en-029 for English_(Caribbean) -
en-029 phonetics: fˈɑːdaz kˈɑ͡əɹ ɪz a d͡ʒˈaɡwɑ͡ə and pˈɑː dɹˈa͡ɪvz ɹˈɑːda fˈa͡astkˈa͡asɛlzfˈɑ͡əmz and dɹˈa͡afti bˈɑ͡ənzˈɔːl ɡˌo͡ʊ t͡ʃˈɑ͡əd͡ʒɪn pˈa͡ast

Using voice: en-gb for English_(Great_Britain) -
en-gb phonetics: fˈɑːðəz kˈɑːɹ ɪz ɐ d͡ʒˈaɡwɑː and pˈɑː dɹˈa͡ɪvz ɹˈɑːðə fˈastkˈasə͡lzfˈɑːmz and dɹˈafti bˈɑːnzˈɔːl ɡˌə͡ʊ t͡ʃˈɑːd͡ʒɪŋ pˈast

Using voice: en-gb-scotland for English_(Scotland) -
en-gb-scotland phonetics: fˈa:ðɜz kˈaːr ɪz ɐ d͡ʒˈaɡwaːr and pˈa: drˈa͡ɪvz rˈa:ðɜ fˈa:stkˈa:sə͡lzfˈaːrmz and drˈa:fte bˈaːrnzˈɔːl ɡˌoː t͡ʃˈaːrd͡ʒɪŋ pˈa:st
 …

The Mandelbrot Set, before Mandelbrot

How many CPU hours did I burn in the early 1990s rendering bits of the Mandelbrot Set? A lot, mainly because I was doing it in BASIC on an unexpanded 8 MHz Commodore Amiga A500. The image below that Fraqtive rendered in almost no time would have taken me days:

the squashed bug that started it all: the Mandelbrot set

But it turns out that the first rendering of what we now call the Mandelbrot set wasn’t produced by Benoit Mandelbrot, but by Brooks & Matelski a year or two earlier:

text plot of the Mandelbrot set with points inside set marked with asterisks
figure 2 (original caption “The set of C’s such that f(z) = z² + C has a stable periodic orbit“) from Brooks, Robert, and J. Peter Matelski. “The dynamics of 2-generator subgroups of PSL (2, C).Riemann surfaces and related topics: Proceedings of the 1978 Stony Brook Conference, Ann. of Math. Stud. Vol. 97. 1981.

What I’ve done – and mostly thanks to tweaks by commenter nobody below – is create period-appropriate code to reproduce that graphic. Since the paper was presented in 1978, there’s a fair chance that the authors had access to a machine running FORTRAN-77 or a near equivalent. FORTRAN’s particularly good for this:

  • it has a built-in COMPLEX type that extends regular mathematical functions;
  • it has just good enough string handling to output a line of spaces/asterisks. I would not have wanted to write this in FORTRAN-66, as that language had no string manipulation facilities at all.

So here’s the code. It should compile on any Fortran compiler:

      PROGRAM BRKMTF
!     GENERATE FIGURE FROM BROOKS-MATELSKI PAPER C.1978
!     THAT EVENTUALLY BECAME KNOWN AS THE MANDELBROT SET
!     - SCRUSS, 2022-05
!     REF: BROOKS, ROBERT, AND J. PETER MATELSKI.
!     "THE DYNAMICS OF 2-GENERATOR SUBGROUPS OF PSL (2, C)."
!     RIEMANN SURFACES AND RELATED TOPICS: PROCEEDINGS OF THE
!     1978 STONY BROOK CONFERENCE,
!     ANN. OF MATH. STUD. VOL. 97. 1981: FIG. 2, P. 81

      REAL MAP, CR, CI
      INTEGER I, J, K, M, ROWS, COLS, MAXIT
      COMPLEX C, Z
      PARAMETER (ROWS=31, COLS=71, MAXIT=200)
      CHARACTER*80 OUT
      CHARACTER CH*1

      DO J=1,ROWS
         CI=MAP(REAL(J), 1.0, REAL(ROWS), -0.8715, 0.8715)
         DO I=1,COLS
            CR=MAP(REAL(I), 1.0, REAL(COLS), -1.975, 0.475)
            C=CMPLX(CR, CI)
            Z=CMPLX(0.0, 0.0)
            CH='*'
            DO 100, K=1,MAXIT
               Z = Z**2 + C
               IF (ABS(Z) .GT. 2) THEN
                  CH=' '
                  GO TO 101
               END IF
 100        CONTINUE
 101        OUT(I:I)=CH
         END DO
         WRITE(*,*)OUT
      END DO
      END

      REAL FUNCTION MAP(X, XMIN, XMAX, YMIN, YMAX)
      REAL X, XMIN, XMAX, YMIN, YMAX
      MAP = YMIN + (YMAX - YMIN) * ((X - XMIN) / (XMAX - XMIN))
      END

The results are spot-on:

mandelbrot set, rendered in asterisks from BRKMTF.F
party like it’s ’78

Maybe Brooks & Matelski had access to an Apple II and wrote something in BASIC? I could be entirely period-accurate and write something in PDP-8 BASIC on my SBC6120, but not today.

It really is much easier using a language with complex number support when working with the Mandelbrot set. Here’s the same program in Python3, which bears more of a resemblance to FORTRAN-77 than it might admit:

#!/usr/bin/python3
# brkmtf - Brooks-Matelski proto ASCII Mandelbrot set - scruss, 2022-05
# -*- coding: utf-8 -*-

def valmap(value, istart, istop, ostart, ostop):
    return ostart + (ostop - ostart) * ((value - istart) / (istop - istart))

rows = 31
cols = 71
maxit = 200

for y in range(rows):
    ci = valmap(float(y + 1), 1.0, float(rows), -0.8715, 0.8715)
    for x in range(cols):
        cr = valmap(float(x + 1), 1.0, float(cols), -1.975, 0.475)
        c = complex(cr, ci)
        z = complex(0.0, 0.0)
        ch = '*'
        for k in range(maxit):
            z = z**2 + c
            if (abs(z) > 2.0):
                ch = ' '
                break
        print(ch, end='')
    print()

I found out about the Brooks-Matelski paper from this article: Who Discovered the Mandelbrot Set? – Scientific American. It’s none too complimentary towards Benoit Mandelbrot.

Update: see the comment from one of the authors of the original paper below.

MicroPython on the BrainPad Classic/BP2

GHI Electronics BrainPad Classic/BP2 electronics project board on a tasteful faux-leopard background. Small OLED screen is showing "SCRUSS 2020"
GHI Electronics BrainPad Classic/BP2 in 3d printed case

I’ve extended the MicroPython examples for the BrainPad Classic so that all of the devices work: scruss/brainpad-micropython: Micropython examples for the BrainPad Classic (BP2) from GHI Electronics.

The ones that already worked in the original examples repo are:

  • buttons
  • accelerometer
  • LEDs
  • light sensor
  • OLED screen

I’ve added:

  • temperature sensor: although my calibration may be a bit off on the MCP9701a used on the board
  • timer blink example: STM32 Timers are cool and we should use them
  • PWM RGB LED example: floating-point silliness with HSV(ish) Colour Wheel in Python
  • buzzer: simple tones plus tunes (in RTTTL) via dhylands / upy-rtttl
  • servos: I may have forgotten to put the example in there, but the standard Servo(1) code should work.

Yes, it would be nice to have a slick unified library like the BBC micro:bit does. For later, though.

Other resources:

groundhog!

nomming from our deck

Published
Categorised as photo

terminal colour silliness with Python

terminal text in rainbows

Using ansicolors:

#!/usr/bin/python3
# -*- coding: utf-8 -*-
# colourshen.py - stdin to rainbow stdout
# scruss, 2020-06

from colors import *            # see https://pypi.org/project/ansicolors/
import sys
wheel_pos = 0


def cos_wheel(pos):
    # Input a value 0 to 255 to get a colour value.
    # scruss (Stewart Russell) - 2019-03 - CC-BY-SA
    from math import cos, pi
    if pos < 0:
        return (0, 0, 0)
    pos %= 256
    pos /= 255.0
    return (int(255 * (1 + cos(pos * 2 * pi)) / 2),
            int(255 * (1 + cos((pos - 1 / 3.0) * 2 * pi)) / 2),
            int(255 * (1 + cos((pos - 2 / 3.0) * 2 * pi)) / 2))


def hex_wheel(pos):
    rgb = cos_wheel(pos)
    return('#%02x%02x%02x' % rgb)


def wheel_print(s):
    global wheel_pos
    incr = int(256/(1+len(s)))-1
    if incr < 1:
        incr = 1
    for c in s:
        print(color(c, fg=hex_wheel(wheel_pos)), end='')
        wheel_pos = (wheel_pos+incr) % 256
    print()


for txt in sys.stdin:
    wheel_print(txt.rstrip())

(fixed a very obvious ahem! in the code, hope no-one noticed …)