Search in sources :

Example 1 with WAVWriter

use of com.codename1.media.WAVWriter in project CodenameOne by codenameone.

the class AudioBufferSample method start.

public void start() {
    if (current != null) {
        current.show();
        return;
    }
    Form hi = new Form("Capture", BoxLayout.y());
    hi.setToolbar(new Toolbar());
    Style s = UIManager.getInstance().getComponentStyle("Title");
    FontImage icon = FontImage.createMaterial(FontImage.MATERIAL_MIC, s);
    FileSystemStorage fs = FileSystemStorage.getInstance();
    String recordingsDir = fs.getAppHomePath() + "recordings/";
    fs.mkdir(recordingsDir);
    try {
        for (String file : fs.listFiles(recordingsDir)) {
            MultiButton mb = new MultiButton(file.substring(file.lastIndexOf("/") + 1));
            mb.addActionListener((e) -> {
                try {
                    Media m = MediaManager.createMedia(recordingsDir + file, false);
                    m.play();
                } catch (Throwable err) {
                    Log.e(err);
                }
            });
            hi.add(mb);
        }
        hi.getToolbar().addCommandToRightBar("", icon, (ev) -> {
            try {
                String path = "tmpBuffer.pcm";
                int wavSampleRate = 16000;
                WAVWriter wavFileWriter = new WAVWriter(new File("tmpBuffer.wav"), wavSampleRate, 1, 16);
                AudioBuffer audioBuffer = MediaManager.getAudioBuffer(path, true, 64);
                MediaRecorderBuilder options = new MediaRecorderBuilder().audioChannels(1).redirectToAudioBuffer(true).path(path);
                System.out.println("Builder isredirect? " + options.isRedirectToAudioBuffer());
                float[] byteBuffer = new float[audioBuffer.getMaxSize()];
                audioBuffer.addCallback(buf -> {
                    if (buf.getSampleRate() > wavSampleRate) {
                        buf.downSample(wavSampleRate);
                    }
                    buf.copyTo(byteBuffer);
                    try {
                        wavFileWriter.write(byteBuffer, 0, buf.getSize());
                    } catch (Throwable t) {
                        Log.e(t);
                    }
                });
                String file = Capture.captureAudio(options);
                wavFileWriter.close();
                SimpleDateFormat sd = new SimpleDateFormat("yyyy-MMM-dd-kk-mm");
                String fileName = sd.format(new Date());
                String filePath = recordingsDir + fileName;
                Util.copy(fs.openInputStream(new File("tmpBuffer.wav").getAbsolutePath()), fs.openOutputStream(filePath));
                MultiButton mb = new MultiButton(fileName);
                mb.addActionListener((e) -> {
                    try {
                        Media m = MediaManager.createMedia(filePath, false);
                        m.play();
                    } catch (IOException err) {
                        Log.e(err);
                    }
                });
                hi.add(mb);
                hi.revalidate();
                if (file != null) {
                    System.out.println(file);
                }
            } catch (Throwable err) {
                Log.e(err);
            }
        });
    } catch (Throwable err) {
        Log.e(err);
    }
    hi.show();
}
Also used : MediaRecorderBuilder(com.codename1.media.MediaRecorderBuilder) Form(com.codename1.ui.Form) FileSystemStorage(com.codename1.io.FileSystemStorage) Media(com.codename1.media.Media) IOException(java.io.IOException) WAVWriter(com.codename1.media.WAVWriter) Date(java.util.Date) Style(com.codename1.ui.plaf.Style) AudioBuffer(com.codename1.media.AudioBuffer) FontImage(com.codename1.ui.FontImage) MultiButton(com.codename1.components.MultiButton) File(com.codename1.io.File) SimpleDateFormat(com.codename1.l10n.SimpleDateFormat) Toolbar(com.codename1.ui.Toolbar)

Aggregations

MultiButton (com.codename1.components.MultiButton)1 File (com.codename1.io.File)1 FileSystemStorage (com.codename1.io.FileSystemStorage)1 SimpleDateFormat (com.codename1.l10n.SimpleDateFormat)1 AudioBuffer (com.codename1.media.AudioBuffer)1 Media (com.codename1.media.Media)1 MediaRecorderBuilder (com.codename1.media.MediaRecorderBuilder)1 WAVWriter (com.codename1.media.WAVWriter)1 FontImage (com.codename1.ui.FontImage)1 Form (com.codename1.ui.Form)1 Toolbar (com.codename1.ui.Toolbar)1 Style (com.codename1.ui.plaf.Style)1 IOException (java.io.IOException)1 Date (java.util.Date)1