Skip to content

Commit

Permalink
v0.1.24
Browse files Browse the repository at this point in the history
- Meico update to v0.9.0.
- Some first preparations for an audio recorder function.
  • Loading branch information
axelberndt committed May 24, 2023
1 parent ff96dfa commit b497e5a
Show file tree
Hide file tree
Showing 5 changed files with 214 additions and 3 deletions.
5 changes: 4 additions & 1 deletion src/mpmToolbox/gui/ProjectPane.java
Original file line number Diff line number Diff line change
Expand Up @@ -339,11 +339,14 @@ public ArrayList<Audio> getAudio() {
/**
* add an Audio object to the list of audios
* @param audio
* @return success
*/
public void addAudio(Audio audio) {
public boolean addAudio(Audio audio) {
if (this.data.addAudio(audio)) {
this.syncPlayer.addAudio(audio);
return true;
}
return false;
}

/**
Expand Down
162 changes: 162 additions & 0 deletions src/mpmToolbox/gui/syncPlayer/RecorderDialog.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
package mpmToolbox.gui.syncPlayer;

import com.alee.laf.button.WebButton;
import com.alee.laf.combobox.WebComboBox;
import com.alee.laf.label.WebLabel;
import com.alee.laf.panel.WebPanel;
import com.alee.laf.window.WebDialog;
import mpmToolbox.gui.Settings;
import mpmToolbox.gui.syncPlayer.utilities.RecordingDeviceChooserItem;
import mpmToolbox.projectData.audio.Audio;
import mpmToolbox.supplementary.Tools;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
* This class represents an audio recorder. Create an instance and invoke openDialog().
* This will return null or an Audio object with the recording.
*/
public class RecorderDialog extends WebDialog<RecorderDialog> {
protected GridBagLayout contentPanelLayout = new GridBagLayout();
protected WebPanel contentPanel = new WebPanel(this.contentPanelLayout);
private Audio recording = null; // the audio recording to be made

/**
* constructor
*/
public RecorderDialog() {
super();

this.setTitle("Audio Recorder");
this.setIconImages(Settings.getIcons(null));
this.setResizable(Settings.debug);
this.setModal(true);
this.initKeyboardShortcuts();
this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

// close procedure when clicking on X
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
recording = null;
dispose();
}
});

// content panel
this.contentPanel.setPadding(Settings.paddingInDialogs);
this.makeContentPanel();

// Run / Cancel panel
GridBagLayout runPanelLayout = new GridBagLayout();
WebPanel okPanel = new WebPanel(runPanelLayout);
okPanel.setPadding(Settings.paddingInDialogs);

WebButton run = new WebButton("Store", actionEvent -> {
this.dispose();
});
run.setHorizontalAlignment(WebButton.CENTER);
run.setPadding(Settings.paddingInDialogs*2, Settings.paddingInDialogs, Settings.paddingInDialogs*2, Settings.paddingInDialogs);
Tools.addComponentToGridBagLayout(okPanel, runPanelLayout, run, 0, 0, 1, 1, 1.0, 1.0, 0, 0, GridBagConstraints.BOTH, GridBagConstraints.LINE_START);

WebButton cancel = new WebButton("Cancel", actionEvent -> {
this.recording = null;
this.dispose();
});
cancel.setHorizontalAlignment(WebButton.CENTER);
cancel.setPadding(Settings.paddingInDialogs*2, Settings.paddingInDialogs, Settings.paddingInDialogs*2, Settings.paddingInDialogs);
Tools.addComponentToGridBagLayout(okPanel, runPanelLayout, cancel, 1, 0, 1, 1, 1.0, 1.0, 0, 0, GridBagConstraints.BOTH, GridBagConstraints.LINE_START);

// put all together
GridBagLayout layout = new GridBagLayout();
this.setLayout(layout);
Tools.addComponentToGridBagLayout(this, layout, this.contentPanel, 0, 0, 1, 1, 1.0, 1.0, 0, 0, GridBagConstraints.BOTH, GridBagConstraints.LINE_START);
Tools.addComponentToGridBagLayout(this, layout, okPanel, 0, 1, 1, 1, 1.0, 1.0, 0, 0, GridBagConstraints.BOTH, GridBagConstraints.LINE_START);

this.pack(); // fit the size to the content

this.setLocationRelativeTo(null);
}

