@@ -473,7 +473,7 @@ exit:
473
473
audio_i2s.in ->stopRx ();
474
474
audio_i2s_mp3.mic_stop = 0 ;
475
475
audio_i2s_mp3.mic_error = error;
476
- AddLog (LOG_LEVEL_INFO, PSTR (" mp3task result code: %d" ), error);
476
+ AddLog (LOG_LEVEL_INFO, PSTR (" I2S: mp3task result code: %d" ), error);
477
477
audio_i2s_mp3.mic_task_handle = 0 ;
478
478
audio_i2s_mp3.recdur = 0 ;
479
479
audio_i2s_mp3.stream_active = 0 ;
@@ -484,6 +484,15 @@ exit:
484
484
int32_t I2sRecordShine (char *path) {
485
485
esp_err_t err = ESP_OK;
486
486
487
+ switch (audio_i2s.Settings ->rx .sample_rate ){
488
+ case 32000 : case 48000 : case 44100 :
489
+ break ; // supported
490
+ default :
491
+ AddLog (LOG_LEVEL_INFO, PSTR (" I2S: unsupported sample rate for MP3 encoding: %d" ), audio_i2s.Settings ->rx .sample_rate );
492
+ return -1 ;
493
+ }
494
+ AddLog (LOG_LEVEL_INFO, PSTR (" I2S: accepted sample rate for MP3 encoding: %d" ), audio_i2s.Settings ->rx .sample_rate );
495
+
487
496
#ifdef USE_I2S_MP3
488
497
if (audio_i2s_mp3.decoder || audio_i2s_mp3.mp3 ) return 0 ;
489
498
#endif
@@ -735,8 +744,8 @@ void I2sInit(void) {
735
744
}
736
745
if (init_tx_ok) { audio_i2s.out = i2s; }
737
746
if (init_rx_ok) { audio_i2s.in = i2s; }
738
- audio_i2s.Settings ->sys .tx = init_tx_ok;
739
- audio_i2s.Settings ->sys .rx = init_rx_ok;
747
+ audio_i2s.Settings ->sys .tx | = init_tx_ok; // Do not set to zero id already configured on another channnel
748
+ audio_i2s.Settings ->sys .rx | = init_rx_ok;
740
749
if (init_tx_ok && init_rx_ok) { audio_i2s.Settings ->sys .duplex = true ; }
741
750
742
751
// if intput and output are configured, don't proceed with other IS2 ports
@@ -750,11 +759,11 @@ void I2sInit(void) {
750
759
if (audio_i2s.out ) { audio_i2s.out ->setExclusive (exclusive); }
751
760
if (audio_i2s.in ) { audio_i2s.in ->setExclusive (exclusive); }
752
761
753
- if (audio_i2s.out != nullptr ){
754
- audio_i2s.out ->SetGain (((float )audio_i2s.Settings ->tx .gain / 100.0 ) * 4.0 );
755
- audio_i2s.out ->beginTx (); // TODO - useful?
756
- audio_i2s.out ->stopTx ();
757
- }
762
+ // if(audio_i2s.out != nullptr){
763
+ // audio_i2s.out->SetGain(((float)( audio_i2s.Settings->tx.gain + 1) / 100.0));
764
+ // audio_i2s.out->beginTx(); // TODO - useful?
765
+ // audio_i2s.out->stopTx();
766
+ // }
758
767
#ifdef USE_I2S_MP3
759
768
audio_i2s_mp3.mp3ram = nullptr ;
760
769
if (audio_i2s.Settings ->sys .mp3_preallocate == 1 ){
@@ -784,11 +793,21 @@ int32_t I2SPrepareTx(void) {
784
793
delay (1 );
785
794
}
786
795
}
796
+
797
+ if (audio_i2s_mp3.mic_task_handle ) {
798
+ audio_i2s_mp3.mic_stop = 1 ;
799
+ while (audio_i2s_mp3.mic_stop ) {
800
+ delay (1 );
801
+ }
802
+ }
787
803
788
804
AddLog (LOG_LEVEL_DEBUG, " I2S: I2SPrepareTx out=%p" , audio_i2s.out );
789
805
if (!audio_i2s.out ) { return I2S_ERR_OUTPUT_NOT_CONFIGURED; }
790
806
791
807
if (!audio_i2s.out ->beginTx ()) { return I2S_ERR_TX_FAILED; }
808
+
809
+ audio_i2s.out ->SetGain (((float )(audio_i2s.Settings ->tx .gain + 1 )/ 100.0 ));
810
+
792
811
return I2S_OK;
793
812
}
794
813
@@ -813,21 +832,20 @@ int32_t I2SPrepareRx(void) {
813
832
814
833
#if defined(USE_I2S_MP3) || defined(USE_I2S_WEBRADIO)
815
834
void I2sMp3Task (void *arg) {
816
- while (1 ) {
817
- while (audio_i2s_mp3.mp3 ->isRunning ()) {
818
- if (!audio_i2s_mp3.mp3 ->loop ()) {
819
- audio_i2s_mp3.mp3 ->stop ();
820
- mp3_delete ();
821
- audio_i2s.out ->stop ();
822
- if (audio_i2s_mp3.mp3_task_handle ) {
823
- vTaskDelete (audio_i2s_mp3.mp3_task_handle );
824
- audio_i2s_mp3.mp3_task_handle = 0 ;
825
- }
826
- // mp3_task_handle=nullptr;
827
- }
828
- vTaskDelay (pdMS_TO_TICKS (1 ));
835
+ audio_i2s_mp3.task_running = true ;
836
+ while (audio_i2s_mp3.mp3 ->isRunning () && audio_i2s_mp3.task_running ) {
837
+ if (!audio_i2s_mp3.mp3 ->loop ()) {
838
+ audio_i2s_mp3.task_running == false ;
829
839
}
840
+ vTaskDelay (pdMS_TO_TICKS (1 ));
830
841
}
842
+ audio_i2s.out ->flush ();
843
+ audio_i2s_mp3.mp3 ->stop ();
844
+ I2sStopPlaying ();
845
+ mp3_delete ();
846
+ audio_i2s_mp3.mp3_task_handle = nullptr ;
847
+ audio_i2s_mp3.task_has_ended = true ;
848
+ vTaskDelete (NULL );
831
849
}
832
850
#endif // defined(USE_I2S_MP3) || defined(USE_I2S_WEBRADIO)
833
851
@@ -851,29 +869,17 @@ void I2sMp3WrTask(void *arg){
851
869
vTaskDelay (pdMS_TO_TICKS (1 ));
852
870
}
853
871
}
854
- audio_i2s_mp3.decoder ->stop ();
855
- audio_i2s_mp3.task_has_ended = true ;
872
+ audio_i2s.out ->flush ();
856
873
I2sStopPlaying ();
874
+ audio_i2s_mp3.mp3_task_handle = nullptr ;
875
+ audio_i2s_mp3.task_has_ended = true ;
876
+ vTaskDelete (NULL );
857
877
}
858
- void I2SStopMP3Play (void ) {
859
878
860
- if (audio_i2s_mp3.decoder ) {
861
- audio_i2s_mp3.decoder ->stop ();
862
- delete audio_i2s_mp3.decoder ;
863
- audio_i2s_mp3.decoder = NULL ;
864
- }
865
-
866
- if (audio_i2s_mp3.mp3_task_handle ) {
867
- vTaskDelete (audio_i2s_mp3.mp3_task_handle );
868
- audio_i2s_mp3.mp3_task_handle = nullptr ;
869
- }
870
- }
871
879
#endif // USE_I2S_MP3
872
880
873
881
void I2sStopPlaying () {
874
- #ifdef USE_I2S_MP3
875
- I2SStopMP3Play ();
876
- #endif // USE_I2S_MP3
882
+
877
883
#ifdef USE_I2S_WEBRADIO
878
884
I2sWebRadioStopPlaying ();
879
885
#endif
@@ -921,7 +927,12 @@ void mp3_delete(void) {
921
927
delete audio_i2s_mp3.id3 ;
922
928
delete audio_i2s_mp3.mp3 ;
923
929
audio_i2s_mp3.mp3 =nullptr ;
924
- I2SAudioPower (false );
930
+
931
+ if (audio_i2s_mp3.decoder ) {
932
+ audio_i2s_mp3.decoder ->stop ();
933
+ delete audio_i2s_mp3.decoder ;
934
+ audio_i2s_mp3.decoder = NULL ;
935
+ }
925
936
}
926
937
#endif // USE_I2S_MP3
927
938
@@ -959,7 +970,7 @@ void CmndI2SMic(void) {
959
970
960
971
961
972
void CmndI2SStop (void ) {
962
- if (! I2SPrepareTx ()) {
973
+ if (I2SPrepareTx () != I2S_OK ) {
963
974
ResponseCmndChar (" I2S output not configured" );
964
975
return ;
965
976
}
@@ -1006,7 +1017,7 @@ void CmndI2SGain(void) {
1006
1017
if ((XdrvMailbox.payload >= 0 ) && (XdrvMailbox.payload <= 100 )) {
1007
1018
if (audio_i2s.out ) {
1008
1019
audio_i2s.Settings ->tx .gain = XdrvMailbox.payload ;
1009
- audio_i2s.out ->SetGain (((float )(audio_i2s.Settings ->tx .gain - 2 )/100.0 )* 4.0 );
1020
+ audio_i2s.out ->SetGain (((float )(audio_i2s.Settings ->tx .gain + 1 )/100.0 ));
1010
1021
}
1011
1022
}
1012
1023
ResponseCmndNumber (audio_i2s.Settings ->tx .gain );
@@ -1050,8 +1061,12 @@ void CmndI2SMicRec(void) {
1050
1061
if (!strncmp (XdrvMailbox.data , " -?" , 2 )) {
1051
1062
Response_P (" {\" I2SREC-duration\" :%d}" , audio_i2s_mp3.recdur );
1052
1063
} else {
1053
- I2sRecordShine (XdrvMailbox.data );
1054
- ResponseCmndChar (XdrvMailbox.data );
1064
+ int err = I2sRecordShine (XdrvMailbox.data );
1065
+ if (err == pdPASS){
1066
+ ResponseCmndChar (XdrvMailbox.data );
1067
+ } else {
1068
+ ResponseCmndChar_P (PSTR (" Did not launch recording task" ));
1069
+ }
1055
1070
}
1056
1071
} else {
1057
1072
if (audio_i2s_mp3.mic_task_handle ) {
@@ -1062,6 +1077,9 @@ void CmndI2SMicRec(void) {
1062
1077
}
1063
1078
ResponseCmndChar_P (PSTR (" Stopped" ));
1064
1079
}
1080
+ else {
1081
+ ResponseCmndChar_P (PSTR (" No running recording" ));
1082
+ }
1065
1083
}
1066
1084
}
1067
1085
else {
0 commit comments