Mercurial > octave-nkf
comparison libinterp/dldfcn/audiodevinfo.cc @ 19551:22e2f11424b3
avoid more warnings in audio code
* audiodevinfo.cc: Avoid unnecessary use of this-> and eliminate
shadowed variable warnings. Use initialization in class
constructors. Pass octave_value and matrix objects by const reference
instead of by value.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Fri, 02 Jan 2015 12:05:14 -0500 |
parents | fc85d9026bb6 |
children | 572b9bd43e92 |
comparison
equal
deleted
inserted
replaced
19550:fc85d9026bb6 | 19551:22e2f11424b3 |
---|---|
434 bool is_defined (void) const { return true; } | 434 bool is_defined (void) const { return true; } |
435 bool print_as_scalar (void) const { return true; } | 435 bool print_as_scalar (void) const { return true; } |
436 | 436 |
437 void init (void); | 437 void init (void); |
438 void init_fn (void); | 438 void init_fn (void); |
439 void set_y (octave_value y); | 439 void set_y (const octave_value& y); |
440 void set_y (octave_function *fn); | 440 void set_y (octave_function *fn); |
441 void set_y (std::string fn); | 441 void set_y (std::string fn); |
442 Matrix& get_y (void); | 442 Matrix& get_y (void); |
443 RowVector *get_left (void); | 443 RowVector *get_left (void); |
444 RowVector *get_right (void); | 444 RowVector *get_right (void); |
455 unsigned int get_sample_number (void); | 455 unsigned int get_sample_number (void); |
456 unsigned int get_total_samples (void); | 456 unsigned int get_total_samples (void); |
457 void set_end_sample (unsigned int sample); | 457 void set_end_sample (unsigned int sample); |
458 unsigned int get_end_sample (void); | 458 unsigned int get_end_sample (void); |
459 void reset_end_sample (void); | 459 void reset_end_sample (void); |
460 void set_tag (charMatrix tag); | 460 void set_tag (const charMatrix& tag); |
461 charMatrix get_tag (void); | 461 charMatrix get_tag (void); |
462 void set_userdata (octave_value userdata); | 462 void set_userdata (const octave_value& userdata); |
463 octave_value get_userdata (void); | 463 octave_value get_userdata (void); |
464 PaStream *get_stream (void); | 464 PaStream *get_stream (void); |
465 octave_function *octave_callback_function; | |
466 | 465 |
467 void playblocking (void); | 466 void playblocking (void); |
468 void play (void); | 467 void play (void); |
469 void pause (void); | 468 void pause (void); |
470 void resume (void); | 469 void resume (void); |
471 void stop (void); | 470 void stop (void); |
472 bool isplaying (void); | 471 bool isplaying (void); |
473 | 472 |
473 octave_function *octave_callback_function; | |
474 | |
474 private: | 475 private: |
476 int id; | |
477 int fs; | |
478 int nbits; | |
479 int channels; | |
480 unsigned int sample_number; | |
481 unsigned int end_sample; | |
482 charMatrix tag; | |
475 Matrix y; | 483 Matrix y; |
484 octave_value userdata; | |
476 RowVector left; | 485 RowVector left; |
477 RowVector right; | 486 RowVector right; |
478 charMatrix tag; | |
479 octave_value userdata; | |
480 int channels; | |
481 int fs; | |
482 int nbits; | |
483 int id; | |
484 unsigned int sample_number; | |
485 unsigned int end_sample; | |
486 PaStream *stream; | 487 PaStream *stream; |
487 PaStreamParameters output_parameters; | 488 PaStreamParameters output_parameters; |
488 audio_type type; | 489 audio_type type; |
490 | |
489 DECLARE_OCTAVE_ALLOCATOR | 491 DECLARE_OCTAVE_ALLOCATOR |
490 DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA | 492 DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA |
491 }; | 493 }; |
492 | 494 |
493 #define BUFFER_SIZE 512 | 495 #define BUFFER_SIZE 512 |
651 } | 653 } |
652 return paContinue; | 654 return paContinue; |
653 } | 655 } |
654 | 656 |
655 audioplayer::audioplayer (void) | 657 audioplayer::audioplayer (void) |
656 { | 658 : octave_callback_function (0), |
657 this->nbits = 16; | 659 id (-1), fs (0), nbits (16), channels (0), sample_number (0), |
658 this->id = -1; | 660 end_sample (-1), tag (""), y (), userdata (Matrix ()), |
659 this->sample_number = 0; | 661 left (), right (), stream (0), output_parameters (), type () |
660 this->tag = charMatrix (""); | 662 { } |
661 Matrix userdata; | |
662 this->userdata = octave_value (userdata); | |
663 this->stream = 0; | |
664 this->octave_callback_function = 0; | |
665 } | |
666 | 663 |
667 void | 664 void |
668 audioplayer::print (std::ostream& os, bool pr_as_read_syntax) const | 665 audioplayer::print (std::ostream& os, bool pr_as_read_syntax) const |
669 { | 666 { |
670 print_raw (os, pr_as_read_syntax); | 667 print_raw (os, pr_as_read_syntax); |
695 { | 692 { |
696 error ("audioplayer: No audio devices found!"); | 693 error ("audioplayer: No audio devices found!"); |
697 return; | 694 return; |
698 } | 695 } |
699 | 696 |
700 if (this->get_id () == -1) | 697 if (get_id () == -1) |
701 device = Pa_GetDefaultOutputDevice (); | 698 device = Pa_GetDefaultOutputDevice (); |
702 else | 699 else |
703 device = this->get_id (); | 700 device = get_id (); |
704 | 701 |
705 output_parameters.device = device; | 702 output_parameters.device = device; |
706 output_parameters.channelCount = 2; | 703 output_parameters.channelCount = 2; |
707 output_parameters.sampleFormat = bits_to_format (this->get_nbits ()); | 704 output_parameters.sampleFormat = bits_to_format (get_nbits ()); |
708 output_parameters.suggestedLatency = Pa_GetDeviceInfo (device)->defaultHighOutputLatency; | 705 output_parameters.suggestedLatency = Pa_GetDeviceInfo (device)->defaultHighOutputLatency; |
709 output_parameters.hostApiSpecificStreamInfo = NULL; | 706 output_parameters.hostApiSpecificStreamInfo = NULL; |
710 } | 707 } |
711 | 708 |
712 void | 709 void |
715 PaError err; | 712 PaError err; |
716 | 713 |
717 // Both of these variables are unused. Should they be | 714 // Both of these variables are unused. Should they be |
718 // eliminated or is something not yet implemented? | 715 // eliminated or is something not yet implemented? |
719 // | 716 // |
720 // int channels = this->y.rows (); | 717 // int channels = y.rows (); |
721 // RowVector *sound_l = this->get_left (); | 718 // RowVector *sound_l = get_left (); |
722 | 719 |
723 int device; | 720 int device; |
724 | 721 |
725 err = Pa_Initialize (); | 722 err = Pa_Initialize (); |
726 if (err != paNoError) | 723 if (err != paNoError) |
734 { | 731 { |
735 error ("audioplayer: No audio devices found!"); | 732 error ("audioplayer: No audio devices found!"); |
736 return; | 733 return; |
737 } | 734 } |
738 | 735 |
739 if (this->get_id () == -1) | 736 if (get_id () == -1) |
740 device = Pa_GetDefaultOutputDevice (); | 737 device = Pa_GetDefaultOutputDevice (); |
741 else | 738 else |
742 device = this->get_id (); | 739 device = get_id (); |
743 | 740 |
744 output_parameters.device = device; | 741 output_parameters.device = device; |
745 output_parameters.channelCount = 2; | 742 output_parameters.channelCount = 2; |
746 | 743 |
747 if (this->type == DOUBLE) | 744 if (type == DOUBLE) |
748 output_parameters.sampleFormat = bits_to_format (this->get_nbits ()); | 745 output_parameters.sampleFormat = bits_to_format (get_nbits ()); |
749 else if (this->type == INT8) | 746 else if (type == INT8) |
750 output_parameters.sampleFormat = paInt8; | 747 output_parameters.sampleFormat = paInt8; |
751 else if (this->type == UINT8) | 748 else if (type == UINT8) |
752 output_parameters.sampleFormat = paUInt8; | 749 output_parameters.sampleFormat = paUInt8; |
753 else if (this->type == INT16) | 750 else if (type == INT16) |
754 output_parameters.sampleFormat = paInt16; | 751 output_parameters.sampleFormat = paInt16; |
755 | 752 |
756 output_parameters.suggestedLatency = Pa_GetDeviceInfo (device)->defaultHighOutputLatency; | 753 output_parameters.suggestedLatency = Pa_GetDeviceInfo (device)->defaultHighOutputLatency; |
757 output_parameters.hostApiSpecificStreamInfo = NULL; | 754 output_parameters.hostApiSpecificStreamInfo = NULL; |
758 } | 755 } |
759 | 756 |
760 void | 757 void |
761 audioplayer::set_y (octave_value y) | 758 audioplayer::set_y (const octave_value& y_arg) |
762 { | 759 { |
763 if (y.is_int8_type ()) | 760 if (y_arg.is_int8_type ()) |
764 this->type = INT8; | 761 type = INT8; |
765 else if (y.is_uint8_type ()) | 762 else if (y_arg.is_uint8_type ()) |
766 this->type = UINT8; | 763 type = UINT8; |
767 else if (y.is_int16_type ()) | 764 else if (y_arg.is_int16_type ()) |
768 this->type = INT16; | 765 type = INT16; |
769 else | 766 else |
770 this->type = DOUBLE; | 767 type = DOUBLE; |
771 | 768 |
772 this->y = y.matrix_value (); | 769 y = y_arg.matrix_value (); |
773 if (this->y.rows () > 2) | 770 |
774 this->y = this->y.transpose (); | 771 if (y.rows () > 2) |
775 | 772 y = y.transpose (); |
776 this->channels = this->y.rows (); | 773 |
777 this->left = this->y.row (0); | 774 channels = y.rows (); |
778 if (this->channels == 2) | 775 left = y.row (0); |
779 this->right = this->y.row (1); | 776 |
780 | 777 if (channels == 2) |
781 this->reset_end_sample (); | 778 right = y.row (1); |
779 | |
780 reset_end_sample (); | |
782 } | 781 } |
783 | 782 |
784 void | 783 void |
785 audioplayer::set_y (octave_function *fn) | 784 audioplayer::set_y (octave_function *fn) |
786 { | 785 { |
787 this->octave_callback_function = fn; | 786 octave_callback_function = fn; |
788 this->channels = 2; | 787 channels = 2; |
789 this->reset_end_sample (); | 788 reset_end_sample (); |
790 } | 789 } |
791 | 790 |
792 Matrix& | 791 Matrix& |
793 audioplayer::get_y (void) | 792 audioplayer::get_y (void) |
794 { | 793 { |
795 return this->y; | 794 return y; |
796 } | 795 } |
797 | 796 |
798 RowVector * | 797 RowVector * |
799 audioplayer::get_left (void) | 798 audioplayer::get_left (void) |
800 { | 799 { |
801 return &(this->left); | 800 return &(left); |
802 } | 801 } |
803 | 802 |
804 RowVector * | 803 RowVector * |
805 audioplayer::get_right (void) | 804 audioplayer::get_right (void) |
806 { | 805 { |
807 return &(this->right); | 806 return &(right); |
808 } | 807 } |
809 | 808 |
810 void | 809 void |
811 audioplayer::set_fs (int fs) | 810 audioplayer::set_fs (int fs_arg) |
812 { | 811 { |
813 this->fs = fs; | 812 fs = fs_arg; |
814 } | 813 } |
815 | 814 |
816 int | 815 int |
817 audioplayer::get_fs (void) | 816 audioplayer::get_fs (void) |
818 { | 817 { |
819 return this->fs; | 818 return fs; |
820 } | 819 } |
821 | 820 |
822 void | 821 void |
823 audioplayer::set_nbits (int nbits) | 822 audioplayer::set_nbits (int nbits_arg) |
824 { | 823 { |
825 this->nbits = nbits; | 824 nbits = nbits_arg; |
826 } | 825 } |
827 | 826 |
828 int | 827 int |
829 audioplayer::get_nbits (void) | 828 audioplayer::get_nbits (void) |
830 { | 829 { |
831 return this->nbits; | 830 return nbits; |
832 } | 831 } |
833 | 832 |
834 void | 833 void |
835 audioplayer::set_id (int id) | 834 audioplayer::set_id (int id_arg) |
836 { | 835 { |
837 this->id = id; | 836 id = id_arg; |
838 } | 837 } |
839 | 838 |
840 int | 839 int |
841 audioplayer::get_id (void) | 840 audioplayer::get_id (void) |
842 { | 841 { |
843 return this->id; | 842 return id; |
844 } | 843 } |
845 | 844 |
846 int | 845 int |
847 audioplayer::get_channels (void) | 846 audioplayer::get_channels (void) |
848 { | 847 { |
849 return this->channels; | 848 return channels; |
850 } | 849 } |
851 | 850 |
852 audio_type | 851 audio_type |
853 audioplayer::get_type (void) | 852 audioplayer::get_type (void) |
854 { | 853 { |
855 return this->type; | 854 return type; |
856 } | 855 } |
857 | 856 |
858 void | 857 void |
859 audioplayer::set_sample_number (unsigned int sample_number) | 858 audioplayer::set_sample_number (unsigned int sample_number_arg) |
860 { | 859 { |
861 this->sample_number = sample_number; | 860 sample_number = sample_number_arg; |
862 } | 861 } |
863 | 862 |
864 unsigned int | 863 unsigned int |
865 audioplayer::get_sample_number (void) | 864 audioplayer::get_sample_number (void) |
866 { | 865 { |
867 return this->sample_number; | 866 return sample_number; |
868 } | 867 } |
869 | 868 |
870 unsigned int | 869 unsigned int |
871 audioplayer::get_total_samples (void) | 870 audioplayer::get_total_samples (void) |
872 { | 871 { |
873 return this->left.length (); | 872 return left.length (); |
874 } | 873 } |
875 | 874 |
876 void | 875 void |
877 audioplayer::set_end_sample (unsigned int end_sample) | 876 audioplayer::set_end_sample (unsigned int end_sample_arg) |
878 { | 877 { |
879 this->end_sample = end_sample; | 878 end_sample = end_sample_arg; |
880 } | 879 } |
881 | 880 |
882 unsigned int | 881 unsigned int |
883 audioplayer::get_end_sample (void) | 882 audioplayer::get_end_sample (void) |
884 { | 883 { |
885 return this->end_sample; | 884 return end_sample; |
886 } | 885 } |
887 | 886 |
888 void | 887 void |
889 audioplayer::reset_end_sample (void) | 888 audioplayer::reset_end_sample (void) |
890 { | 889 { |
891 this->set_end_sample (this->left.length ()); | 890 set_end_sample (left.length ()); |
892 } | 891 } |
893 | 892 |
894 void | 893 void |
895 audioplayer::set_tag (charMatrix tag) | 894 audioplayer::set_tag (const charMatrix& tag_arg) |
896 { | 895 { |
897 this->tag = tag; | 896 tag = tag_arg; |
898 } | 897 } |
899 | 898 |
900 charMatrix | 899 charMatrix |
901 audioplayer::get_tag (void) | 900 audioplayer::get_tag (void) |
902 { | 901 { |
903 return this->tag; | 902 return tag; |
904 } | 903 } |
905 | 904 |
906 void | 905 void |
907 audioplayer::set_userdata (octave_value userdata) | 906 audioplayer::set_userdata (const octave_value& userdata_arg) |
908 { | 907 { |
909 this->userdata = userdata; | 908 userdata = userdata_arg; |
910 } | 909 } |
911 | 910 |
912 octave_value | 911 octave_value |
913 audioplayer::get_userdata (void) | 912 audioplayer::get_userdata (void) |
914 { | 913 { |
915 return this->userdata; | 914 return userdata; |
916 } | 915 } |
917 | 916 |
918 void | 917 void |
919 audioplayer::playblocking (void) | 918 audioplayer::playblocking (void) |
920 { | 919 { |
921 if (this->get_stream ()) | 920 if (get_stream ()) |
922 this->stop (); | 921 stop (); |
923 | 922 |
924 PaError err; | 923 PaError err; |
925 uint32_t buffer[BUFFER_SIZE * 2]; | 924 uint32_t buffer[BUFFER_SIZE * 2]; |
926 err = Pa_OpenStream (&stream, NULL, &(this->output_parameters), this->get_fs (), BUFFER_SIZE, paClipOff, NULL, NULL); | 925 err = Pa_OpenStream (&stream, NULL, &(output_parameters), get_fs (), BUFFER_SIZE, paClipOff, NULL, NULL); |
927 if (err != paNoError) | 926 if (err != paNoError) |
928 { | 927 { |
929 error ("audioplayer: Error opening audio playback stream"); | 928 error ("audioplayer: Error opening audio playback stream"); |
930 return; | 929 return; |
931 } | 930 } |
936 error ("audioplayer: Error starting audio playback stream"); | 935 error ("audioplayer: Error starting audio playback stream"); |
937 return; | 936 return; |
938 } | 937 } |
939 | 938 |
940 unsigned int start, end; | 939 unsigned int start, end; |
941 start = this->get_sample_number (); | 940 start = get_sample_number (); |
942 end = this->get_end_sample (); | 941 end = get_end_sample (); |
943 for (unsigned int i = start; i < end; i += BUFFER_SIZE) | 942 for (unsigned int i = start; i < end; i += BUFFER_SIZE) |
944 { | 943 { |
945 if (this->octave_callback_function != 0) | 944 if (octave_callback_function != 0) |
946 octave_play_callback (0, buffer, BUFFER_SIZE, 0, 0, this); | 945 octave_play_callback (0, buffer, BUFFER_SIZE, 0, 0, this); |
947 else | 946 else |
948 portaudio_play_callback (0, buffer, BUFFER_SIZE, 0, 0, this); | 947 portaudio_play_callback (0, buffer, BUFFER_SIZE, 0, 0, this); |
949 | 948 |
950 err = Pa_WriteStream (stream, buffer, BUFFER_SIZE); | 949 err = Pa_WriteStream (stream, buffer, BUFFER_SIZE); |
963 error ("audioplayer: Error closing audio playback stream"); | 962 error ("audioplayer: Error closing audio playback stream"); |
964 return; | 963 return; |
965 } | 964 } |
966 | 965 |
967 stream = 0; | 966 stream = 0; |
968 this->set_sample_number (0); | 967 set_sample_number (0); |
969 this->reset_end_sample (); | 968 reset_end_sample (); |
970 } | 969 } |
971 | 970 |
972 void | 971 void |
973 audioplayer::play (void) | 972 audioplayer::play (void) |
974 { | 973 { |
975 if (this->get_stream ()) | 974 if (get_stream ()) |
976 this->stop (); | 975 stop (); |
977 | 976 |
978 PaError err; | 977 PaError err; |
979 if (this->octave_callback_function != 0) | 978 if (octave_callback_function != 0) |
980 err = Pa_OpenStream (&stream, NULL, &(this->output_parameters), | 979 err = Pa_OpenStream (&stream, NULL, &(output_parameters), |
981 this->get_fs (), BUFFER_SIZE, paClipOff, | 980 get_fs (), BUFFER_SIZE, paClipOff, |
982 octave_play_callback, this); | 981 octave_play_callback, this); |
983 else | 982 else |
984 err = Pa_OpenStream (&stream, NULL, &(this->output_parameters), | 983 err = Pa_OpenStream (&stream, NULL, &(output_parameters), |
985 this->get_fs (), BUFFER_SIZE, paClipOff, | 984 get_fs (), BUFFER_SIZE, paClipOff, |
986 portaudio_play_callback, this); | 985 portaudio_play_callback, this); |
987 | 986 |
988 if (err != paNoError) | 987 if (err != paNoError) |
989 { | 988 { |
990 error ("audioplayer: Error opening audio playback stream"); | 989 error ("audioplayer: Error opening audio playback stream"); |
1000 } | 999 } |
1001 | 1000 |
1002 void | 1001 void |
1003 audioplayer::pause (void) | 1002 audioplayer::pause (void) |
1004 { | 1003 { |
1005 if (this->get_stream () == 0) | 1004 if (get_stream () == 0) |
1006 return; | 1005 return; |
1007 | 1006 |
1008 PaError err; | 1007 PaError err; |
1009 err = Pa_StopStream (stream); | 1008 err = Pa_StopStream (stream); |
1010 if (err != paNoError) | 1009 if (err != paNoError) |
1015 } | 1014 } |
1016 | 1015 |
1017 void | 1016 void |
1018 audioplayer::resume (void) | 1017 audioplayer::resume (void) |
1019 { | 1018 { |
1020 if (this->get_stream () == 0) | 1019 if (get_stream () == 0) |
1021 return; | 1020 return; |
1022 | 1021 |
1023 PaError err; | 1022 PaError err; |
1024 err = Pa_StartStream (stream); | 1023 err = Pa_StartStream (stream); |
1025 if (err != paNoError) | 1024 if (err != paNoError) |
1030 } | 1029 } |
1031 | 1030 |
1032 PaStream * | 1031 PaStream * |
1033 audioplayer::get_stream (void) | 1032 audioplayer::get_stream (void) |
1034 { | 1033 { |
1035 return this->stream; | 1034 return stream; |
1036 } | 1035 } |
1037 | 1036 |
1038 void | 1037 void |
1039 audioplayer::stop (void) | 1038 audioplayer::stop (void) |
1040 { | 1039 { |
1041 if (this->get_stream () == 0) | 1040 if (get_stream () == 0) |
1042 return; | 1041 return; |
1043 | 1042 |
1044 PaError err; | 1043 PaError err; |
1045 this->set_sample_number (0); | 1044 set_sample_number (0); |
1046 this->reset_end_sample (); | 1045 reset_end_sample (); |
1047 if (not Pa_IsStreamStopped (this->get_stream ())) | 1046 if (not Pa_IsStreamStopped (get_stream ())) |
1048 { | 1047 { |
1049 err = Pa_AbortStream (this->get_stream ()); | 1048 err = Pa_AbortStream (get_stream ()); |
1050 if (err != paNoError) | 1049 if (err != paNoError) |
1051 { | 1050 { |
1052 error ("audioplayer: Error stopping audio playback stream"); | 1051 error ("audioplayer: Error stopping audio playback stream"); |
1053 return; | 1052 return; |
1054 } | 1053 } |
1055 } | 1054 } |
1056 | 1055 |
1057 err = Pa_CloseStream (this->get_stream ()); | 1056 err = Pa_CloseStream (get_stream ()); |
1058 if (err != paNoError) | 1057 if (err != paNoError) |
1059 { | 1058 { |
1060 error ("audioplayer: Error closing audio playback stream"); | 1059 error ("audioplayer: Error closing audio playback stream"); |
1061 return; | 1060 return; |
1062 } | 1061 } |
1065 } | 1064 } |
1066 | 1065 |
1067 bool | 1066 bool |
1068 audioplayer::isplaying (void) | 1067 audioplayer::isplaying (void) |
1069 { | 1068 { |
1070 if (this->get_stream () == 0) | 1069 if (get_stream () == 0) |
1071 return false; | 1070 return false; |
1072 | 1071 |
1073 PaError err; | 1072 PaError err; |
1074 err = Pa_IsStreamActive (stream); | 1073 err = Pa_IsStreamActive (stream); |
1075 if (err != 0 && err != 1) | 1074 if (err != 0 && err != 1) |
1113 unsigned int get_sample_number (void); | 1112 unsigned int get_sample_number (void); |
1114 unsigned int get_total_samples (void); | 1113 unsigned int get_total_samples (void); |
1115 void set_end_sample (unsigned int sample); | 1114 void set_end_sample (unsigned int sample); |
1116 unsigned int get_end_sample (void); | 1115 unsigned int get_end_sample (void); |
1117 void reset_end_sample (void); | 1116 void reset_end_sample (void); |
1118 void set_tag (charMatrix tag); | 1117 void set_tag (const charMatrix& tag); |
1119 charMatrix get_tag (void); | 1118 charMatrix get_tag (void); |
1120 void set_userdata (octave_value userdata); | 1119 void set_userdata (const octave_value& userdata); |
1121 octave_value get_userdata (void); | 1120 octave_value get_userdata (void); |
1122 PaStream *get_stream (void); | 1121 PaStream *get_stream (void); |
1123 octave_function *octave_callback_function; | |
1124 | 1122 |
1125 octave_value getaudiodata (void); | 1123 octave_value getaudiodata (void); |
1126 audioplayer *getplayer (void); | 1124 audioplayer *getplayer (void); |
1127 bool isrecording (void); | 1125 bool isrecording (void); |
1128 audioplayer play (void); | 1126 audioplayer play (void); |
1131 void pause (void); | 1129 void pause (void); |
1132 void resume (void); | 1130 void resume (void); |
1133 void stop (void); | 1131 void stop (void); |
1134 void append (float sample_l, float sample_r); | 1132 void append (float sample_l, float sample_r); |
1135 | 1133 |
1134 octave_function *octave_callback_function; | |
1135 | |
1136 private: | 1136 private: |
1137 int id; | |
1138 int fs; | |
1139 int nbits; | |
1140 int channels; | |
1141 unsigned int sample_number; | |
1142 unsigned int end_sample; | |
1143 charMatrix tag; | |
1137 Matrix y; | 1144 Matrix y; |
1145 octave_value userdata; | |
1138 std::vector<float> left; | 1146 std::vector<float> left; |
1139 std::vector<float> right; | 1147 std::vector<float> right; |
1140 charMatrix tag; | |
1141 octave_value userdata; | |
1142 int channels; | |
1143 int fs; | |
1144 int nbits; | |
1145 int id; | |
1146 unsigned int sample_number; | |
1147 unsigned int end_sample; | |
1148 PaStream *stream; | 1148 PaStream *stream; |
1149 PaStreamParameters input_parameters; | 1149 PaStreamParameters input_parameters; |
1150 audio_type type; | 1150 audio_type type; |
1151 | |
1151 DECLARE_OCTAVE_ALLOCATOR | 1152 DECLARE_OCTAVE_ALLOCATOR |
1152 DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA | 1153 DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA |
1153 }; | 1154 }; |
1154 | 1155 |
1155 DEFINE_OCTAVE_ALLOCATOR (audiorecorder); | 1156 DEFINE_OCTAVE_ALLOCATOR (audiorecorder); |
1271 | 1272 |
1272 return paContinue; | 1273 return paContinue; |
1273 } | 1274 } |
1274 | 1275 |
1275 audiorecorder::audiorecorder (void) | 1276 audiorecorder::audiorecorder (void) |
1276 { | 1277 : octave_callback_function (0), |
1277 this->id = -1; | 1278 id (-1), fs (44100), nbits (16), channels (2), sample_number (0), |
1278 this->sample_number = 0; | 1279 end_sample (-1), tag (""), y (), userdata (Matrix ()), |
1279 this->channels = 1; | 1280 left (), right (), stream (0), input_parameters (), type () |
1280 this->tag = charMatrix (""); | 1281 { } |
1281 Matrix userdata; | |
1282 this->userdata = octave_value (userdata); | |
1283 this->stream = 0; | |
1284 this->end_sample = -1; | |
1285 this->set_fs (44100); | |
1286 this->set_nbits (16); | |
1287 this->set_channels (2); | |
1288 this->octave_callback_function = 0; | |
1289 } | |
1290 | 1282 |
1291 void | 1283 void |
1292 audiorecorder::print (std::ostream& os, bool pr_as_read_syntax) const | 1284 audiorecorder::print (std::ostream& os, bool pr_as_read_syntax) const |
1293 { | 1285 { |
1294 print_raw (os, pr_as_read_syntax); | 1286 print_raw (os, pr_as_read_syntax); |
1318 { | 1310 { |
1319 error ("audiorecorder: No audio devices found!"); | 1311 error ("audiorecorder: No audio devices found!"); |
1320 return; | 1312 return; |
1321 } | 1313 } |
1322 | 1314 |
1323 if (this->get_id () == -1) | 1315 if (get_id () == -1) |
1324 device = Pa_GetDefaultInputDevice (); | 1316 device = Pa_GetDefaultInputDevice (); |
1325 else | 1317 else |
1326 device = this->get_id (); | 1318 device = get_id (); |
1327 | 1319 |
1328 this->input_parameters.device = device; | 1320 input_parameters.device = device; |
1329 this->input_parameters.channelCount = this->get_channels (); | 1321 input_parameters.channelCount = get_channels (); |
1330 this->input_parameters.sampleFormat = bits_to_format (this->get_nbits ()); | 1322 input_parameters.sampleFormat = bits_to_format (get_nbits ()); |
1331 this->input_parameters.suggestedLatency = Pa_GetDeviceInfo (device)->defaultHighInputLatency; | 1323 input_parameters.suggestedLatency = Pa_GetDeviceInfo (device)->defaultHighInputLatency; |
1332 this->input_parameters.hostApiSpecificStreamInfo = NULL; | 1324 input_parameters.hostApiSpecificStreamInfo = NULL; |
1333 } | 1325 } |
1334 | 1326 |
1335 void | 1327 void |
1336 audiorecorder::set_fs (int fs) | 1328 audiorecorder::set_fs (int fs_arg) |
1337 { | 1329 { |
1338 this->fs = fs; | 1330 fs = fs_arg; |
1339 } | 1331 } |
1340 | 1332 |
1341 int | 1333 int |
1342 audiorecorder::get_fs (void) | 1334 audiorecorder::get_fs (void) |
1343 { | 1335 { |
1344 return this->fs; | 1336 return fs; |
1345 } | 1337 } |
1346 | 1338 |
1347 void | 1339 void |
1348 audiorecorder::set_nbits (int nbits) | 1340 audiorecorder::set_nbits (int nbits_arg) |
1349 { | 1341 { |
1350 this->nbits = nbits; | 1342 nbits = nbits_arg; |
1351 } | 1343 } |
1352 | 1344 |
1353 int | 1345 int |
1354 audiorecorder::get_nbits (void) | 1346 audiorecorder::get_nbits (void) |
1355 { | 1347 { |
1356 return this->nbits; | 1348 return nbits; |
1357 } | 1349 } |
1358 | 1350 |
1359 void | 1351 void |
1360 audiorecorder::set_id (int id) | 1352 audiorecorder::set_id (int id_arg) |
1361 { | 1353 { |
1362 this->id = id; | 1354 id = id_arg; |
1363 } | 1355 } |
1364 | 1356 |
1365 int | 1357 int |
1366 audiorecorder::get_id (void) | 1358 audiorecorder::get_id (void) |
1367 { | 1359 { |
1368 return this->id; | 1360 return id; |
1369 } | 1361 } |
1370 | 1362 |
1371 void | 1363 void |
1372 audiorecorder::set_channels (int channels) | 1364 audiorecorder::set_channels (int channels_arg) |
1373 { | 1365 { |
1374 assert (channels == 1 || channels == 2); | 1366 assert (channels_arg == 1 || channels_arg == 2); |
1375 this->channels = channels; | 1367 channels = channels_arg; |
1376 } | 1368 } |
1377 | 1369 |
1378 int | 1370 int |
1379 audiorecorder::get_channels (void) | 1371 audiorecorder::get_channels (void) |
1380 { | 1372 { |
1381 return this->channels; | 1373 return channels; |
1382 } | 1374 } |
1383 | 1375 |
1384 audio_type | 1376 audio_type |
1385 audiorecorder::get_type (void) | 1377 audiorecorder::get_type (void) |
1386 { | 1378 { |
1387 return this->type; | 1379 return type; |
1388 } | 1380 } |
1389 | 1381 |
1390 void | 1382 void |
1391 audiorecorder::set_sample_number (unsigned int sample_number) | 1383 audiorecorder::set_sample_number (unsigned int sample_number_arg) |
1392 { | 1384 { |
1393 this->sample_number = sample_number; | 1385 sample_number = sample_number_arg; |
1394 } | 1386 } |
1395 | 1387 |
1396 unsigned int | 1388 unsigned int |
1397 audiorecorder::get_sample_number (void) | 1389 audiorecorder::get_sample_number (void) |
1398 { | 1390 { |
1399 return this->sample_number; | 1391 return sample_number; |
1400 } | 1392 } |
1401 | 1393 |
1402 unsigned int | 1394 unsigned int |
1403 audiorecorder::get_total_samples (void) | 1395 audiorecorder::get_total_samples (void) |
1404 { | 1396 { |
1405 return this->left.size (); | 1397 return left.size (); |
1406 } | 1398 } |
1407 | 1399 |
1408 void | 1400 void |
1409 audiorecorder::set_end_sample (unsigned int end_sample) | 1401 audiorecorder::set_end_sample (unsigned int end_sample_arg) |
1410 { | 1402 { |
1411 this->end_sample = end_sample; | 1403 end_sample = end_sample_arg; |
1412 } | 1404 } |
1413 | 1405 |
1414 unsigned int | 1406 unsigned int |
1415 audiorecorder::get_end_sample (void) | 1407 audiorecorder::get_end_sample (void) |
1416 { | 1408 { |
1417 return this->end_sample; | 1409 return end_sample; |
1418 } | 1410 } |
1419 | 1411 |
1420 void | 1412 void |
1421 audiorecorder::reset_end_sample (void) | 1413 audiorecorder::reset_end_sample (void) |
1422 { | 1414 { |
1423 this->set_end_sample (this->left.size ()); | 1415 set_end_sample (left.size ()); |
1424 } | 1416 } |
1425 | 1417 |
1426 void | 1418 void |
1427 audiorecorder::set_tag (charMatrix tag) | 1419 audiorecorder::set_tag (const charMatrix& tag_arg) |
1428 { | 1420 { |
1429 this->tag = tag; | 1421 tag = tag_arg; |
1430 } | 1422 } |
1431 | 1423 |
1432 charMatrix | 1424 charMatrix |
1433 audiorecorder::get_tag (void) | 1425 audiorecorder::get_tag (void) |
1434 { | 1426 { |
1435 return this->tag; | 1427 return tag; |
1436 } | 1428 } |
1437 | 1429 |
1438 void | 1430 void |
1439 audiorecorder::set_userdata (octave_value userdata) | 1431 audiorecorder::set_userdata (const octave_value& userdata_arg) |
1440 { | 1432 { |
1441 this->userdata = userdata; | 1433 userdata = userdata_arg; |
1442 } | 1434 } |
1443 | 1435 |
1444 octave_value | 1436 octave_value |
1445 audiorecorder::get_userdata (void) | 1437 audiorecorder::get_userdata (void) |
1446 { | 1438 { |
1447 return this->userdata; | 1439 return userdata; |
1448 } | 1440 } |
1449 | 1441 |
1450 octave_value | 1442 octave_value |
1451 audiorecorder::getaudiodata (void) | 1443 audiorecorder::getaudiodata (void) |
1452 { | 1444 { |
1453 Matrix audio (2, this->left.size ()); | 1445 Matrix audio (2, left.size ()); |
1454 for (unsigned int i = 0; i < this->left.size (); i++) | 1446 for (unsigned int i = 0; i < left.size (); i++) |
1455 { | 1447 { |
1456 audio(0, i) = this->left[i]; | 1448 audio(0, i) = left[i]; |
1457 audio(1, i) = this->right[i]; | 1449 audio(1, i) = right[i]; |
1458 } | 1450 } |
1459 return octave_value (audio); | 1451 return octave_value (audio); |
1460 } | 1452 } |
1461 | 1453 |
1462 audioplayer * | 1454 audioplayer * |
1463 audiorecorder::getplayer (void) | 1455 audiorecorder::getplayer (void) |
1464 { | 1456 { |
1465 audioplayer *player = new audioplayer (); | 1457 audioplayer *player = new audioplayer (); |
1466 player->set_y (this->getaudiodata ()); | 1458 player->set_y (getaudiodata ()); |
1467 player->set_fs (this->get_fs ()); | 1459 player->set_fs (get_fs ()); |
1468 player->set_nbits (this->get_nbits ()); | 1460 player->set_nbits (get_nbits ()); |
1469 player->init (); | 1461 player->init (); |
1470 return player; | 1462 return player; |
1471 } | 1463 } |
1472 | 1464 |
1473 bool | 1465 bool |
1474 audiorecorder::isrecording (void) | 1466 audiorecorder::isrecording (void) |
1475 { | 1467 { |
1476 if (this->get_stream () == 0) | 1468 if (get_stream () == 0) |
1477 return false; | 1469 return false; |
1478 | 1470 |
1479 PaError err; | 1471 PaError err; |
1480 err = Pa_IsStreamActive (stream); | 1472 err = Pa_IsStreamActive (stream); |
1481 if (err != 0 && err != 1) | 1473 if (err != 0 && err != 1) |
1488 } | 1480 } |
1489 | 1481 |
1490 void | 1482 void |
1491 audiorecorder::record (void) | 1483 audiorecorder::record (void) |
1492 { | 1484 { |
1493 if (this->get_stream ()) | 1485 if (get_stream ()) |
1494 this->stop (); | 1486 stop (); |
1495 | 1487 |
1496 this->left.clear (); | 1488 left.clear (); |
1497 this->right.clear (); | 1489 right.clear (); |
1498 PaError err; | 1490 PaError err; |
1499 if (this->octave_callback_function != 0) | 1491 if (octave_callback_function != 0) |
1500 { | 1492 { |
1501 err = Pa_OpenStream (&stream, &(this->input_parameters), NULL, | 1493 err = Pa_OpenStream (&stream, &(input_parameters), NULL, |
1502 this->get_fs (), BUFFER_SIZE, paClipOff, | 1494 get_fs (), BUFFER_SIZE, paClipOff, |
1503 octave_record_callback, this); | 1495 octave_record_callback, this); |
1504 } | 1496 } |
1505 else | 1497 else |
1506 { | 1498 { |
1507 err = Pa_OpenStream (&stream, &(this->input_parameters), NULL, | 1499 err = Pa_OpenStream (&stream, &(input_parameters), NULL, |
1508 this->get_fs (), BUFFER_SIZE, paClipOff, | 1500 get_fs (), BUFFER_SIZE, paClipOff, |
1509 portaudio_record_callback, this); | 1501 portaudio_record_callback, this); |
1510 } | 1502 } |
1511 if (err != paNoError) | 1503 if (err != paNoError) |
1512 { | 1504 { |
1513 error ("audiorecorder: Error opening audio recording stream"); | 1505 error ("audiorecorder: Error opening audio recording stream"); |
1522 } | 1514 } |
1523 | 1515 |
1524 void | 1516 void |
1525 audiorecorder::recordblocking (float seconds) | 1517 audiorecorder::recordblocking (float seconds) |
1526 { | 1518 { |
1527 if (this->get_stream ()) | 1519 if (get_stream ()) |
1528 this->stop (); | 1520 stop (); |
1529 | 1521 |
1530 this->left.clear (); | 1522 left.clear (); |
1531 this->right.clear (); | 1523 right.clear (); |
1532 | 1524 |
1533 PaError err; | 1525 PaError err; |
1534 err = Pa_OpenStream (&stream, &(this->input_parameters), NULL, | 1526 err = Pa_OpenStream (&stream, &(input_parameters), NULL, |
1535 this->get_fs (), BUFFER_SIZE, paClipOff, NULL, this); | 1527 get_fs (), BUFFER_SIZE, paClipOff, NULL, this); |
1536 if (err != paNoError) | 1528 if (err != paNoError) |
1537 { | 1529 { |
1538 error ("audiorecorder: Error opening audio recording stream"); | 1530 error ("audiorecorder: Error opening audio recording stream"); |
1539 return; | 1531 return; |
1540 } | 1532 } |
1544 { | 1536 { |
1545 error ("audiorecorder: Error starting audio recording stream"); | 1537 error ("audiorecorder: Error starting audio recording stream"); |
1546 return; | 1538 return; |
1547 } | 1539 } |
1548 | 1540 |
1549 unsigned int frames = seconds * this->get_fs (); | 1541 unsigned int frames = seconds * get_fs (); |
1550 uint8_t buffer[BUFFER_SIZE * 2 * 3]; | 1542 uint8_t buffer[BUFFER_SIZE * 2 * 3]; |
1551 for (unsigned long i = 0; i < frames / BUFFER_SIZE; i++) | 1543 for (unsigned long i = 0; i < frames / BUFFER_SIZE; i++) |
1552 { | 1544 { |
1553 Pa_ReadStream (this->get_stream (), buffer, BUFFER_SIZE); | 1545 Pa_ReadStream (get_stream (), buffer, BUFFER_SIZE); |
1554 if (this->octave_callback_function != 0) | 1546 if (octave_callback_function != 0) |
1555 octave_record_callback (buffer, NULL, BUFFER_SIZE, 0, 0, this); | 1547 octave_record_callback (buffer, NULL, BUFFER_SIZE, 0, 0, this); |
1556 else | 1548 else |
1557 portaudio_record_callback (buffer, NULL, BUFFER_SIZE, 0, 0, this); | 1549 portaudio_record_callback (buffer, NULL, BUFFER_SIZE, 0, 0, this); |
1558 } | 1550 } |
1559 } | 1551 } |
1560 | 1552 |
1561 void | 1553 void |
1562 audiorecorder::pause (void) | 1554 audiorecorder::pause (void) |
1563 { | 1555 { |
1564 if (this->get_stream () == 0) | 1556 if (get_stream () == 0) |
1565 return; | 1557 return; |
1566 | 1558 |
1567 PaError err; | 1559 PaError err; |
1568 err = Pa_StopStream (stream); | 1560 err = Pa_StopStream (stream); |
1569 if (err != paNoError) | 1561 if (err != paNoError) |
1574 } | 1566 } |
1575 | 1567 |
1576 void | 1568 void |
1577 audiorecorder::resume (void) | 1569 audiorecorder::resume (void) |
1578 { | 1570 { |
1579 if (this->get_stream () == 0) | 1571 if (get_stream () == 0) |
1580 return; | 1572 return; |
1581 | 1573 |
1582 PaError err; | 1574 PaError err; |
1583 err = Pa_StartStream (stream); | 1575 err = Pa_StartStream (stream); |
1584 if (err != paNoError) | 1576 if (err != paNoError) |
1589 } | 1581 } |
1590 | 1582 |
1591 void | 1583 void |
1592 audiorecorder::stop (void) | 1584 audiorecorder::stop (void) |
1593 { | 1585 { |
1594 if (this->get_stream () == 0) | 1586 if (get_stream () == 0) |
1595 return; | 1587 return; |
1596 | 1588 |
1597 PaError err; | 1589 PaError err; |
1598 if (not Pa_IsStreamStopped (this->get_stream ())) | 1590 if (not Pa_IsStreamStopped (get_stream ())) |
1599 { | 1591 { |
1600 err = Pa_AbortStream (this->get_stream ()); | 1592 err = Pa_AbortStream (get_stream ()); |
1601 if (err != paNoError) | 1593 if (err != paNoError) |
1602 { | 1594 { |
1603 error ("audioplayer: Error stopping audio playback stream"); | 1595 error ("audioplayer: Error stopping audio playback stream"); |
1604 return; | 1596 return; |
1605 } | 1597 } |
1610 { | 1602 { |
1611 error ("audiorecorder: Error closing audio recording stream"); | 1603 error ("audiorecorder: Error closing audio recording stream"); |
1612 return; | 1604 return; |
1613 } | 1605 } |
1614 | 1606 |
1615 this->set_sample_number (0); | 1607 set_sample_number (0); |
1616 this->reset_end_sample (); | 1608 reset_end_sample (); |
1617 stream = 0; | 1609 stream = 0; |
1618 } | 1610 } |
1619 | 1611 |
1620 void | 1612 void |
1621 audiorecorder::append (float sample_l, float sample_r) | 1613 audiorecorder::append (float sample_l, float sample_r) |
1622 { | 1614 { |
1623 this->left.push_back (sample_l); | 1615 left.push_back (sample_l); |
1624 this->right.push_back (sample_r); | 1616 right.push_back (sample_r); |
1625 this->set_sample_number (this->get_sample_number () + 1); | 1617 set_sample_number (get_sample_number () + 1); |
1626 } | 1618 } |
1627 | 1619 |
1628 PaStream * | 1620 PaStream * |
1629 audiorecorder::get_stream (void) | 1621 audiorecorder::get_stream (void) |
1630 { | 1622 { |
1631 return this->stream; | 1623 return stream; |
1632 } | 1624 } |
1633 | 1625 |
1634 DEFUN_DLD (__recorder_audiorecorder__, args, , | 1626 DEFUN_DLD (__recorder_audiorecorder__, args, , |
1635 "-*- texinfo -*-\n\ | 1627 "-*- texinfo -*-\n\ |
1636 @deftypefn {Loadable Function} {@var{recorder} =} __recorder_audiorecorder__ (@var{fs}, @var{nbits}, @var{channels})\n\ | 1628 @deftypefn {Loadable Function} {@var{recorder} =} __recorder_audiorecorder__ (@var{fs}, @var{nbits}, @var{channels})\n\ |