M
M
Michael2015-02-08 22:14:22
Programming
Michael, 2015-02-08 22:14:22

How to fix write buffer length in bass.dll?

Hello. Trying hard to find a solution to my problem with bass.dll .
My program reads the sound from the microphone and processes it. The catch is that the length of the buffer that the callback operates on is not static. The length parameter changes: 5512, 11024, 8268, 8268, 8268, 11024, 8268 ... I
studied the help for a long time, but did not find how to fix the buffer length.
Here's how audio is captured:

rchan = BASS_RecordStart(FREQ,CHANS,0,&DuffRecording,0);  // rchan - канал записи

callback:
BOOL CALLBACK DuffRecording(HRECORD hangle, const void *buffer, DWORD lenght, void *user)
  {
  if((reclen % BUFSTEP) + lenght >= BUFSTEP)  // динамическое выделение памяти под поток
     recbuf = (char *)realloc(recbuf,((reclen + lenght) / BUFSTEP + 1) * BUFSTEP);
  memcpy(recbuf+reclen,buffer,lenght);  // recbuf - массив данных
  reclen+=lenght;  // reclen - длина массива recbuf 
  return TRUE;
  }

The organization is not the best. I will fix a lot in the callback. But the main thing for me now is to fix the length of the buffer . Have you dealt with a similar problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jcmvbkbc, 2015-02-08
@jcmvbkbc

A typical solution to this problem is to accumulate data at home and process it when it is accumulated as much as necessary. Very stupid implementation in C:

char *buf;
size_t buf_sz;
...
BOOL CALLBACK DuffRecording(HRECORD hangle, const void *buffer, DWORD lenght, void *user)
{
  buf = realloc(buf, buf_sz + length);
  memcpy(buf + buf_sz, buffer, lenght);
  buf_sz += length;
  return TRUE;
}

// эту функцию нужно вызывать когда есть новые данные,
// например, после каждого вызова DuffRecording
void chunk_handler(void)
{
  size_t offset = 0;
  while (buf_sz >= offset + CHUNK_SIZE) {
    do_something_with_data(buf + offset);
    offset += CHUNK_SIZE;
  }
  if (offset) {
    memmove(buf, buf + offset, buf_sz - offset);
    buf_sz -= offset;
  }
}

// эта функция будет вызвана с p указывающим на буфер длиной CHUNK_SIZE
void do_something_with_data(void *p)
{
  ...
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question