api3.window.sound.panel.SoundPanel.java Source code

Java tutorial

Introduction

Here is the source code for api3.window.sound.panel.SoundPanel.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package api3.window.sound.panel;

import api3.WavFile.WavFile;
import api3.main.utils.Utils;
import api3.transform.FundamentalFrequency.FundamentalFrequencyFinder;
import api3.window.sound.panel.main.MainSoundPanel;
import java.awt.BasicStroke;
import java.awt.Color;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.Border;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.ValueMarker;
import org.jfree.chart.plot.XYPlot;

/**
 *
 * @author Aga
 */
public abstract class SoundPanel extends javax.swing.JPanel {

    /**
     * Creates new form SoundPanel
     * @param wavfile
     * @param string
     * @param signal
     */
    public SoundPanel(WavFile wavfile, String string, double[] signal) {
        this.wavFile = wavfile;
        this.file_appendix = string;
        this.signal = signal;
        initComponents();
    }

    public SoundPanel() {

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        soundPanel = new javax.swing.JPanel();
        jScrollPane = new javax.swing.JScrollPane();
        soundLog = new javax.swing.JTextArea();

        soundPanel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        soundPanel.setLayout(new java.awt.BorderLayout());

        soundLog.setColumns(20);
        soundLog.setRows(5);
        jScrollPane.setViewportView(soundLog);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addContainerGap()
                        .addComponent(soundPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 500,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE)
                        .addContainerGap()));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
                javax.swing.GroupLayout.Alignment.TRAILING,
                layout.createSequentialGroup().addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jScrollPane).addComponent(soundPanel,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, 228, Short.MAX_VALUE))
                        .addContainerGap()));
    }// </editor-fold>//GEN-END:initComponents

    private static final Logger LOG = Logger.getLogger(SoundPanel.class.getName());
    private String file_appendix;
    private NumberAxis domainAxis;
    public double[] signal;
    public WavFile wavFile;
    private JFreeChart soundChart;
    private PanelData soundPlot;

    public void setFile_appendix(String file_appendix) {
        this.file_appendix = file_appendix;
    }

    public String getFile_appendix() {
        return file_appendix;
    }

    public WavFile getWavFile() {
        return wavFile;
    }

    public JFreeChart getSoundChart() {
        return soundChart;
    }

    public PanelData getSoundPlot() {
        return soundPlot;
    }

    public void setWavFile(WavFile wavFile) {
        this.wavFile = wavFile;
    }

    public void setSoundChart(JFreeChart soundChart) {
        this.soundChart = soundChart;
    }

    public void setSoundPlot(PanelData soundPlot) {
        this.soundPlot = soundPlot;
    }

    public abstract void processSound();

    public void saveFile() {
        String filename = Utils.OUTPUT_FILES_DIRETORY + wavFile.getFilename() + file_appendix;
        LOG.info("filename : " + filename);
        soundPlot.generateWaveFile(wavFile, filename);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PrintStream ps = new PrintStream(baos);
        wavFile.display(ps);
        try {
            appendLog(baos.toString("UTF-8"));
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(MainSoundPanel.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public void appendLog(String log) {
        this.soundLog.append(log + FundamentalFrequencyFinder.newline);
    }

    public void plot(PanelData data, JFreeChart chart, JPanel plotPanel) {
        chart = ChartFactory.createXYLineChart(data.name, "prbka", "warto", data.dataset,
                PlotOrientation.VERTICAL, true, false, false);
        XYPlot plot = (XYPlot) chart.getPlot();
        LOG.info("PLOTTING      1");
        domainAxis = (NumberAxis) plot.getDomainAxis();
        plot.addRangeMarker(new ValueMarker(0, Color.BLACK, new BasicStroke(1)));

        ChartPanel chartPanel = new ChartPanel(chart);
        Border border = BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4),
                BorderFactory.createEtchedBorder());
        chartPanel.setBorder(border);
        LOG.info("PLOTTING      2");
        plotPanel.removeAll();
        plotPanel.add(chartPanel);
        plotPanel.revalidate();
        LOG.info("PLOTTING      3");
    }

    public void setSoundPanel(JPanel soundPanel) {
        this.soundPanel = soundPanel;
    }

    public JPanel getSoundPanel() {
        return soundPanel;
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane;
    private javax.swing.JTextArea soundLog;
    private javax.swing.JPanel soundPanel;
    // End of variables declaration//GEN-END:variables
}