1
0

An initial commit. :)

This commit is contained in:
Alexander Andreev 2024-03-21 04:10:02 +04:00
commit 64176055d0
Signed by: Arav
GPG Key ID: 25969B23DCB5CA34
6 changed files with 171 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
/obj
tetatet

25
Makefile Normal file
View File

@ -0,0 +1,25 @@
TARGET = tetatet
CC = cc
DESTDIR :=
DESTDIR = /
PREFIX := /usr/local
SRC_DIR := src
OBJ_DIR := obj
SRCs := $(wildcard $(SRC_DIR)/*.c)
OBJs := $(SRCs:$(SRC_DIR)/%.c=$(OBJ_DIR)/%.o)
CFLAGS = --std=c99 -O3 -Wall -Werror -Wextra -pedantic
LDFLAGS += -lportaudio -lopus -lsodium
$(TARGET): $(OBJs)
$(CC) $(CFLAGS) $(LDFLAGS) $^ -o $@
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c | $(OBJ_DIR)
$(CC) $(CFLAGS) $(LDFLAGS) -c $< -o $@
$(OBJ_DIR):
mkdir -p $@

93
src/audio.c Normal file
View File

@ -0,0 +1,93 @@
#include "audio.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int audio_default_callback(const void *input, void *output,
unsigned long frameCount, const PaStreamCallbackTimeInfo *timeInfo,
PaStreamCallbackFlags statusFlags, void *userData) {
(void)timeInfo;
(void)statusFlags;
audio_t *aud = (audio_t *)userData;
int encLen = opus_encode_float(aud->opusEnc, (float *)input, frameCount, aud->opusBuf, aud->opusBufSz);
(void)encLen;
int frameSize = opus_decode_float(aud->opusDec, aud->opusBuf, aud->opusBufSz, (float *)output, frameCount, 0);
(void)frameSize;
// printf("%i %i | %li %li\n", encLen, frameSize, frameCount, aud->opusBufSz);
return paContinue;
}
int audio_init_default(audio_t *aud, int channels, int sampleRate, int frameSize, PaStreamCallback callback) {
PaError paErr;
int opusErr;
if ((paErr = Pa_Initialize()) != paNoError) {
fprintf(stderr, "Cannot initialise PortAudio: %s\n", Pa_GetErrorText(paErr));
return -1;
}
aud->opusBufSz = frameSize * channels;
aud->opusBuf = (uint8_t *)malloc(aud->opusBufSz * sizeof(uint8_t));
aud->opusEnc = opus_encoder_create(sampleRate, channels, OPUS_APPLICATION_VOIP, &opusErr);
if (opusErr != OPUS_OK) {
fprintf(stderr, "An Opus encoder cannot be created: %s\n", opus_strerror(opusErr));
return -1;
}
aud->opusDec = opus_decoder_create(sampleRate, channels, &opusErr);
if (opusErr != OPUS_OK) {
fprintf(stderr, "An Opus decoder cannot be created: %s\n", opus_strerror(opusErr));
return -1;
}
if ((paErr = Pa_OpenDefaultStream(&aud->stream, channels, channels, paFloat32,
sampleRate, frameSize, callback, aud)) != paNoError) {
fprintf(stderr, "Cannot open a PortAudio stream: %s\n", Pa_GetErrorText(paErr));
return -1;
}
if ((paErr = Pa_StartStream(aud->stream)) != paNoError) {
fprintf(stderr, "Cannot start a PortAudio stream: %s\n", Pa_GetErrorText(paErr));
return -1;
}
return 0;
}
int audio_destroy(audio_t *aud) {
PaError paErr;
if (aud->stream) {
if ((paErr = Pa_StopStream(aud->stream)) != paNoError) {
fprintf(stderr, "Cannot stop a PortAudio stream: %s\n", Pa_GetErrorText(paErr));
return -1;
}
if ((paErr = Pa_CloseStream(aud->stream)) != paNoError) {
fprintf(stderr, "Cannot close a PortAudio stream: %s\n", Pa_GetErrorText(paErr));
return -1;
}
}
if ((paErr = Pa_Terminate()) != paNoError) {
fprintf(stderr, "Cannot terminate PortAudio: %s\n", Pa_GetErrorText(paErr));
return -1;
}
if (aud->opusEnc)
opus_encoder_destroy(aud->opusEnc);
if (aud->opusDec)
opus_decoder_destroy(aud->opusDec);
if (aud->opusBuf)
free(aud->opusBuf);
return 0;
}

25
src/audio.h Normal file
View File

@ -0,0 +1,25 @@
#ifndef _AUDIO_H_
#define _AUDIO_H_
#include <stdint.h>
#include <stdlib.h>
#include <opus/opus.h>
#include <portaudio.h>
typedef struct audio_t {
PaStream *stream;
OpusEncoder *opusEnc;
OpusDecoder *opusDec;
uint8_t *opusBuf;
size_t opusBufSz;
} audio_t;
int audio_default_callback(const void *input, void *output,
unsigned long frameCount, const PaStreamCallbackTimeInfo *timeInfo,
PaStreamCallbackFlags statusFlags, void *userData);
int audio_init_default(audio_t *aud, int channels, int sampleRate, int frameSize, PaStreamCallback *callback);
int audio_destroy(audio_t *aud);
#endif /* _AUDIO_H_ */

6
src/crypto.h Normal file
View File

@ -0,0 +1,6 @@
#ifndef _CRYPTO_H_
#define _CRYPTO_H_
#endif /* _CRYPTO_H_ */

20
src/main.c Normal file
View File

@ -0,0 +1,20 @@
#include <stdio.h>
#include "audio.h"
int main(int argc, char **argv) {
(void)argv;
(void)argc;
audio_t aud;
audio_init_default(&aud, 1, 48000, 480, audio_default_callback);
printf("Listening... ");
getchar();
printf("Done!\n");
audio_destroy(&aud);
return 0;
}