view pcm/pcm_tx.c @ 7:ca351324187a

pcm: implement Tx on the E1 timeslot
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 23 Jun 2024 19:21:00 +0000
parents
children 70aa8cbdbde9
line wrap: on
line source

/*
 * In this module we implement PCM Tx toward the TRAU.
 */

#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <osmocom/core/select.h>

#include "globals.h"

static const uint8_t dmw_alaw[8] =
	{0x34, 0x21, 0x21, 0x34, 0xB4, 0xA1, 0xA1, 0xB4};

static uint8_t pcm_fill_octet = 0x54;
static bool dmw_active;
static uint8_t *play_buffer;
static unsigned play_buf_nframes, play_buf_ptr;

static void fill_with_dmw(uint8_t *buf)
{
	unsigned n;

	for (n = 0; n < 20; n++) {
		memcpy(buf, dmw_alaw, 8);
		buf += 8;
	}
}

static void fill_with_play(uint8_t *outbuf)
{
	memcpy(outbuf, play_buffer + play_buf_ptr * 160, 160);
	play_buf_ptr++;
	if (play_buf_ptr < play_buf_nframes)
		return;
	free(play_buffer);
	play_buffer = 0;
	printf("file play finished\n");
}

void transmit_pcm_20ms(void)
{
	uint8_t buf[160];

	if (play_buffer)
		fill_with_play(buf);
	else if (dmw_active)
		fill_with_dmw(buf);
	else
		memset(buf, pcm_fill_octet, 160);
	write(ts_fd, buf, 160);
}