Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- //audio generating and playback functions
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdint.h>
- #include <string.h>
- #include <portaudio.h>
- #include "ukaelTypedefC.h"
- #include "ukaelWaveC.h"
- // Callback function that is called by PortAudio to fill the output buffer
- static int audioCallback(
- const void* inputBuffer,
- void* outputBuffer,
- unsigned long framesPerBuffer,
- const PaStreamCallbackTimeInfo* timeInfo,
- PaStreamCallbackFlags statusFlags,
- void* userData
- ){
- AudioData* audioData = (AudioData*)userData;
- uint8_t* out = (uint8_t*)outputBuffer;
- size_t remainingData = audioData->size - audioData->position;
- size_t numFrames = framesPerBuffer;
- // If there is not enough audio data remaining, fill the buffer with silence
- if (remainingData < framesPerBuffer) {
- numFrames = remainingData;
- for (size_t i = 0; i < numFrames; i++) {
- out[i] = DEFAULT_AMPLITUDE; // DEFAULT_AMPLITUDE represents silence for unsigned 8-bit audio
- }
- }
- // Copy the audio data to the output buffer
- for (size_t i = 0; i < numFrames; i++) {
- out[i] = audioData->data[audioData->position++];
- }
- return paContinue;
- }
- //file IO
- void audioDataToBin(const char* filename, const AudioData* samples) {
- if(!(samples->size)){
- printf("Nothing to write to file.\n");
- }
- // Open the file in binary mode for writing
- FILE* file = fopen(filename, "wb");
- if (file == NULL) {
- printf("Failed to open the file for writing.\n");
- return;
- }
- // Write the samples data to the file
- size_t bytesWritten = fwrite(samples->data, sizeof(uint8_t), samples->size, file);
- if (bytesWritten != samples->size) {
- printf("Failed to write the samples data to the file.\n");
- }
- // Close the file
- uint8_t closeResult = fclose(file);
- if (closeResult != 0) {
- printf("Failed to close the file.\n");
- }
- }
- //Memory functions
- //Allocate and set to DEFAULT_AMPLITUDE
- void sampleAlloc(AudioData* sampleData) {
- // Allocate memory for channel->data
- sampleData->data = (uint8_t*)malloc((sampleData->size)*sizeof(uint8_t));
- if (sampleData->data == NULL) {
- // Failed to allocate memory
- printf("Failed to allocate memory for sampleData->data\n");
- return;
- }
- // memset to DEFAULT_AMPLITUDE
- memset((void*)sampleData->data, DEFAULT_AMPLITUDE, sampleData->size);
- if (sampleData->data == NULL) {
- printf("Failed set sampleData to silent.\n");
- return;
- }
- }
- //Free AudioData .data
- void sampleFree(AudioData* sampleData) {
- free((void*)sampleData->data);
- }
- // Copy and allocate AudioData to destin from source
- void sampleCopy(AudioData* destin, const AudioData* source) {
- if((destin->size)!=(source->size)){
- printf("Failed to copy samples, different size.\n");
- destin->size=0;
- return;
- }
- memcpy((void*)destin->data, (void*)source->data, source->size);
- if (destin->data == NULL) {
- printf("Failed to copy samples.\n");
- return;
- }
- }
- //Tone generation
- //List of waveform functions
- const WaveMap waveList[] = {
- //(uint8)
- {"lsine", ukaeLSine },
- {"testing", testing },
- {"sine", ukaelSine },
- {"csine", ukaelCSine },
- {"triangle", ukaelTriangle },
- {"saw", ukaelSaw },
- {"square", ukaelSquare },
- {"noise", ukaelNoise },
- {"wnoise", ukaelWNoise },
- {"pulse", ukaelPulse },
- {"sinef", ukaelSinef },
- {"rwalk", ukaelRWalk }
- };
- //Number of elements in the mapping table
- const size_t waveCount = sizeof(waveList) / sizeof(waveList[0]);
- // generate and store waveform to buffer
- const void generateTone(
- AudioData* samples,
- uint8_t amplitude,
- const char* waveKey,
- WaveArg* wargs
- ) {
- // Find the waveform function pointers based on the string identifier
- ukaelWave waveFunc = NULL;
- for (size_t i = 0; i < waveCount; ++i) {
- if (strcmp(waveKey, waveList[i].id) == 0) {
- waveFunc = waveList[i].waveFunc;
- break;
- }
- }
- if(waveFunc==NULL){
- printf("Invalid key!\n");
- return;
- }
- // Generate
- uint16_t value;
- for (uint32_t i = 0; i < (samples->size); ++i) {
- wargs->time = i;
- //Generate and pass arguments
- value = waveFunc(wargs);
- value = ((value * amplitude)>>8) + ((UINT8_MAX - amplitude)>>1); // Amplitude
- *&samples->data[i] = value;
- }
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement