Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to read from buffer with feedback, so doesn't buffer overflow?
    text
    copied!<p>I have this code</p> <pre><code>#define BUFFER_LEN (2048) static float buffer[BUFFER_LEN]; int readcount; while ((readcount = sf_read_float(handle, buffer, BUFFER_LEN))) { // alsa play } </code></pre> <p>which reads BUFFER_LEN floats from buffer, and returns the number of floats it actually read. "handle" tells sf_rad_float how big buffer is.</p> <p>E.g. if buffer contains 5 floats, and BUFFER_LEN is 3, readcount would first return 3, and next time 2, and the while-loop would exit.</p> <p>I would like to have a function that does the same.</p> <p><strong>Update</strong></p> <p>After a lot of coding, I think this is the solution.</p> <pre><code>#include &lt;stdio.h&gt; int copy_buffer(double* src, int src_length, int* src_pos, float* dest, int dest_length) { int copy_length = 0; if (src_length - *src_pos &gt; dest_length) { copy_length = dest_length; printf("copy_length1 %i\n", copy_length); } else { copy_length = src_length - *src_pos; printf("copy_length2 %i\n", copy_length); } for (int i = 0; i &lt; copy_length; i++) { dest[i] = (float) src[*src_pos + i]; } // remember where to continue next time the copy_buffer() is called *src_pos += copy_length; return copy_length; } int main() { double src[] = {1,2,3,4,5}; int src_length = 5; float dest[] = {0,0}; int dest_length = 2; int read; int src_pos = 0; read = copy_buffer(src, src_length, &amp;src_pos, dest, dest_length); printf("read %i\n", read); printf("src_pos %i\n", src_pos); for (int i = 0; i &lt; src_length; i++) { printf("src %f\n", src[i]); } for (int i = 0; i &lt; dest_length; i++) { printf("dest %f\n", dest[i]); } return 0; } </code></pre> <p>Next time copy_buffer() is called, dest contains 3,4. Running <code>copy_buffer()</code> again only copies the value "5". So I think it works now.</p> <p>Although it is not very pretty, that I have <code>int src_pos = 0;</code> outside on <code>copy_buffer()</code>.</p> <p>It would be a lot better, if I instead could give <code>copy_buffer()</code> a unique handle instead of <code>&amp;src_pos</code>, just like sndfile does.</p> <p>Does anyone know how that could be done?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload