| 1 | /*---------------------------------------------------------------------------- |
| 2 | ChucK Strongly-timed Audio Programming Language |
| 3 | Compiler, Virtual Machine, and Synthesis Engine |
| 4 | |
| 5 | Copyright (c) 2003 Ge Wang and Perry R. Cook. All rights reserved. |
| 6 | http://chuck.stanford.edu/ |
| 7 | http://chuck.cs.princeton.edu/ |
| 8 | |
| 9 | This program is free software; you can redistribute it and/or modify |
| 10 | it under the dual-license terms of EITHER the MIT License OR the GNU |
| 11 | General Public License (the latter as published by the Free Software |
| 12 | Foundation; either version 2 of the License or, at your option, any |
| 13 | later version). |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful and/or |
| 16 | interesting, but WITHOUT ANY WARRANTY; without even the implied warranty |
| 17 | of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | MIT Licence and/or the GNU General Public License for details. |
| 19 | |
| 20 | You should have received a copy of the MIT License and the GNU General |
| 21 | Public License (GPL) along with this program; a copy of the GPL can also |
| 22 | be obtained by writing to the Free Software Foundation, Inc., 59 Temple |
| 23 | Place, Suite 330, Boston, MA 02111-1307 U.S.A. |
| 24 | -----------------------------------------------------------------------------*/ |
| 25 | |
| 26 | //----------------------------------------------------------------------------- |
| 27 | // file: util_buffers.cpp |
| 28 | // desc: buffer utilities |
| 29 | // |
| 30 | // author: Ge Wang ([email protected]) |
| 31 | // Perry R. Cook ([email protected]) |
| 32 | // Ananya Misra ([email protected]) |
| 33 | // date: Spring 2004 |
| 34 | // Summer 2005 - allow multiple readers |
| 35 | //----------------------------------------------------------------------------- |
| 36 | #include "util_buffers.h" |
| 37 | #include "chuck_errmsg.h" |
| 38 | #include <stdlib.h> |
| 39 | |
| 40 | |
| 41 | #ifndef CALLBACK |
| 42 | #define CALLBACK |
| 43 | #endif |
| 44 | |
| 45 | |
| 46 | |
| 47 | //----------------------------------------------------------------------------- |
| 48 | // name: CBufferAdvance() |
| 49 | // desc: constructor |
| 50 | //----------------------------------------------------------------------------- |
| 51 | CBufferAdvance::CBufferAdvance() |
| 52 | { |
| 53 | m_data = NULL; |
| 54 | m_data_width = m_write_offset = m_max_elem = 0; // = m_read_offset |
| 55 | } |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | //----------------------------------------------------------------------------- |
| 61 | // name: ~CBufferAdvance() |
| 62 | // desc: destructor |
| 63 | //----------------------------------------------------------------------------- |
| 64 | CBufferAdvance::~CBufferAdvance() |
| 65 | { |
| 66 | this->cleanup(); |
| 67 | } |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | //----------------------------------------------------------------------------- |
| 73 | // name: initialize() |
| 74 | // desc: initialize |
| 75 | //----------------------------------------------------------------------------- |
| 76 | BOOL__ CBufferAdvance::initialize( UINT__ num_elem, UINT__ width, CBufferSimple * event_buffer ) |
| 77 | { |
| 78 | // cleanup |
| 79 | cleanup(); |
| 80 | |
| 81 | // allocate |
| 82 | m_data = (BYTE__ *)malloc( num_elem * width ); |
| 83 | if( !m_data ) |
| 84 | return false; |
| 85 | |
| 86 | m_data_width = width; |
| 87 | //m_read_offset = 0; |
| 88 | m_write_offset = 0; |
| 89 | m_max_elem = (SINT__)num_elem; |
| 90 | |
| 91 | m_event_buffer = event_buffer; |
| 92 | |
| 93 | return true; |
| 94 | } |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | //----------------------------------------------------------------------------- |
| 100 | // name: cleanup() |
| 101 | // desc: cleanup |
| 102 | //----------------------------------------------------------------------------- |
| 103 | void CBufferAdvance::cleanup() |
| 104 | { |
| 105 | if( !m_data ) |
| 106 | return; |
| 107 | |
| 108 | free( m_data ); |
| 109 | |
| 110 | m_data = NULL; |
| 111 | m_data_width = 0; |
| 112 | m_write_offset = m_max_elem = 0; // = m_read_offset |
| 113 | } |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | //----------------------------------------------------------------------------- |
| 119 | // name: UINT__ join() |
| 120 | // desc: shred can call this to get an index into the vector of read pointers |
| 121 | //----------------------------------------------------------------------------- |
| 122 | UINT__ CBufferAdvance::join( Chuck_Event * event ) |
| 123 | { |
| 124 | // TODO: necessary? |
| 125 | #ifndef __DISABLE_THREADS__ |
| 126 | m_mutex.acquire(); |
| 127 | #endif |
| 128 | |
| 129 | // index of new pointer that will be pushed back |
| 130 | UINT__ read_offset_index; |
| 131 | |
| 132 | // add new pointer pointing (as pointers do) to current write offset |
| 133 | // (shreds don't get interrupted, so m_write_offset will always be correct, right?) |
| 134 | // (uh, hope so...) |
| 135 | if( !m_free.empty() ) |
| 136 | { |
| 137 | read_offset_index = m_free.front(); |
| 138 | m_free.pop(); |
| 139 | //assert( read_offset_index < m_read_offsets.size() ); |
| 140 | m_read_offsets[read_offset_index] = ReadOffset( m_write_offset, event ); |
| 141 | } |
| 142 | else |
| 143 | { |
| 144 | read_offset_index = m_read_offsets.size(); |
| 145 | m_read_offsets.push_back( ReadOffset( m_write_offset, event ) ); |
| 146 | } |
| 147 | |
| 148 | // TODO: necessary? |
| 149 | #ifndef __DISABLE_THREADS__ |
| 150 | m_mutex.release(); |
| 151 | #endif |
| 152 | |
| 153 | // return index |
| 154 | return read_offset_index; |
| 155 | } |
| 156 | |
| 157 | |
| 158 | //----------------------------------------------------------------------------- |
| 159 | // name: resign |
| 160 | // desc: shred quits buffer; frees its index |
| 161 | //----------------------------------------------------------------------------- |
| 162 | void CBufferAdvance::resign( UINT__ read_offset_index ) |
| 163 | { |
| 164 | // make sure read_offset_index passed in is valid |
| 165 | if( read_offset_index >= m_read_offsets.size() ) |
| 166 | return; |
| 167 | |
| 168 | // TODO: necessary? |
| 169 | #ifndef __DISABLE_THREADS__ |
| 170 | m_mutex.acquire(); |
| 171 | #endif |
| 172 | |
| 173 | // add this index to free queue |
| 174 | m_free.push( read_offset_index ); |
| 175 | |
| 176 | // "invalidate" the pointer at that index |
| 177 | m_read_offsets[read_offset_index].read_offset = -1; |
| 178 | m_read_offsets[read_offset_index].event = NULL; |
| 179 | |
| 180 | // TODO: necessary? |
| 181 | #ifndef __DISABLE_THREADS__ |
| 182 | m_mutex.release(); |
| 183 | #endif |
| 184 | } |
| 185 | |
| 186 | |
| 187 | //----------------------------------------------------------------------------- |
| 188 | // name: put() |
| 189 | // desc: put |
| 190 | //----------------------------------------------------------------------------- |
| 191 | /*void CBufferAdvance::put( void * data, UINT__ num_elem ) |
| 192 | { |
| 193 | UINT__ i, j; |
| 194 | BYTE__ * d = (BYTE__ *)data; |
| 195 | |
| 196 | // copy |
| 197 | for( i = 0; i < num_elem; i++ ) |
| 198 | { |
| 199 | for( j = 0; j < m_data_width; j++ ) |
| 200 | { |
| 201 | m_data[m_write_offset*m_data_width+j] = d[i*m_data_width+j]; |
| 202 | } |
| 203 | |
| 204 | // move the write |
| 205 | m_write_offset++; |
| 206 | |
| 207 | // wrap |
| 208 | if( m_write_offset >= m_max_elem ) |
| 209 | m_write_offset = 0; |
| 210 | } |
| 211 | }*/ |
| 212 | |
| 213 | |
| 214 | void CBufferAdvance::put( void * data, UINT__ num_elem ) |
| 215 | { |
| 216 | UINT__ i, j; |
| 217 | BYTE__ * d = (BYTE__ *)data; |
| 218 | |
| 219 | // TODO: necessary? |
| 220 | #ifndef __DISABLE_THREADS__ |
| 221 | m_mutex.acquire(); |
| 222 | #endif |
| 223 | |
| 224 | // copy |
| 225 | for( i = 0; i < num_elem; i++ ) |
| 226 | { |
| 227 | for( j = 0; j < m_data_width; j++ ) |
| 228 | { |
| 229 | m_data[m_write_offset*m_data_width+j] = d[i*m_data_width+j]; |
| 230 | } |
| 231 | |
| 232 | // move the write |
| 233 | m_write_offset++; |
| 234 | // wrap |
| 235 | if( m_write_offset >= m_max_elem ) |
| 236 | m_write_offset = 0; |
| 237 | |
| 238 | // possibility of expelling evil shreds |
| 239 | for( j = 0; j < m_read_offsets.size(); j++ ) |
| 240 | { |
| 241 | if( m_write_offset == m_read_offsets[j].read_offset ) |
| 242 | { |
| 243 | // inform shred with index j that it has lost its privileges? |
| 244 | // invalidate its read_offset |
| 245 | // m_read_offsets[j].read_offset = -1; |
| 246 | } |
| 247 | |
| 248 | if( m_read_offsets[j].event ) |
| 249 | m_read_offsets[j].event->queue_broadcast( m_event_buffer ); |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | // TODO: necessary? |
| 254 | #ifndef __DISABLE_THREADS__ |
| 255 | m_mutex.release(); |
| 256 | #endif |
| 257 | } |
| 258 | |
| 259 | |
| 260 | |
| 261 | |
| 262 | //----------------------------------------------------------------------------- |
| 263 | // name: get() |
| 264 | // desc: get |
| 265 | //----------------------------------------------------------------------------- |
| 266 | /*UINT__ CBufferAdvance::get( void * data, UINT__ num_elem ) |
| 267 | { |
| 268 | UINT__ i, j; |
| 269 | BYTE__ * d = (BYTE__ *)data; |
| 270 | |
| 271 | // read catch up with write |
| 272 | if( m_read_offset == m_write_offset ) |
| 273 | return 0; |
| 274 | |
| 275 | // copy |
| 276 | for( i = 0; i < num_elem; i++ ) |
| 277 | { |
| 278 | for( j = 0; j < m_data_width; j++ ) |
| 279 | { |
| 280 | d[i*m_data_width+j] = m_data[m_read_offset*m_data_width+j]; |
| 281 | } |
| 282 | |
| 283 | // move read |
| 284 | m_read_offset++; |
| 285 | |
| 286 | // catch up |
| 287 | if( m_read_offset == m_write_offset ) |
| 288 | { |
| 289 | i++; |
| 290 | break; |
| 291 | } |
| 292 | |
| 293 | // wrap |
| 294 | if( m_read_offset >= m_max_elem ) |
| 295 | m_read_offset = 0; |
| 296 | } |
| 297 | |
| 298 | // return number of elems |
| 299 | return 1; |
| 300 | }*/ |
| 301 | |
| 302 | |
| 303 | BOOL__ CBufferAdvance::empty( UINT__ read_offset_index ) |
| 304 | { |
| 305 | // make sure index is valid |
| 306 | if( read_offset_index >= m_read_offsets.size() ) |
| 307 | return TRUE; |
| 308 | if( m_read_offsets[read_offset_index].read_offset < 0 ) |
| 309 | return TRUE; |
| 310 | |
| 311 | SINT__ m_read_offset = m_read_offsets[read_offset_index].read_offset; |
| 312 | |
| 313 | // see if caught up |
| 314 | return m_read_offset == m_write_offset; |
| 315 | } |
| 316 | |
| 317 | |
| 318 | UINT__ CBufferAdvance::get( void * data, UINT__ num_elem, UINT__ read_offset_index ) |
| 319 | { |
| 320 | UINT__ i, j; |
| 321 | BYTE__ * d = (BYTE__ *)data; |
| 322 | |
| 323 | // TODO: necessary? |
| 324 | #ifndef __DISABLE_THREADS__ |
| 325 | m_mutex.acquire(); |
| 326 | #endif |
| 327 | |
| 328 | // make sure index is valid |
| 329 | if( read_offset_index >= m_read_offsets.size() ) |
| 330 | { |
| 331 | #ifndef __DISABLE_THREADS__ |
| 332 | m_mutex.release(); |
| 333 | #endif |
| 334 | return 0; |
| 335 | } |
| 336 | if( m_read_offsets[read_offset_index].read_offset < 0 ) |
| 337 | { |
| 338 | #ifndef __DISABLE_THREADS__ |
| 339 | m_mutex.release(); |
| 340 | #endif |
| 341 | return 0; |
| 342 | } |
| 343 | |
| 344 | SINT__ m_read_offset = m_read_offsets[read_offset_index].read_offset; |
| 345 | |
| 346 | // read catch up with write |
| 347 | if( m_read_offset == m_write_offset ) |
| 348 | { |
| 349 | #ifndef __DISABLE_THREADS__ |
| 350 | m_mutex.release(); |
| 351 | #endif |
| 352 | return 0; |
| 353 | } |
| 354 | |
| 355 | // copy |
| 356 | for( i = 0; i < num_elem; i++ ) |
| 357 | { |
| 358 | for( j = 0; j < m_data_width; j++ ) |
| 359 | { |
| 360 | d[i*m_data_width+j] = m_data[m_read_offset*m_data_width+j]; |
| 361 | } |
| 362 | |
| 363 | // move read |
| 364 | m_read_offset++; |
| 365 | // wrap |
| 366 | if( m_read_offset >= m_max_elem ) |
| 367 | m_read_offset = 0; |
| 368 | |
| 369 | // catch up |
| 370 | if( m_read_offset == m_write_offset ) |
| 371 | { |
| 372 | i++; |
| 373 | break; |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | // update read offset at given index |
| 378 | m_read_offsets[read_offset_index].read_offset = m_read_offset; |
| 379 | |
| 380 | // TODO: necessary? |
| 381 | #ifndef __DISABLE_THREADS__ |
| 382 | m_mutex.release(); |
| 383 | #endif |
| 384 | |
| 385 | // return number of elems |
| 386 | return i; |
| 387 | } |
| 388 | |
| 389 | |
| 390 | |
| 391 | |
| 392 | //----------------------------------------------------------------------------- |
| 393 | // name: CBufferSimple() |
| 394 | // desc: constructor |
| 395 | //----------------------------------------------------------------------------- |
| 396 | CBufferSimple::CBufferSimple() |
| 397 | { |
| 398 | m_data = NULL; |
| 399 | m_data_width = m_read_offset = m_write_offset = m_max_elem = 0; |
| 400 | } |
| 401 | |
| 402 | |
| 403 | |
| 404 | |
| 405 | //----------------------------------------------------------------------------- |
| 406 | // name: ~CBufferSimple() |
| 407 | // desc: destructor |
| 408 | //----------------------------------------------------------------------------- |
| 409 | CBufferSimple::~CBufferSimple() |
| 410 | { |
| 411 | this->cleanup(); |
| 412 | } |
| 413 | |
| 414 | |
| 415 | |
| 416 | |
| 417 | //----------------------------------------------------------------------------- |
| 418 | // name: initialize() |
| 419 | // desc: initialize |
| 420 | //----------------------------------------------------------------------------- |
| 421 | BOOL__ CBufferSimple::initialize( UINT__ num_elem, UINT__ width ) |
| 422 | { |
| 423 | // cleanup |
| 424 | cleanup(); |
| 425 | |
| 426 | // allocate |
| 427 | m_data = (BYTE__ *)malloc( num_elem * width ); |
| 428 | if( !m_data ) |
| 429 | return false; |
| 430 | |
| 431 | m_data_width = width; |
| 432 | m_read_offset = 0; |
| 433 | m_write_offset = 0; |
| 434 | m_max_elem = num_elem; |
| 435 | |
| 436 | return true; |
| 437 | } |
| 438 | |
| 439 | |
| 440 | |
| 441 | |
| 442 | //----------------------------------------------------------------------------- |
| 443 | // name: cleanup() |
| 444 | // desc: cleanup |
| 445 | //----------------------------------------------------------------------------- |
| 446 | void CBufferSimple::cleanup() |
| 447 | { |
| 448 | if( !m_data ) |
| 449 | return; |
| 450 | |
| 451 | free( m_data ); |
| 452 | |
| 453 | m_data = NULL; |
| 454 | m_data_width = m_read_offset = m_write_offset = m_max_elem = 0; |
| 455 | } |
| 456 | |
| 457 | |
| 458 | |
| 459 | |
| 460 | //----------------------------------------------------------------------------- |
| 461 | // name: put() |
| 462 | // desc: put |
| 463 | //----------------------------------------------------------------------------- |
| 464 | void CBufferSimple::put( void * data, UINT__ num_elem ) |
| 465 | { |
| 466 | UINT__ i, j; |
| 467 | BYTE__ * d = (BYTE__ *)data; |
| 468 | |
| 469 | #ifndef __DISABLE_THREADS__ |
| 470 | m_mutex.acquire(); |
| 471 | #endif |
| 472 | |
| 473 | // copy |
| 474 | for( i = 0; i < num_elem; i++ ) |
| 475 | { |
| 476 | for( j = 0; j < m_data_width; j++ ) |
| 477 | { |
| 478 | m_data[m_write_offset*m_data_width+j] = d[i*m_data_width+j]; |
| 479 | } |
| 480 | |
| 481 | // move the write |
| 482 | // Aug 2014 - spencer |
| 483 | // change to fully "atomic" increment+wrap |
| 484 | m_write_offset = (m_write_offset + 1) % m_max_elem; |
| 485 | } |
| 486 | |
| 487 | #ifndef __DISABLE_THREADS__ |
| 488 | m_mutex.release(); |
| 489 | #endif |
| 490 | } |
| 491 | |
| 492 | |
| 493 | |
| 494 | |
| 495 | //----------------------------------------------------------------------------- |
| 496 | // name: get() |
| 497 | // desc: get |
| 498 | //----------------------------------------------------------------------------- |
| 499 | UINT__ CBufferSimple::get( void * data, UINT__ num_elem ) |
| 500 | { |
| 501 | UINT__ i, j; |
| 502 | BYTE__ * d = (BYTE__ *)data; |
| 503 | |
| 504 | // read catch up with write |
| 505 | if( m_read_offset == m_write_offset ) |
| 506 | return 0; |
| 507 | |
| 508 | #ifndef __DISABLE_THREADS__ |
| 509 | m_mutex.acquire(); |
| 510 | #endif |
| 511 | |
| 512 | // copy |
| 513 | for( i = 0; i < num_elem; i++ ) |
| 514 | { |
| 515 | for( j = 0; j < m_data_width; j++ ) |
| 516 | { |
| 517 | d[i*m_data_width+j] = m_data[m_read_offset*m_data_width+j]; |
| 518 | } |
| 519 | |
| 520 | // move read |
| 521 | // Aug 2014 - spencer |
| 522 | // change to fully "atomic" increment+wrap |
| 523 | m_read_offset = (m_read_offset + 1) % m_max_elem; |
| 524 | |
| 525 | // catch up |
| 526 | if( m_read_offset == m_write_offset ) |
| 527 | { |
| 528 | i++; |
| 529 | break; |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | #ifndef __DISABLE_THREADS__ |
| 534 | m_mutex.release(); |
| 535 | #endif |
| 536 | |
| 537 | // return number of elems |
| 538 | return 1; // shouldn't it return i? |
| 539 | } |
| 540 | |
| 541 | |
| 542 | |
| 543 | |
| 544 | //----------------------------------------------------------------------------- |
| 545 | // name: AccumBuffer() |
| 546 | // desc: constructor |
| 547 | //----------------------------------------------------------------------------- |
| 548 | AccumBuffer::AccumBuffer() |
| 549 | { |
| 550 | m_data = NULL; |
| 551 | m_write_offset = m_max_elem = 0; |
| 552 | } |
| 553 | |
| 554 | |
| 555 | |
| 556 | |
| 557 | //----------------------------------------------------------------------------- |
| 558 | // name: ~AccumBuffer() |
| 559 | // desc: destructor |
| 560 | //----------------------------------------------------------------------------- |
| 561 | AccumBuffer::~AccumBuffer() |
| 562 | { |
| 563 | this->cleanup(); |
| 564 | } |
| 565 | |
| 566 | |
| 567 | |
| 568 | |
| 569 | //----------------------------------------------------------------------------- |
| 570 | // name: resize() |
| 571 | // desc: resize |
| 572 | //----------------------------------------------------------------------------- |
| 573 | t_CKINT AccumBuffer::resize( t_CKINT size ) |
| 574 | { |
| 575 | // if the same, then done |
| 576 | if( size == m_max_elem ) return size; |
| 577 | |
| 578 | // allocate |
| 579 | SAMPLE * next = (SAMPLE *)malloc( size * sizeof(SAMPLE) ); |
| 580 | // zero it |
| 581 | memset( next, 0, size * sizeof(SAMPLE) ); |
| 582 | if( !next ) |
| 583 | { |
| 584 | // log |
| 585 | EM_log( CK_LOG_WARNING, "AccumBuffer:resize(%ld) failed to allocated..." ); |
| 586 | // clean up |
| 587 | this->cleanup(); |
| 588 | // done |
| 589 | return FALSE; |
| 590 | } |
| 591 | |
| 592 | // if no current |
| 593 | if( !m_data ) |
| 594 | { |
| 595 | // reset everything |
| 596 | m_data = next; |
| 597 | m_write_offset = 0; |
| 598 | m_max_elem = size; |
| 599 | } |
| 600 | else |
| 601 | { |
| 602 | // retrieve next |
| 603 | this->get( next, size ); |
| 604 | // clean up |
| 605 | this->cleanup(); |
| 606 | // copy |
| 607 | m_data = next; |
| 608 | // write pointer is at the end |
| 609 | m_write_offset = 0; |
| 610 | // update max elem |
| 611 | m_max_elem = size; |
| 612 | } |
| 613 | |
| 614 | return TRUE; |
| 615 | } |
| 616 | |
| 617 | |
| 618 | |
| 619 | |
| 620 | //----------------------------------------------------------------------------- |
| 621 | // name: cleanup() |
| 622 | // desc: cleanup |
| 623 | //----------------------------------------------------------------------------- |
| 624 | void AccumBuffer::cleanup() |
| 625 | { |
| 626 | if( !m_data ) |
| 627 | return; |
| 628 | |
| 629 | free( m_data ); |
| 630 | m_data = NULL; |
| 631 | m_write_offset = m_max_elem = 0; |
| 632 | } |
| 633 | |
| 634 | |
| 635 | |
| 636 | |
| 637 | //----------------------------------------------------------------------------- |
| 638 | // name: put() |
| 639 | // desc: put |
| 640 | //----------------------------------------------------------------------------- |
| 641 | void AccumBuffer::put( SAMPLE data ) |
| 642 | { |
| 643 | // copy |
| 644 | m_data[m_write_offset] = data; |
| 645 | // move the write |
| 646 | m_write_offset++; |
| 647 | // wrap |
| 648 | if( m_write_offset >= m_max_elem ) |
| 649 | m_write_offset = 0; |
| 650 | } |
| 651 | |
| 652 | |
| 653 | |
| 654 | |
| 655 | //----------------------------------------------------------------------------- |
| 656 | // name: get() |
| 657 | // desc: get least recent num_elem (starting with our oldest element) |
| 658 | //----------------------------------------------------------------------------- |
| 659 | void AccumBuffer::get( SAMPLE * buffer, t_CKINT num_elem ) |
| 660 | { |
| 661 | // left to copy |
| 662 | t_CKINT left = num_elem; |
| 663 | // amount |
| 664 | t_CKINT amount = m_max_elem - m_write_offset; |
| 665 | // to copy |
| 666 | t_CKINT tocopy = ck_min( left, amount ); |
| 667 | // update left |
| 668 | left -= tocopy; |
| 669 | // copy after the write pointer |
| 670 | memcpy( buffer, m_data + m_write_offset, tocopy * sizeof(SAMPLE) ); |
| 671 | |
| 672 | // more? |
| 673 | if( left ) |
| 674 | { |
| 675 | // amount |
| 676 | amount = m_write_offset; |
| 677 | // to copy |
| 678 | t_CKINT tocopy2 = ck_min( left, amount ); |
| 679 | // update left |
| 680 | left -= tocopy2; |
| 681 | // copy before the write pointer |
| 682 | memcpy( buffer + tocopy, m_data, tocopy2 * sizeof(SAMPLE) ); |
| 683 | |
| 684 | // more? |
| 685 | if( left ) |
| 686 | { |
| 687 | // make sure |
| 688 | assert( m_max_elem == (tocopy + tocopy2) ); |
| 689 | assert( num_elem > m_max_elem ); |
| 690 | // zero it out |
| 691 | memset( buffer + m_max_elem, 0, (num_elem - m_max_elem) * sizeof(SAMPLE) ); |
| 692 | } |
| 693 | } |
| 694 | } |
| 695 | |
| 696 | |
| 697 | |
| 698 | |
| 699 | //----------------------------------------------------------------------------- |
| 700 | // name: get_most_recent() |
| 701 | // desc: get most recent num_elem. (get() gets least recent num_elem) |
| 702 | //----------------------------------------------------------------------------- |
| 703 | void AccumBuffer::get_most_recent( SAMPLE * buffer, t_CKINT num_elem ) |
| 704 | { |
| 705 | // compute offset of starting point to copy memory |
| 706 | t_CKINT startOffset = m_write_offset - num_elem; |
| 707 | // wrap around until have a valid offset |
| 708 | while( startOffset < 0 ) |
| 709 | { |
| 710 | startOffset += m_max_elem; |
| 711 | } |
| 712 | |
| 713 | // left to copy |
| 714 | t_CKINT left = num_elem; |
| 715 | // amount |
| 716 | t_CKINT amount = m_max_elem - startOffset; |
| 717 | // to copy |
| 718 | t_CKINT tocopy = ck_min( left, amount ); |
| 719 | // update left |
| 720 | left -= tocopy; |
| 721 | // copy after the write pointer |
| 722 | memcpy( buffer, m_data + startOffset, tocopy * sizeof(SAMPLE) ); |
| 723 | |
| 724 | // more? |
| 725 | if( left ) |
| 726 | { |
| 727 | // amount |
| 728 | amount = startOffset; |
| 729 | // to copy |
| 730 | t_CKINT tocopy2 = ck_min( left, amount ); |
| 731 | // update left |
| 732 | left -= tocopy2; |
| 733 | // copy before the write pointer |
| 734 | memcpy( buffer + tocopy, m_data, tocopy2 * sizeof(SAMPLE) ); |
| 735 | |
| 736 | // more? |
| 737 | if( left ) |
| 738 | { |
| 739 | // make sure |
| 740 | assert( m_max_elem == (tocopy + tocopy2) ); |
| 741 | assert( num_elem > m_max_elem ); |
| 742 | // zero it out |
| 743 | memset( buffer + m_max_elem, 0, (num_elem - m_max_elem) * sizeof(SAMPLE) ); |
| 744 | } |
| 745 | } |
| 746 | } |
| 747 | |
| 748 | |
| 749 | |
| 750 | |
| 751 | //----------------------------------------------------------------------------- |
| 752 | // name: DeccumBuffer() |
| 753 | // desc: constructor |
| 754 | //----------------------------------------------------------------------------- |
| 755 | DeccumBuffer::DeccumBuffer() |
| 756 | { |
| 757 | m_data = NULL; |
| 758 | m_read_offset = m_max_elem = 0; |
| 759 | } |
| 760 | |
| 761 | |
| 762 | |
| 763 | |
| 764 | //----------------------------------------------------------------------------- |
| 765 | // name: ~DeccumBuffer() |
| 766 | // desc: destructor |
| 767 | //----------------------------------------------------------------------------- |
| 768 | DeccumBuffer::~DeccumBuffer() |
| 769 | { |
| 770 | this->cleanup(); |
| 771 | } |
| 772 | |
| 773 | |
| 774 | |
| 775 | |
| 776 | //----------------------------------------------------------------------------- |
| 777 | // name: resize() |
| 778 | // desc: resize |
| 779 | //----------------------------------------------------------------------------- |
| 780 | t_CKINT DeccumBuffer::resize( t_CKINT size ) |
| 781 | { |
| 782 | // if the same, then done |
| 783 | if( size == m_max_elem ) return size; |
| 784 | |
| 785 | // allocate |
| 786 | SAMPLE * next = (SAMPLE *)malloc( size * sizeof(SAMPLE) ); |
| 787 | // zero it |
| 788 | memset( next, 0, size * sizeof(SAMPLE) ); |
| 789 | if( !next ) |
| 790 | { |
| 791 | // log |
| 792 | EM_log( CK_LOG_WARNING, "DeccumBuffer:resize(%ld) failed to allocated..." ); |
| 793 | // clean up |
| 794 | this->cleanup(); |
| 795 | // done |
| 796 | return FALSE; |
| 797 | } |
| 798 | |
| 799 | // if no current |
| 800 | if( !m_data ) |
| 801 | { |
| 802 | // reset everything |
| 803 | m_data = next; |
| 804 | m_read_offset = 0; |
| 805 | m_max_elem = size; |
| 806 | } |
| 807 | else |
| 808 | { |
| 809 | // retrieve next |
| 810 | this->get( next, size ); |
| 811 | // clean up |
| 812 | this->cleanup(); |
| 813 | // copy |
| 814 | m_data = next; |
| 815 | // read pointer is at the end |
| 816 | m_read_offset = 0; |
| 817 | // update max elem |
| 818 | m_max_elem = size; |
| 819 | } |
| 820 | |
| 821 | return TRUE; |
| 822 | } |
| 823 | |
| 824 | |
| 825 | |
| 826 | |
| 827 | //----------------------------------------------------------------------------- |
| 828 | // name: cleanup() |
| 829 | // desc: cleanup |
| 830 | //----------------------------------------------------------------------------- |
| 831 | void DeccumBuffer::cleanup() |
| 832 | { |
| 833 | if( !m_data ) |
| 834 | return; |
| 835 | |
| 836 | free( m_data ); |
| 837 | m_data = NULL; |
| 838 | m_read_offset = m_max_elem = 0; |
| 839 | } |
| 840 | |
| 841 | |
| 842 | |
| 843 | |
| 844 | //----------------------------------------------------------------------------- |
| 845 | // name: put() |
| 846 | // desc: put |
| 847 | //----------------------------------------------------------------------------- |
| 848 | void DeccumBuffer::get( SAMPLE * data ) |
| 849 | { |
| 850 | // copy |
| 851 | *data = m_data[m_read_offset]; |
| 852 | // zero |
| 853 | m_data[m_read_offset] = 0; |
| 854 | // move the write |
| 855 | m_read_offset++; |
| 856 | // wrap |
| 857 | if( m_read_offset >= m_max_elem ) |
| 858 | m_read_offset = 0; |
| 859 | } |
| 860 | |
| 861 | |
| 862 | |
| 863 | |
| 864 | //----------------------------------------------------------------------------- |
| 865 | // name: get() |
| 866 | // desc: get |
| 867 | //----------------------------------------------------------------------------- |
| 868 | void DeccumBuffer::get( SAMPLE * buffer, t_CKINT num_elem ) |
| 869 | { |
| 870 | // left to copy |
| 871 | t_CKINT left = num_elem; |
| 872 | // amount |
| 873 | t_CKINT amount = m_max_elem - m_read_offset; |
| 874 | // to copy |
| 875 | t_CKINT tocopy = ck_min( left, amount ); |
| 876 | // update left |
| 877 | left -= tocopy; |
| 878 | // copy after the write pointer |
| 879 | memcpy( buffer, m_data + m_read_offset, tocopy * sizeof(SAMPLE) ); |
| 880 | |
| 881 | // more? |
| 882 | if( left ) |
| 883 | { |
| 884 | // amount |
| 885 | amount = m_read_offset; |
| 886 | // to copy |
| 887 | t_CKINT tocopy2 = ck_min( left, amount ); |
| 888 | // update left |
| 889 | left -= tocopy2; |
| 890 | // copy before the write pointer |
| 891 | memcpy( buffer + tocopy, m_data, tocopy2 * sizeof(SAMPLE) ); |
| 892 | |
| 893 | // more? |
| 894 | if( left ) |
| 895 | { |
| 896 | // make sure |
| 897 | assert( m_max_elem == (tocopy + tocopy2) ); |
| 898 | assert( num_elem > m_max_elem ); |
| 899 | // zero it out |
| 900 | memset( buffer + m_max_elem, 0, (num_elem - m_max_elem) * sizeof(SAMPLE) ); |
| 901 | } |
| 902 | } |
| 903 | } |
| 904 | |
| 905 | |
| 906 | |
| 907 | |
| 908 | //----------------------------------------------------------------------------- |
| 909 | // name: put() |
| 910 | // desc: put |
| 911 | //----------------------------------------------------------------------------- |
| 912 | void DeccumBuffer::put( SAMPLE * buffer, t_CKINT num_elem ) |
| 913 | { |
| 914 | // left to add |
| 915 | t_CKINT left = num_elem; |
| 916 | // amount |
| 917 | t_CKINT amount = m_max_elem - m_read_offset; |
| 918 | // to add |
| 919 | t_CKINT tocopy = ck_min( left, amount ); |
| 920 | // update left |
| 921 | left -= tocopy; |
| 922 | // copy after the write pointer |
| 923 | t_CKINT i; |
| 924 | for( i = 0; i < tocopy; i++ ) |
| 925 | m_data[m_read_offset+i] += buffer[i]; |
| 926 | |
| 927 | // more? |
| 928 | if( left ) |
| 929 | { |
| 930 | // amount |
| 931 | amount = m_read_offset; |
| 932 | // to copy |
| 933 | t_CKINT tocopy2 = ck_min( left, amount ); |
| 934 | // update left |
| 935 | left -= tocopy2; |
| 936 | // copy before the write pointer |
| 937 | for( i = 0; i < tocopy2; i++ ) |
| 938 | m_data[i] += buffer[tocopy+i]; |
| 939 | |
| 940 | // more? |
| 941 | if( left ) |
| 942 | { |
| 943 | // make sure |
| 944 | assert( m_max_elem == (tocopy + tocopy2) ); |
| 945 | assert( num_elem > m_max_elem ); |
| 946 | // log |
| 947 | EM_log( CK_LOG_WARNING, "(IFFT): discarding data during OLA synthesis..." ); |
| 948 | } |
| 949 | } |
| 950 | } |
| 951 | |
| 952 | |
| 953 | //----------------------------------------------------------------------------- |
| 954 | // name: SMCircularBuffer() |
| 955 | // desc: constructor |
| 956 | //----------------------------------------------------------------------------- |
| 957 | FastCircularBuffer::FastCircularBuffer() |
| 958 | { |
| 959 | m_data = NULL; |
| 960 | m_data_width = m_read_offset = m_write_offset = m_max_elem = 0; |
| 961 | } |
| 962 | |
| 963 | |
| 964 | |
| 965 | |
| 966 | //----------------------------------------------------------------------------- |
| 967 | // name: ~SMCircularBuffer() |
| 968 | // desc: destructor |
| 969 | //----------------------------------------------------------------------------- |
| 970 | FastCircularBuffer::~FastCircularBuffer() |
| 971 | { |
| 972 | this->cleanup(); |
| 973 | } |
| 974 | |
| 975 | |
| 976 | |
| 977 | |
| 978 | //----------------------------------------------------------------------------- |
| 979 | // name: initialize() |
| 980 | // desc: initialize |
| 981 | //----------------------------------------------------------------------------- |
| 982 | t_CKUINT FastCircularBuffer::initialize( t_CKUINT num_elem, t_CKUINT width ) |
| 983 | { |
| 984 | // cleanup |
| 985 | cleanup(); |
| 986 | |
| 987 | // allocate |
| 988 | m_data = (t_CKBYTE *)malloc( num_elem * width ); |
| 989 | if( !m_data ) |
| 990 | return false; |
| 991 | |
| 992 | m_data_width = width; |
| 993 | m_read_offset = 0; |
| 994 | m_write_offset = 0; |
| 995 | m_max_elem = num_elem; |
| 996 | |
| 997 | return true; |
| 998 | } |
| 999 | |
| 1000 | |
| 1001 | |
| 1002 | |
| 1003 | //----------------------------------------------------------------------------- |
| 1004 | // name: cleanup() |
| 1005 | // desc: cleanup |
| 1006 | //----------------------------------------------------------------------------- |
| 1007 | void FastCircularBuffer::cleanup() |
| 1008 | { |
| 1009 | if( !m_data ) |
| 1010 | return; |
| 1011 | |
| 1012 | free( m_data ); |
| 1013 | |
| 1014 | m_data = NULL; |
| 1015 | m_data_width = m_read_offset = m_write_offset = m_max_elem = 0; |
| 1016 | } |
| 1017 | |
| 1018 | |
| 1019 | |
| 1020 | |
| 1021 | //----------------------------------------------------------------------------- |
| 1022 | // name: put() |
| 1023 | // desc: put |
| 1024 | //----------------------------------------------------------------------------- |
| 1025 | t_CKUINT FastCircularBuffer::put( void * _data, t_CKUINT num_elem ) |
| 1026 | { |
| 1027 | t_CKBYTE * data = (t_CKBYTE *)_data; |
| 1028 | |
| 1029 | // TODO: overflow checking |
| 1030 | if(!(num_elem < ((m_read_offset > m_write_offset) ? |
| 1031 | (m_read_offset - m_write_offset) : |
| 1032 | (m_max_elem - m_write_offset + m_read_offset)))) |
| 1033 | { |
| 1034 | return 0; |
| 1035 | } |
| 1036 | |
| 1037 | t_CKUINT elems_before_end = ck_min(num_elem, m_max_elem - m_write_offset); |
| 1038 | t_CKUINT elems_after_end = num_elem - elems_before_end; |
| 1039 | |
| 1040 | if(elems_before_end) |
| 1041 | memcpy(m_data + m_write_offset * m_data_width, |
| 1042 | data, |
| 1043 | elems_before_end * m_data_width); |
| 1044 | |
| 1045 | if(elems_after_end) |
| 1046 | memcpy(m_data, |
| 1047 | data + elems_before_end * m_data_width, |
| 1048 | elems_after_end * m_data_width); |
| 1049 | |
| 1050 | if(elems_after_end) |
| 1051 | m_write_offset = elems_after_end; |
| 1052 | else |
| 1053 | m_write_offset += elems_before_end; |
| 1054 | |
| 1055 | return elems_before_end + elems_after_end; |
| 1056 | } |
| 1057 | |
| 1058 | |
| 1059 | |
| 1060 | |
| 1061 | //----------------------------------------------------------------------------- |
| 1062 | // name: get() |
| 1063 | // desc: get |
| 1064 | //----------------------------------------------------------------------------- |
| 1065 | t_CKUINT FastCircularBuffer::get( void * _data, t_CKUINT num_elem ) |
| 1066 | { |
| 1067 | t_CKBYTE * data = (t_CKBYTE *)_data; |
| 1068 | |
| 1069 | t_CKUINT elems_before_end; |
| 1070 | t_CKUINT elems_after_end; |
| 1071 | if(m_write_offset < m_read_offset) |
| 1072 | { |
| 1073 | elems_before_end = m_max_elem - m_read_offset; |
| 1074 | elems_after_end = m_write_offset; |
| 1075 | } |
| 1076 | else |
| 1077 | { |
| 1078 | elems_before_end = m_write_offset - m_read_offset; |
| 1079 | elems_after_end = 0; |
| 1080 | } |
| 1081 | |
| 1082 | if(elems_before_end > num_elem) |
| 1083 | { |
| 1084 | elems_before_end = num_elem; |
| 1085 | elems_after_end = 0; |
| 1086 | } |
| 1087 | else if(elems_before_end + elems_after_end > num_elem) |
| 1088 | { |
| 1089 | elems_after_end = num_elem - elems_before_end; |
| 1090 | } |
| 1091 | |
| 1092 | // UInt32 elems_before_end = min(m_write_offset - m_read_offset, m_max_elem - m_read_offset); |
| 1093 | // UInt32 elems_after_end = num_elem - elems_before_end; |
| 1094 | |
| 1095 | if(elems_before_end) |
| 1096 | memcpy(data, |
| 1097 | m_data + m_read_offset * m_data_width, |
| 1098 | elems_before_end * m_data_width); |
| 1099 | |
| 1100 | if(elems_after_end) |
| 1101 | memcpy(data + elems_before_end * m_data_width, |
| 1102 | m_data, |
| 1103 | elems_after_end * m_data_width); |
| 1104 | |
| 1105 | if(elems_after_end) |
| 1106 | m_read_offset = elems_after_end; |
| 1107 | else |
| 1108 | m_read_offset += elems_before_end; |
| 1109 | |
| 1110 | return elems_before_end + elems_after_end; |
| 1111 | } |
| 1112 | |
| 1113 |