Add import wav file

This commit is contained in:
2020-02-08 23:32:33 +00:00
parent 577ff9c1eb
commit f6af5ce2e7
3 changed files with 76 additions and 8 deletions

View File

@@ -90,6 +90,7 @@ import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.OutputKeys;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Mixer;
import javax.sound.sampled.SourceDataLine;
@@ -141,8 +142,6 @@ public class AudiobookRecorder extends JFrame implements DocumentListener {
JMenuItem toolsManuscript;
JMenuItem toolsOptions;
JMenuItem helpAbout;
FlashPanel centralPanel;
JPanel statusBar;
@@ -361,16 +360,21 @@ public class AudiobookRecorder extends JFrame implements DocumentListener {
helpMenu = new JMenu("Help");
helpAbout = new JMenuItem("About AudiobookRecorder");
helpAbout.addActionListener(new ActionListener() {
helpMenu.add(new JMenuObject("Website", null, new ActionListener() {
public void actionPerformed(ActionEvent e) {
Debug.trace();
Utils.browse("https://majenkoprojects.github.io/AudiobookRecorder/index.html");
}
}));
helpMenu.add(new JMenuObject("About AudiobookRecorder", null, new ActionListener() {
public void actionPerformed(ActionEvent e) {
Debug.trace();
JOptionPane.showMessageDialog(AudiobookRecorder.this, new AboutPanel(), "About AudiobookRecorder", JOptionPane.PLAIN_MESSAGE);
}
});
}));
helpMenu.add(helpAbout);
menuBar.add(helpMenu);
ob.add(menuBar, BorderLayout.NORTH);
@@ -574,7 +578,7 @@ public class AudiobookRecorder extends JFrame implements DocumentListener {
}
});
gainPercent = new JSpinner(new SteppedNumericSpinnerModel(0, 500, 1, 100));
gainPercent = new JSpinner(new SteppedNumericSpinnerModel(0, 1000, 1, 100));
gainPercent.setPreferredSize(new Dimension(50, 20));
gainPercent.addChangeListener(new ChangeListener() {
@@ -1681,6 +1685,15 @@ public class AudiobookRecorder extends JFrame implements DocumentListener {
}
});
JMenuObject importWav = new JMenuObject("Import WAV file...", c, new ActionListener() {
public void actionPerformed(ActionEvent e) {
Debug.trace();
JMenuObject o = (JMenuObject)e.getSource();
Chapter chap = (Chapter)o.getObject();
importWavFile(chap);
}
});
JMenuObject exportChapter = new JMenuObject("Export chapter", c, new ActionListener() {
public void actionPerformed(ActionEvent e) {
Debug.trace();
@@ -1791,6 +1804,7 @@ public class AudiobookRecorder extends JFrame implements DocumentListener {
menu.add(lockAll);
menu.add(unlockAll);
menu.addSeparator();
menu.add(importWav);
menu.add(exportChapter);
menu.addSeparator();
menu.add(deleteChapter);
@@ -3772,4 +3786,45 @@ public class AudiobookRecorder extends JFrame implements DocumentListener {
CacheManager.purgeCache();
}
public void importWavFile(Chapter c) {
JFileChooser jc = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("WAV Files", "wav");
jc.addChoosableFileFilter(filter);
jc.setFileFilter(filter);
jc.setDialogTitle("Select WAV File");
int r = jc.showOpenDialog(this);
if (r != JFileChooser.APPROVE_OPTION) return;
File f = jc.getSelectedFile();
if (!f.exists()) return;
try {
Book book = c.getBook();
AudioFormat targetFormat = book.getAudioFormat();
Sentence newSentence = new Sentence();
newSentence.setText(f.getName());
newSentence.setParentBook(book);
c.add(newSentence);
FileOutputStream fos = new FileOutputStream(newSentence.getFile());
AudioInputStream source = AudioSystem.getAudioInputStream(f);
AudioInputStream in = AudioSystem.getAudioInputStream(targetFormat, source);
AudioSystem.write(in, AudioFileFormat.Type.WAVE, newSentence.getFile());
// fos.close();
in.close();
source.close();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
bookTreeModel.reload(c);
bookTree.setSelectionPath(new TreePath(newSentence.getPath()));
bookTree.scrollPathToVisible(new TreePath(newSentence.getPath()));
}
});
} catch (Exception ex) {
ex.printStackTrace();
}
}
}

View File

@@ -54,6 +54,7 @@ public class Book extends BookTreeNode {
File location;
Random rng = new Random();
TreeMap<String, EffectGroup> effects;
AudioFormat cachedFormat = null;
public Book(String bookname) {
super(bookname);
@@ -126,6 +127,13 @@ public class Book extends BookTreeNode {
roomNoise = new Sentence("room-noise", "Room Noise");
roomNoise.setParentBook(this);
AudioFormat fmt = getAudioFormat();
if (fmt != null) {
sampleRate = (int)fmt.getSampleRate();
channels = fmt.getChannels();
resolution = fmt.getSampleSizeInBits();
}
Element chapters = getNode(root, "chapters");
NodeList chapterList = chapters.getElementsByTagName("chapter");
for (int i = 0; i < chapterList.getLength(); i++) {
@@ -342,7 +350,11 @@ public class Book extends BookTreeNode {
public AudioFormat getAudioFormat() {
Debug.trace();
return new AudioFormat(getSampleRate(), getResolution(), getChannels(), true, false);
if (cachedFormat != null) {
return cachedFormat;
}
cachedFormat = roomNoise.getAudioFormat();
return cachedFormat;
}
public File getBookFolder() {

View File

@@ -24,6 +24,7 @@ import org.w3c.dom.Element;
import org.w3c.dom.Text;
import javax.sound.sampled.TargetDataLine;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import java.io.File;
import java.io.IOException;