Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Java - Come salvare in binario dalla sorgente audio
Forum - Java - Come salvare in binario dalla sorgente audio

Avatar
luam11 (Normal User)
Newbie


Messaggi: 3
Iscritto: 17/02/2011

Segnala al moderatore
Postato alle 20:55
Domenica, 20/03/2011
Salve, volevo sapere come si può salvare i dati in binario da una sorgente audio?

Questo e quello che ho preso spunto da vari tutorial.

Codice sorgente - presumibilmente Java

  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.io.*;
  5. import javax.sound.sampled.*;
  6.  
  7. public class CaptureFrame extends JFrame {
  8.  
  9.   protected boolean running;
  10.  
  11.   public CaptureFrame() {
  12.     super("Capture Sound Demo");
  13.     setDefaultCloseOperation(EXIT_ON_CLOSE);
  14.     Container content = getContentPane();
  15.  
  16.     final JButton capture = new JButton("Capture");
  17.     final JButton stop = new JButton("Stop");
  18.     final JButton play = new JButton("Play");
  19.  
  20.     capture.setEnabled(true);
  21.     stop.setEnabled(false);
  22.     play.setEnabled(false);
  23.  
  24.     ActionListener captureListener =
  25.         new ActionListener() {
  26.       public void actionPerformed(ActionEvent e) {
  27.         capture.setEnabled(false);
  28.         stop.setEnabled(true);
  29.         play.setEnabled(false);
  30.         captureAudio();
  31.       }
  32.     };
  33.     capture.addActionListener(captureListener);
  34.     content.add(capture, BorderLayout.NORTH);
  35.  
  36.     ActionListener stopListener =
  37.         new ActionListener() {
  38.       public void actionPerformed(ActionEvent e) {
  39.         capture.setEnabled(true);
  40.         stop.setEnabled(false);
  41.         play.setEnabled(true);
  42.         running = false;
  43.       }
  44.     };
  45.     stop.addActionListener(stopListener);
  46.     content.add(stop, BorderLayout.CENTER);
  47.  
  48.     ActionListener playListener =
  49.         new ActionListener() {
  50.       public void actionPerformed(ActionEvent e) {
  51.         playAudio();
  52.       }
  53.     };
  54.     play.addActionListener(playListener);
  55.     content.add(play, BorderLayout.SOUTH);
  56.   }
  57.  
  58.   private void captureAudio() {
  59.     try {
  60.       final AudioFormat format = getFormat();
  61.       DataLine.Info info = new DataLine.Info(
  62.         TargetDataLine.class, format);
  63.       final TargetDataLine line = (TargetDataLine)
  64.         AudioSystem.getLine(info);
  65.       line.open(format);
  66.       line.start();
  67.       Runnable runner = new Runnable() {
  68.         int bufferSize = (int)format.getSampleRate()
  69.           * format.getFrameSize();
  70.         byte buffer[] = new byte[bufferSize];
  71.  
  72.         public void run() {
  73.           out = new ByteArrayOutputStream();
  74.           running = true;
  75.           try {
  76.             while (running) {
  77.               int count =
  78.                 line.read(buffer, 0, buffer.length);
  79.               if (count > 0) {
  80.                 out.write(buffer, 0, count);
  81.               }
  82.             }
  83.             out.close();
  84.           } catch (IOException e) {
  85.             System.err.println("I/O problems: " + e);
  86.             System.exit(-1);
  87.           }
  88.         }
  89.       };
  90.       Thread captureThread = new Thread(runner);
  91.       captureThread.start();
  92.     } catch (LineUnavailableException e) {
  93.       System.err.println("Line unavailable: " + e);
  94.       System.exit(-2);
  95.     }
  96.   }
  97.  
  98.   private void playAudio() {
  99.     try {
  100.       byte audio[] = out.toByteArray();
  101.       InputStream input =
  102.         new ByteArrayInputStream(audio);
  103.       final AudioFormat format = getFormat();
  104.       final AudioInputStream ais =
  105.         new AudioInputStream(input, format,
  106.         audio.length / format.getFrameSize());
  107.       DataLine.Info info = new DataLine.Info(
  108.         SourceDataLine.class, format);
  109.       final SourceDataLine line = (SourceDataLine)
  110.         AudioSystem.getLine(info);
  111.       line.open(format);
  112.       line.start();
  113.  
  114.       Runnable runner = new Runnable() {
  115.         int bufferSize = (int) format.getSampleRate()
  116.           * format.getFrameSize();
  117.         byte buffer[] = new byte[bufferSize];
  118.  
  119.         public void run() {
  120.           try {
  121.             int count;
  122.             while ((count = ais.read(
  123.                 buffer, 0, buffer.length)) != -1) {
  124.               if (count > 0) {
  125.                 line.write(buffer, 0, count);
  126.               }
  127.             }
  128.             line.drain();
  129.             line.close();
  130.           } catch (IOException e) {
  131.             System.err.println("I/O problems: " + e);
  132.             System.exit(-3);
  133.           }
  134.         }
  135.       };
  136.       Thread playThread = new Thread(runner);
  137.       playThread.start();
  138.     } catch (LineUnavailableException e) {
  139.       System.err.println("Line unavailable: " + e);
  140.       System.exit(-4);
  141.     }
  142.   }
  143.  
  144.   private AudioFormat getFormat() {
  145.     float sampleRate = 8000;
  146.     int sampleSizeInBits = 8;
  147.     int channels = 1;
  148.     boolean signed = true;
  149.     boolean bigEndian = true;
  150.     return new AudioFormat(sampleRate,
  151.       sampleSizeInBits, channels, signed, bigEndian);
  152.   }
  153. }


PM Quote