Skip to content

Commit

Permalink
Added nioTSO's EA MicroTalk encoder to the project
Browse files Browse the repository at this point in the history
  • Loading branch information
Tony Bark committed May 7, 2024
1 parent 4bee18c commit 41be518
Show file tree
Hide file tree
Showing 14 changed files with 2,374 additions and 4 deletions.
6 changes: 2 additions & 4 deletions library/extra/d20/d20.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
/** @file d20.h
* @brief Implements Dungeons & Dragons style dice in C
*
* d20.h is a reimplementation of https://github.com/opensourcedoc/d20-c,
* but following the principles of being a single header/file library with
* a minimal API
*
* d20.h is a reimplementation of https://github.com/opensourcedoc/d20-c,but following the principles of being a single header/file library with a minimal API.
* @source https://github.com/adamml/d20
* @author adamml
* @date 2022-11-07
*/
Expand Down
49 changes: 49 additions & 0 deletions library/tools/utkencode/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
## EA MicroTalk

EA MicroTalk (also UTalk or UTK) is a linear-predictive speech codec used in
various games by Electronic Arts. The earliest known game to use it is
Beasts & Bumpkins (1997). The codec has a bandwidth of 11.025kHz (sampling rate
22.05kHz) and frame size of 20ms (432 samples) and only supports mono. It is
typically encoded at 32 kbit/s.

Docs: http://wiki.niotso.org/UTK

In this repository, I have created a set of open source (public domain
via the UNLICENSE) MicroTalk decoders/encoders.

