Qui sto cercando di scrivere del codice per un sistema audio in registrazione continua. Sto quindi tentando di registrare l'audio per un certo periodo di tempo quando viene interrotta una determinata soglia di ampiezza.Problemi di memoria con registrazione continua audio
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <time.h>
#include <portaudio.h>
#include <sndfile.h>
#define FRAMES_PER_BUFFER (1024)
#define SAMPLE_SIZE (4)
typedef struct
{
uint16_t formatType;
uint16_t numberOfChannels;
uint32_t sampleRate;
float* recordedSamples;
} AudioData;
AudioData initAudioData(uint32_t sampleRate, uint16_t channels, int type)
{
AudioData data;
data.formatType = type;
data.numberOfChannels = channels;
data.sampleRate = sampleRate;
return data;
}
float avg(float *data)
{
int elems = sizeof(data)/sizeof(data[0]);
float sum = 0;
for (int i = 0; i < elems; i++)
{
sum += fabs(*(data + i));
}
return (float) sum/elems;
}
int main(void)
{
AudioData data = initAudioData(44100, 2, paFloat32);
PaStream *stream = NULL;
PaError err = paNoError;
int size = FRAMES_PER_BUFFER * data.numberOfChannels * SAMPLE_SIZE;
float *sampleBlock = malloc(size);
float *recordedSamples = NULL;
time_t talking = 0;
time_t silence = 0;
if((err = Pa_Initialize())) goto done;
PaStreamParameters inputParameters =
{
.device = Pa_GetDefaultInputDevice(),
.channelCount = data.numberOfChannels,
.sampleFormat = data.formatType,
.suggestedLatency = Pa_GetDeviceInfo(Pa_GetDefaultInputDevice())->defaultHighInputLatency,
.hostApiSpecificStreamInfo = NULL
};
if((err = Pa_OpenStream(&stream, &inputParameters, NULL, data.sampleRate, FRAMES_PER_BUFFER, paClipOff, NULL, NULL))) goto done;
if((err = Pa_StartStream(stream))) goto done;
for(int i = 0;;)
{
err = Pa_ReadStream(stream, sampleBlock, FRAMES_PER_BUFFER);
if(avg(sampleBlock) > 0.000550) // talking
{
printf("You're talking! %d\n", i);
i++;
time(&talking);
recordedSamples = realloc(recordedSamples, size * i);
if (recordedSamples) memcpy(recordedSamples + ((i - 1) * size), sampleBlock, size); // problem here writing to memory at i = 16?
else free(recordedSamples);
}
else //silence
{
double test = difftime(time(&silence), talking);
printf("Time diff: %g\n", test);
if (test >= 1.5)
{
// TODO: finish code processing audio snippet
talking = 0;
free(recordedSamples); // problem freeing memory?
}
}
}
done:
free(sampleBlock);
Pa_Terminate();
return err;
}
Tuttavia, il codice è essere un po 'schizzinoso. A volte, quando eseguo il mio programma in Xcode, ottengo il seguente output:
Time diff: 1.4218e+09 You're talking! 0 You're talking! 1 You're talking! 2 You're talking! 3 You're talking! 4 You're talking! 5 You're talking! 6 You're talking! 7 You're talking! 8 You're talking! 9 You're talking! 10 You're talking! 11 You're talking! 12 You're talking! 13 You're talking! 14 You're talking! 15 (lldb)
Con Xcode che punta a questa linea è il problema:
if (recordedSamples) memcpy(recordedSamples + ((i - 1) * size), sampleBlock, size); // problem here writing to memory at i = 16?
Altre volte ho eseguito il codice, ottengo questo errore :
Time diff: 1.4218e+09 You're talking! 0 Time diff: 0 Time diff: 0 Time diff: 0 Time diff: 0 Time diff: 0 Time diff: 0 Time diff: 0 Time diff: 0 Time diff: 0 Time diff: 0 Time diff: 0 Time diff: 0 Time diff: 0 Time diff: 0 Time diff: 0 Time diff: 0 Time diff: 0 Time diff: 0 Time diff: 0 Time diff: 0 Time diff: 0 Time diff: 0 Time diff: 0 Time diff: 0 Time diff: 0 Time diff: 0 Time diff: 0 Time diff: 0 Time diff: 1 Time diff: 1 Time diff: 1 Time diff: 1 Time diff: 1 Time diff: 1 Time diff: 1 Time diff: 1 Time diff: 1 Time diff: 1 Time diff: 1 Time diff: 1 Time diff: 1 Time diff: 1 Time diff: 1 Time diff: 1 Time diff: 1 Time diff: 1 Time diff: 1 Time diff: 1 Time diff: 1 Time diff: 1 Time diff: 1 Time diff: 1 Time diff: 1 Time diff: 1 Time diff: 1 Time diff: 1 Time diff: 1 Time diff: 1 Time diff: 1 Time diff: 1 Time diff: 1 Time diff: 1 Time diff: 1 Time diff: 1 Time diff: 1 Time diff: 1 Time diff: 1 Time diff: 1 Time diff: 1 Time diff: 1 Time diff: 1 Time diff: 2 Time diff: 1.4218e+09 CTestEnvironment(55085,0x7fff7938e300) malloc: *** error for object 0x10081ea00: pointer being freed was not allocated *** set a breakpoint in malloc_error_break to debug
Entrambi gli errori sono un po 'confuse me ... qualche suggerimento?
non sei assegnazione 'recordedSamples' ma cercando di riallocare o libero che –
@Lashane Si prega di leggere la documentazione' realloc' – syb0rg
questa linea: 'int elems = sizeof (dati)/sizeof (dati [0]);' ha il problema che 'sizeof (data)' sarà la dimensione di un puntatore, piuttosto che la dimensione di un float. suggerire: int elems = sizeof (float)/sizeof (data [0]); – user3629249