Speech in C
Writing C programs for speech
on the SC126 (RC2014 Compatible) with a SPO256-AL2 Speech Synthesis Chip
I spent some time yesterday working with ChatGPT to assist me in writing a moderately complex (based on my personal experience) C program. The program needed to be a port of the following BASIC program:
10 DATA 27,7,45,15,53,3,46,51,45,1,21,3 20 LET LE=12
30 DIM XX(LE)
40 FOR Y = 1 TO LE
50 READ XX (Y)
60 NEXT Y
70 FOR Z=1 TO LE
80 IF (INP(31) AND 2) = 2 THEN GOTO 100
90 GOTO 80
100 OUT 31,XX(Z) 110 NEXT Z
This program sends the correct allophones to the speech synthesizer to make it say “Hello, World”.
I was able to get a simple C program written, but I was having difficulty getting it to use a dynamic linked list so I could use a variable length int array for the data. It needed to use any number of data values and adjust itself automatically to the size of the array.
So, after a littl time with ChatGPT and Visual Studio Code, I was able to get it working. Here is the final C code:
#include <z180.h>
#include <stdlib.h>
#include <stdio.h>
// Define a node structure for the linked list
struct Node {
int data;
struct Node* next;
};
void addNode(struct Node** head, struct Node** current, int value) {
// Create a new node
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
// Check if memory allocation is successful
if (newNode == NULL) {
// Handle memory allocation failure
printf("There was an error with memory allocation");
exit(EXIT_FAILURE);
}
// Set the data for the new node
newNode->data = value;
// Link the new node to the list
newNode->next = NULL;
if (*head == NULL) {
// If the list is empty, set the new node as the head
*head = newNode;
*current = *head;
} else {
// Otherwise, add the new node to the end of the list
(*current)->next = newNode;
*current = newNode;
}
}
int main() {
// Create a linked list
struct Node* head = NULL;
struct Node* current = NULL;
// Define which allophones we're going to use
int input[] = {27, 7, 45, 15, 53, 3, 46, 51, 45, 1, 21, 3};
int inputSize = sizeof(input) / sizeof(input[0]);
// Add nodes to the linked list
for (int i = 0; i < inputSize; ++i) {
addNode(&head, ¤t, input[i]);
}
// Traverse the linked list and perform operations
struct Node* iterator = head;
while (iterator != NULL) {
// Perform operations using iterator->data
while ((z180_inp(31) & 0x02) != 0x02) {
// Wait for bit 1 of port 31 to become 1
}
z180_outp(31, iterator->data);
// Move to the next node in the list
iterator = iterator->next;
}
// Free the memory allocated for the linked list
iterator = head;
while (iterator != NULL) {
struct Node* temp = iterator;
iterator = iterator->next;
free(temp);
}
return 0;
}
As you can see, it is using a header file from the z180 CPU. This is the processor that I have running in my retro computer. My computer is a kit from Stephen Cousins (SC126 on Tindie). He makes other kits that you can solder together to have a working homebrew computer.
ChatGPT was able to create the code specifically for the z88dk compiler. It worked after some slight modifications to the inp
and outp
functions to match them to my z180 header.