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.stream.StreamResult;
import javax.xml.transform.OutputKeys; import javax.xml.transform.OutputKeys;
import javax.sound.sampled.AudioFormat; import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioSystem; import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Mixer; import javax.sound.sampled.Mixer;
import javax.sound.sampled.SourceDataLine; import javax.sound.sampled.SourceDataLine;
@@ -141,8 +142,6 @@ public class AudiobookRecorder extends JFrame implements DocumentListener {
JMenuItem toolsManuscript; JMenuItem toolsManuscript;
JMenuItem toolsOptions; JMenuItem toolsOptions;
JMenuItem helpAbout;
FlashPanel centralPanel; FlashPanel centralPanel;
JPanel statusBar; JPanel statusBar;
@@ -361,16 +360,21 @@ public class AudiobookRecorder extends JFrame implements DocumentListener {
helpMenu = new JMenu("Help"); 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) { public void actionPerformed(ActionEvent e) {
Debug.trace(); Debug.trace();
JOptionPane.showMessageDialog(AudiobookRecorder.this, new AboutPanel(), "About AudiobookRecorder", JOptionPane.PLAIN_MESSAGE); JOptionPane.showMessageDialog(AudiobookRecorder.this, new AboutPanel(), "About AudiobookRecorder", JOptionPane.PLAIN_MESSAGE);
} }
}); }));
helpMenu.add(helpAbout);
menuBar.add(helpMenu); menuBar.add(helpMenu);
ob.add(menuBar, BorderLayout.NORTH); 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.setPreferredSize(new Dimension(50, 20));
gainPercent.addChangeListener(new ChangeListener() { 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() { JMenuObject exportChapter = new JMenuObject("Export chapter", c, new ActionListener() {
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
Debug.trace(); Debug.trace();
@@ -1791,6 +1804,7 @@ public class AudiobookRecorder extends JFrame implements DocumentListener {
menu.add(lockAll); menu.add(lockAll);
menu.add(unlockAll); menu.add(unlockAll);
menu.addSeparator(); menu.addSeparator();
menu.add(importWav);
menu.add(exportChapter); menu.add(exportChapter);
menu.addSeparator(); menu.addSeparator();
menu.add(deleteChapter); menu.add(deleteChapter);
@@ -3772,4 +3786,45 @@ public class AudiobookRecorder extends JFrame implements DocumentListener {
CacheManager.purgeCache(); 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; File location;
Random rng = new Random(); Random rng = new Random();
TreeMap<String, EffectGroup> effects; TreeMap<String, EffectGroup> effects;
AudioFormat cachedFormat = null;
public Book(String bookname) { public Book(String bookname) {
super(bookname); super(bookname);
@@ -126,6 +127,13 @@ public class Book extends BookTreeNode {
roomNoise = new Sentence("room-noise", "Room Noise"); roomNoise = new Sentence("room-noise", "Room Noise");
roomNoise.setParentBook(this); roomNoise.setParentBook(this);
AudioFormat fmt = getAudioFormat();
if (fmt != null) {
sampleRate = (int)fmt.getSampleRate();
channels = fmt.getChannels();
resolution = fmt.getSampleSizeInBits();
}
Element chapters = getNode(root, "chapters"); Element chapters = getNode(root, "chapters");
NodeList chapterList = chapters.getElementsByTagName("chapter"); NodeList chapterList = chapters.getElementsByTagName("chapter");
for (int i = 0; i < chapterList.getLength(); i++) { for (int i = 0; i < chapterList.getLength(); i++) {
@@ -342,7 +350,11 @@ public class Book extends BookTreeNode {
public AudioFormat getAudioFormat() { public AudioFormat getAudioFormat() {
Debug.trace(); Debug.trace();
return new AudioFormat(getSampleRate(), getResolution(), getChannels(), true, false); if (cachedFormat != null) {
return cachedFormat;
}
cachedFormat = roomNoise.getAudioFormat();
return cachedFormat;
} }
public File getBookFolder() { public File getBookFolder() {

View File

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