/**
* This method must be implemented to create the real contents of the dialog. Consider that the content panel's layout manager is of type GridBagLayout.
*/
public void makeContentPanel() {
WebLabel deviceLabel = new WebLabel("Recording Device:");
deviceLabel.setHorizontalAlignment(WebLabel.RIGHT);
deviceLabel.setPadding(Settings.paddingInDialogs);
this.addToContentPanel(deviceLabel, 0, 0, 1, 1, 1.0, 1.0, 0, 0, GridBagConstraints.BOTH);

WebComboBox deviceChooser = new WebComboBox();
deviceChooser.setToolTip("Choose recording device.");
deviceChooser.setPadding(Settings.paddingInDialogs);
// deviceChooser.add(new RecordingDeviceChooserItem("Choose Recording Device", null));

}

/**
* this opens the dialog window
* @return the recorded audio data or null
*/
public Audio openDialog() {
this.setVisible(true); // start the dialog

// after the dialog closed do the following

return this.recording;
}

/**
* a helper method to add components to the content panel
* @param component
* @param x horizontal position in grid
* @param y vertical position in grid
* @param width how many horizontal grid boxes are covered by the component
* @param height how many vertical grid boxes are covered by the component
* @param weightx default is 1.0
* @param weighty default is 1.0
* @param ipadx default is 0
* @param ipady default is 0
* @param fill default is GridBagConstraints.BOTH
*/
public void addToContentPanel(Component component, int x, int y, int width, int height, double weightx, double weighty, int ipadx, int ipady, int fill) {
Tools.addComponentToGridBagLayout(this.contentPanel, this.contentPanelLayout, component, x, y, width, height, weightx, weighty, ipadx, ipady, fill, GridBagConstraints.LINE_START);
}

/**
* this initializes the keyboard shortcuts (ESC, ENTER)
*/
private void initKeyboardShortcuts() {
InputMap inputMap = this.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);

inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "Cancel");
this.getRootPane().getActionMap().put("Cancel", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
recording = null;
dispose();
}
});

inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "Run");
this.getRootPane().getActionMap().put("Run", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
dispose();
}
});
}

/**
* get the name of this implementation
* @return
*/
@Override
public String toString() {
return this.getTitle();
}
}
18 changes: 16 additions & 2 deletions src/mpmToolbox/gui/syncPlayer/SyncPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,9 @@ private void makeGui() {
recordButton.setToolTip("Make an Audio Recording");
recordButton.setPadding(Settings.paddingInDialogs);
recordButton.addActionListener(actionEvent -> {
System.out.println("REC");
// TODO: open recording dialog
Audio recording = (new RecorderDialog()).openDialog(); // open recording dialog
if (this.parent.addAudio(recording)) // if the recording is not null and could successfully be added to the project data, the syncPlayer's audio list gets updated
this.selectAudio(recording); // and we select the recording immediately
});
Tools.addComponentToGridBagLayout(this, (GridBagLayout) this.getLayout(), recordButton, 6, 0, 1, 2, 1.0, 1.0, 0, 0, GridBagConstraints.BOTH, GridBagConstraints.LINE_START);

Expand Down Expand Up @@ -306,6 +307,19 @@ public void selectPerformance(Performance performance) {
}
}

/**
* select an audio programmatically
* @param audio
*/
public void selectAudio(Audio audio) {
for (int i = 0; i < this.audioChooser.getItemCount(); ++i) {
AudioChooserItem item = (AudioChooserItem) this.audioChooser.getItemAt(i);
if (item.getValue() == audio) {
this.audioChooser.setSelectedItem(item);
}
}
}

/**
* select the alignment to the currently chosen audio object; if no audio is selected, this has no effect
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package mpmToolbox.gui.syncPlayer.utilities;

import meico.supplementary.KeyValue;

import javax.sound.sampled.Mixer;

/**
* This represents a recording device entry in a combobox.
* @author Axel Berndt
*/
public class RecordingDeviceChooserItem extends KeyValue<String, Mixer.Info> {
/**
* constructor
* @param string
* @param device
*/
public RecordingDeviceChooserItem(String string, Mixer.Info device) {
super(string, device);
}

/**
* All combobox items require this method. The override here makes sure that the string being returned
* is the device name instead of some Java Object ID.
* @return
*/
@Override
public String toString() {
return this.getKey();
}
}
2 changes: 2 additions & 0 deletions src/mpmToolbox/projectData/ProjectData.java
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,8 @@ public synchronized ArrayList<Audio> getAudio() {
* @param audio
*/
public synchronized boolean addAudio(Audio audio) {
if (audio == null)
return false;
if (this.audio.contains(audio))
return false;
return this.audio.add(audio);
Expand Down

0 comments on commit b497e5a

Please sign in to comment.