Click here to Skip to main content
15,902,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I do english application for pronoun correctly.My Problem about save .wav file from user speech.

Code user speech recording.
.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if(motionEvent.getAction() == MotionEvent.ACTION_DOWN){ //start recording
                    Toast.makeText(getApplicationContext(), "Recording...", Toast.LENGTH_LONG).show();
                    MediaRecorderReady();
                    try {
                        mediaRecorder.prepare();
                        mediaRecorder.start();   // Recording is now started
                    } catch (IllegalStateException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }else if(motionEvent.getAction() == MotionEvent.ACTION_UP){ 
                    Toast.makeText(getApplicationContext(), "Audio Recorder successfully", Toast.LENGTH_SHORT).show();
                    if (mediaRecorder != null){
                        try{
                            mediaRecorder.stop();
                        }catch (RuntimeException e){

                        }finally {
                            mediaRecorder.release(); 
                            mediaRecorder = null;
                            uploadSound1(view);
                            Toast.makeText(StudyingActivity.this,"Successes upload 2",Toast.LENGTH_LONG).show();
                        }
                    }
                }
                return false;
            }
        });


private void MediaRecorderReady() {
       SimpleDateFormat formatter = new SimpleDateFormat("dd_MM_yyyy", Locale.KOREA);
       Date now = new Date();
       String outputFile = Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+"record_"+formatter.format(now)+".wav";

       mediaRecorder = new MediaRecorder();
       mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
       mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
       mediaRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
       mediaRecorder.setOutputFile(outputFile);
   }


Sound.py file
import librosa
import librosa.display
import matplotlib.pyplot as plt
from numpy.linalg import norm
from dtw import dtw
import datetime


today_date = datetime.date.today()

new_today_date = today_date.strftime('%d_%m_%Y')

name = 'record_'+(new_today_date)+'.wav'


y1, sr1 = librosa.load('words-english.wav') //can read
y2, sr2 = librosa.load(name)  //can't read


plt.subplot(1, 2, 1) 
mfcc1 = librosa.feature.mfcc(y1,sr1)   
librosa.display.specshow(mfcc1)
plt.subplot(1, 2, 2)
mfcc2 = librosa.feature.mfcc(y2, sr2)
librosa.display.specshow(mfcc2)


dist, cost, acc_cost, path = dtw(mfcc1.T, mfcc2.T, dist=lambda x, y: norm(x - y, ord=1))


print('%.3f' % dist)

librosa.output.write_wav('1.wav', y1, sr1)


What I have tried:

I can't read audiorecord from user speech (.wav) to python file but I can read TTS(Text to speech sound).


This Error in sound.py
D:\Users\Python\Python36\lib\site-packages\librosa\core\audio.py:14
  warnings.warn('PySoundFile failed. Trying audioread instead.')
Traceback (most recent call last):
  File "D:\Users\Python\Python36\lib\site-packages\librosa\core\aud
RuntimeError: Error opening 'record_15_12_2019.wav': File contains data in an unknown format.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "d:/xampp/htdocs/wordsapp/sound/sound.py", line 17, in <module>
    y2, sr2 = librosa.load('record.wav')
  File "D:\Users\Python\Python36\lib\site-packages\librosa\core\audio.py", line 147, in load
    y, sr_native = __audioread_load(path, offset, duration, dtype)
  File "D:\Users\Python\Python36\lib\site-packages\librosa\core\audio.py", line 171, in __audioread_load
    with audioread.audio_open(path) as input_file:
  File "D:\Users\Python\Python36\lib\site-packages\audioread\__init__.py", line 116, in audio_open
    raise NoBackendError()
audioread.exceptions.NoBackendError
Posted
Updated 15-Dec-19 4:42am
v2
Comments
Richard MacCutchan 15-Dec-19 11:31am    
"Error opening 'record_15_12_2019.wav': File contains data in an unknown format."
The message tells you why it fails.
JBGot7 18-Dec-19 1:53am    
I tried and it didn't work.Can you advise me to how to convert wav format?
Richard MacCutchan 18-Dec-19 3:50am    
Sorry, I have no idea. As I already said, the message is clear, this package does not understand wav format. You need to do more searching to find a package that can do the conversion.
JBGot7 18-Dec-19 11:32am    
okay.thank you :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900