* Use utkdecode to decode Maxis UTK (The Sims Online, SimCity 4).
* Use utkdecode-bnb to decode PT/M10 (Beasts & Bumpkins).
* Use utkdecode-fifa to decode FIFA 2001/2002 (PS2) speech samples. This tool
supports regular MicroTalk and MicroTalk Revision 3
[SCxl files](https://wiki.multimedia.cx/index.php/Electronic_Arts_SCxl).(*)
* Use utkencode to encode Maxis UTK. (This is the simplest container format and
is currently the only one supported for encoding.)

(*) I wasn't able to find any real-world MicroTalk Rev. 3 samples in any games.
However, you can transcode a FIFA MicroTalk Rev. 2 file to Rev. 3 using
[EA's Sound eXchange tool](https://wiki.multimedia.cx/index.php/Electronic_Arts_Sound_eXchange)
(`sx -mt_blk input.dat -=output.dat`).

## Compiling

```
gcc -Wall -Wextra -Wno-unused-function -ansi -pedantic -O2 -ffast-math -fwhole-program -g0 -s -static-libgcc -o utkdecode utkdecode.c
gcc -Wall -Wextra -Wno-unused-function -ansi -pedantic -O2 -ffast-math -fwhole-program -g0 -s -static-libgcc -o utkdecode-fifa utkdecode-fifa.c
gcc -Wall -Wextra -Wno-unused-function -ansi -pedantic -O2 -ffast-math -fwhole-program -g0 -s -static-libgcc -o utkdecode-bnb utkdecode-bnb.c
gcc -Wall -Wextra -Wno-unused-function -ansi -pedantic -O2 -ffast-math -fwhole-program -g0 -s -static-libgcc -o utkencode utkencode.c
```

## How the encoder works

The encoder for now is very simple. It does LPC analysis using the Levinson
algorithm and transmits the entire excitation signal explicitly. Compression is
achieved by choosing a large fixed codebook gain, such that each excitation
sample has a large (coarse) quantization step size. Error is minimized in the
excitation domain, and the quality is somewhat poor for bitrates below about
48 kbit/s.

However, MicroTalk is a multi-pulse codec (it is cheap to code long runs of
zeros in the excitation signal). Hence, a much better design (and indeed the
standard practice for multi-pulse speech codecs) is to search for the positions
and amplitudes of n pulses such that error is minimized in the output domain
(or the perceptually weighted domain). This new encoder is still in the works.
24 changes: 24 additions & 0 deletions library/tools/utkencode/UNLICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.

In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to <http://unlicense.org/>
77 changes: 77 additions & 0 deletions library/tools/utkencode/eachunk.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
typedef struct EAChunk {
uint32_t type;
uint8_t *start;
uint8_t *ptr;
uint8_t *end;
} EAChunk;

static void chunk_read_bytes(EAChunk *chunk, uint8_t *dest, size_t size)
{
size_t bytes_remaining = chunk->end - chunk->ptr;

if (bytes_remaining < size) {
fprintf(stderr, "error: unexpected end of chunk\n");
exit(EXIT_FAILURE);
}

memcpy(dest, chunk->ptr, size);
chunk->ptr += size;
}

static uint32_t chunk_read_u32(EAChunk *chunk)
{
uint8_t dest[4];
chunk_read_bytes(chunk, dest, sizeof(dest));
return dest[0] | (dest[1] << 8) | (dest[2] << 16) | (dest[3] << 24);
}

static uint32_t chunk_read_u8(EAChunk *chunk)
{
uint8_t dest;
chunk_read_bytes(chunk, &dest, sizeof(dest));
return dest;
}

static uint32_t chunk_read_var_int(EAChunk *chunk)
{
uint8_t dest[4];
uint8_t size = chunk_read_u8(chunk);

if (size > 4) {
fprintf(stderr, "error: invalid varint size %u\n", (unsigned)size);
exit(EXIT_FAILURE);
}

chunk_read_bytes(chunk, dest, size);

/* read a big-endian integer of variable length */
switch (size) {
case 1: return dest[0];
case 2: return (dest[0]<<8) | dest[1];
case 3: return (dest[0]<<16) | (dest[1] << 8) | dest[2];
case 4: return (dest[0]<<24) | (dest[1] << 16) | (dest[2] << 8) | dest[3];
default: return 0;
}
}

static EAChunk *read_chunk(FILE *fp)
{
uint32_t size;
static EAChunk chunk;
static uint8_t buffer[4096];

chunk.type = read_u32(fp);

size = read_u32(fp);
if (size < 8 || size-8 > sizeof(buffer)) {
fprintf(stderr, "error: invalid chunk size %u\n", (unsigned)size);
exit(EXIT_FAILURE);
}

size -= 8;
read_bytes(fp, buffer, size);
chunk.start = chunk.ptr = buffer;
chunk.end = buffer+size;

return &chunk;
}
78 changes: 78 additions & 0 deletions library/tools/utkencode/io.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

static void read_bytes(FILE *fp, uint8_t *dest, size_t size)
{
size_t bytes_copied;

if (!size)
return;

bytes_copied = fread(dest, 1, size, fp);
if (bytes_copied < size) {
if (ferror(fp))
fprintf(stderr, "error: fread failed: %s\n", strerror(errno));
else
fprintf(stderr, "error: unexpected end of file\n");

exit(EXIT_FAILURE);
}
}

static uint32_t read_u32(FILE *fp)
{
uint8_t dest[4];
read_bytes(fp, dest, sizeof(dest));
return dest[0] | (dest[1] << 8) | (dest[2] << 16) | (dest[3] << 24);
}

static uint16_t read_u16(FILE *fp)
{
uint8_t dest[2];
read_bytes(fp, dest, sizeof(dest));
return dest[0] | (dest[1] << 8);
}

static uint16_t read_u8(FILE *fp)
{
uint8_t dest;
read_bytes(fp, &dest, sizeof(dest));
return dest;
}

static void write_bytes(FILE *fp, const uint8_t *dest, size_t size)
{
if (!size)
return;

if (fwrite(dest, 1, size, fp) != size) {
fprintf(stderr, "error: fwrite failed: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
}

static void write_u32(FILE *fp, uint32_t x)
{
uint8_t dest[4];
dest[0] = (uint8_t)x;
dest[1] = (uint8_t)(x>>8);
dest[2] = (uint8_t)(x>>16);
dest[3] = (uint8_t)(x>>24);
write_bytes(fp, dest, sizeof(dest));
}

static void write_u16(FILE *fp, uint16_t x)
{
uint8_t dest[2];
dest[0] = (uint8_t)x;
dest[1] = (uint8_t)(x>>8);
write_bytes(fp, dest, sizeof(dest));
}

static void write_u8(FILE *fp, uint8_t x)
{
write_bytes(fp, &x, sizeof(x));
}
Binary file added library/tools/utkencode/samples/DS1.M10
Binary file not shown.
Binary file added library/tools/utkencode/samples/fifa2001-mt5.dat
Binary file not shown.
Binary file added library/tools/utkencode/samples/fifa2001.dat
Binary file not shown.
Binary file added library/tools/utkencode/samples/male.utk
Binary file not shown.
Loading

0 comments on commit 41be518

Please sign in to comment.