comparison libinterp/dldfcn/audiodevinfo.cc @ 19581:c5b8a91baec6

avoid unnecessary use of pointer in audio code * audiodevinfo.cc (audioplayer::get_left): Return RowVector instead of pointer to RowVector. Method is now const. Change all uses. (audioplayer::get_right): Likewise. Handle case of single channel audio here. (portaudio_play_callback): Not here.
author John W. Eaton <jwe@octave.org>
date Thu, 08 Jan 2015 13:08:52 -0500
parents 827394ba8eb2
children 7c0e20def8e3
comparison
equal deleted inserted replaced
19580:cbd5d36c5472 19581:c5b8a91baec6
456 void init_fn (void); 456 void init_fn (void);
457 void set_y (const octave_value& y); 457 void set_y (const octave_value& y);
458 void set_y (octave_function *fn); 458 void set_y (octave_function *fn);
459 void set_y (std::string fn); 459 void set_y (std::string fn);
460 Matrix& get_y (void); 460 Matrix& get_y (void);
461 RowVector *get_left (void); 461 RowVector get_left (void) const;
462 RowVector *get_right (void); 462 RowVector get_right (void) const;
463 void set_fs (int fs); 463 void set_fs (int fs);
464 int get_fs (void); 464 int get_fs (void);
465 void set_nbits (int nbits); 465 void set_nbits (int nbits);
466 int get_nbits (void); 466 int get_nbits (void);
467 void set_id (int id); 467 void set_id (int id);
601 PaStreamCallbackFlags, void *data) 601 PaStreamCallbackFlags, void *data)
602 { 602 {
603 audioplayer *player = static_cast<audioplayer *> (data); 603 audioplayer *player = static_cast<audioplayer *> (data);
604 int big_endian = is_big_endian (); 604 int big_endian = is_big_endian ();
605 int channels = player->get_channels (); 605 int channels = player->get_channels ();
606 RowVector *sound_l = player->get_left (); 606
607 RowVector *sound_r; 607 const RowVector sound_l = player->get_left ();
608 608 const RowVector sound_r = player->get_right ();
609 if (channels > 1)
610 sound_r = player->get_right ();
611 else
612 sound_r = sound_l;
613 609
614 for (unsigned long j = 0, k = 0; j < frames; j++, k += 2) 610 for (unsigned long j = 0, k = 0; j < frames; j++, k += 2)
615 { 611 {
616 unsigned int sample_number = player->get_sample_number (); 612 unsigned int sample_number = player->get_sample_number ();
617 if (sample_number > player->get_end_sample ()) 613 if (sample_number > player->get_end_sample ())
620 if (player->get_type () == DOUBLE) 616 if (player->get_type () == DOUBLE)
621 { 617 {
622 if (player->get_nbits () == 8) 618 if (player->get_nbits () == 8)
623 { 619 {
624 int8_t *buffer = static_cast<int8_t *> (output); 620 int8_t *buffer = static_cast<int8_t *> (output);
625 buffer[k] = sound_l->elem (sample_number) * (pow (2.0, 7) - 1); 621 buffer[k] = sound_l.elem (sample_number) * (pow (2.0, 7) - 1);
626 buffer[k + 1] = sound_r->elem (sample_number) * (pow (2.0, 7) - 1); 622 buffer[k + 1] = sound_r.elem (sample_number) * (pow (2.0, 7) - 1);
627 } 623 }
628 else if (player->get_nbits () == 16) 624 else if (player->get_nbits () == 16)
629 { 625 {
630 int16_t *buffer = static_cast<int16_t *> (output); 626 int16_t *buffer = static_cast<int16_t *> (output);
631 buffer[k] = sound_l->elem (sample_number) * (pow (2.0, 15) - 1); 627 buffer[k] = sound_l.elem (sample_number) * (pow (2.0, 15) - 1);
632 buffer[k + 1] = sound_r->elem (sample_number) * (pow (2.0, 15) - 1); 628 buffer[k + 1] = sound_r.elem (sample_number) * (pow (2.0, 15) - 1);
633 } 629 }
634 else if (player->get_nbits () == 24) 630 else if (player->get_nbits () == 24)
635 { 631 {
636 uint8_t *buffer = static_cast<uint8_t *> (output); 632 uint8_t *buffer = static_cast<uint8_t *> (output);
637 int32_t sample_l = sound_l->elem (sample_number) * (pow (2.0, 23) - 1); 633 int32_t sample_l = sound_l.elem (sample_number) * (pow (2.0, 23) - 1);
638 int32_t sample_r = sound_r->elem (sample_number) * (pow (2.0, 23) - 1); 634 int32_t sample_r = sound_r.elem (sample_number) * (pow (2.0, 23) - 1);
639 sample_l &= 0x00ffffff; 635 sample_l &= 0x00ffffff;
640 sample_r &= 0x00ffffff; 636 sample_r &= 0x00ffffff;
641 // FIXME: Would a mask work better? 637 // FIXME: Would a mask work better?
642 uint8_t *_sample_l = reinterpret_cast<uint8_t *> (&sample_l); 638 uint8_t *_sample_l = reinterpret_cast<uint8_t *> (&sample_l);
643 uint8_t *_sample_r = reinterpret_cast<uint8_t *> (&sample_r); 639 uint8_t *_sample_r = reinterpret_cast<uint8_t *> (&sample_r);
650 } 646 }
651 } 647 }
652 else if (player->get_type () == INT8) 648 else if (player->get_type () == INT8)
653 { 649 {
654 int8_t *buffer = static_cast<int8_t *> (output); 650 int8_t *buffer = static_cast<int8_t *> (output);
655 buffer[k] = sound_l->elem (sample_number); 651 buffer[k] = sound_l.elem (sample_number);
656 buffer[k + 1] = sound_r->elem (sample_number); 652 buffer[k + 1] = sound_r.elem (sample_number);
657 } 653 }
658 else if (player->get_type () == UINT8) 654 else if (player->get_type () == UINT8)
659 { 655 {
660 uint8_t *buffer = static_cast<uint8_t *> (output); 656 uint8_t *buffer = static_cast<uint8_t *> (output);
661 buffer[k] = sound_l->elem (sample_number); 657 buffer[k] = sound_l.elem (sample_number);
662 buffer[k + 1] = sound_r->elem (sample_number); 658 buffer[k + 1] = sound_r.elem (sample_number);
663 } 659 }
664 else if (player->get_type () == INT16) 660 else if (player->get_type () == INT16)
665 { 661 {
666 int16_t *buffer = static_cast<int16_t *> (output); 662 int16_t *buffer = static_cast<int16_t *> (output);
667 buffer[k] = sound_l->elem (sample_number); 663 buffer[k] = sound_l.elem (sample_number);
668 buffer[k + 1] = sound_r->elem (sample_number); 664 buffer[k + 1] = sound_r.elem (sample_number);
669 } 665 }
670 player->set_sample_number (sample_number + 1); 666 player->set_sample_number (sample_number + 1);
671 } 667 }
672 return paContinue; 668 return paContinue;
673 } 669 }
799 audioplayer::get_y (void) 795 audioplayer::get_y (void)
800 { 796 {
801 return y; 797 return y;
802 } 798 }
803 799
804 RowVector * 800 RowVector
805 audioplayer::get_left (void) 801 audioplayer::get_left (void) const
806 { 802 {
807 return &(left); 803 return left;
808 } 804 }
809 805
810 RowVector * 806 RowVector
811 audioplayer::get_right (void) 807 audioplayer::get_right (void) const
812 { 808 {
813 return &(right); 809 return channels == 1 ? left : right;
814 } 810 }
815 811
816 void 812 void
817 audioplayer::set_fs (int fs_arg) 813 audioplayer::set_fs (int fs_arg)
818 { 814 {