Skip to content

Commit

Permalink
adding label maker
Browse files Browse the repository at this point in the history
  • Loading branch information
dkohlsdorf committed Feb 25, 2024
1 parent fcf6902 commit 5e82fb4
Show file tree
Hide file tree
Showing 35 changed files with 2,515 additions and 0 deletions.
27 changes: 27 additions & 0 deletions dolphin_mnist/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Intellij files
*.iml
*.target

# Compiled class file
*.class

# Log file
*.log

# BlueJ files
*.ctxt

# Mobile Tools for Java (J2ME)
.mtj.tmp/

# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
21 changes: 21 additions & 0 deletions dolphin_mnist/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 Daniel Kohlsdorf

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
14 changes: 14 additions & 0 deletions dolphin_mnist/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Dolphin MNIST
A dataset creation tool for equally sized audio windows written in Java.
Below you can see the main user interface of the annotation tool.

<img src="images/dolphin_mnist.png"/>

In the spectrogram you see the spectrogram with the
selected region highlighted. You can label that area
using the commands at the side. You can use the controls
described at the side to navigate the spectrogram.
At the bottom you can see the raw audio.
The output of the program is an audio file with all the audio snippets:

<img src="images/spec_export.png"/>
Binary file added dolphin_mnist/images/dolphin_mnist.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added dolphin_mnist/images/spec_export.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
41 changes: 41 additions & 0 deletions dolphin_mnist/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.dkohl</groupId>
<artifactId>AudioMNIST</artifactId>
<version>1.0-SNAPSHOT</version>
<repositories>
<repository>
<id>Sonatype-public</id>
<name>SnakeYAML repository</name>
<url>http://oss.sonatype.org/content/groups/public/</url>
</repository>
</repositories>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.26</version>
</dependency>
<dependency>
<groupId>com.github.wendykierp</groupId>
<artifactId>JTransforms</artifactId>
<version>3.0</version>
</dependency>
</dependencies>
</project>
72 changes: 72 additions & 0 deletions dolphin_mnist/src/main/java/org/dkohl/wdp/io/Audio.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package org.dkohl.wdp.io;

import org.dkohl.wdp.spectrogram.Annotation;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

public class Audio {

public static double[] read(String file) throws IOException, WavFileException {
WavFile wavFile = WavFile.openWavFile(new File(file));
int numChannels = wavFile.getNumChannels();
long frames = wavFile.getNumFrames();
double data[] = new double[(int) frames];
double[] buffer = new double[100 * numChannels];
int framesRead = 0;
int t = 0;
do {
framesRead = wavFile.readFrames(buffer, 100);
// Average channels
for (int s = 0; s < numChannels * framesRead; s += numChannels) {
for (int c = 0; c < numChannels; c++) {
data[t] += buffer[s + c];
}
data[t] /= numChannels;
t++;
}
} while (framesRead != 0);
wavFile.close();
return data;
}

public static void write(String outputWav, String outputCsv, List<Annotation> annotations, AudioWritingUpdate update) throws IOException, WavFileException {
if(annotations.size() > 0) {
int length = (int) (annotations.get(0).getStop() - annotations.get(0).getStart());
int nFrames = length * annotations.size();
int offset = 0;
WavFile out = WavFile.newWavFile(new File(outputWav), 1, nFrames, 16, 44100);
FileWriter csvWriter = new FileWriter(new File(outputCsv));
csvWriter.write("offset, annotation, source_file, source_start, source_stop\n");
double audio[] = null;
String currentFilename = null;
int i = 0;
for (Annotation annotation : annotations) {
System.out.println("Writing: " + annotation);
if(audio == null || !annotation.getFile().equals(currentFilename)) {
audio = read(annotation.getFile());
currentFilename = annotation.getFile();
}
double snippet[] = Arrays.copyOfRange(audio, (int) annotation.getStart(), (int) annotation.getStop());
out.writeFrames(snippet, snippet.length);
csvWriter.write(String.format("%d,%s,%s,%d,%d\n",
offset,
annotation.getAnnotation(),
annotation.getFile(),
annotation.getStart(),
annotation.getStop()
));
offset += length;
update.progress((int) (i / (double) annotations.size()) * 100, outputWav);
i += 1;
}
csvWriter.close();
out.close();
update.progress(100, outputWav);
System.out.println("Done Writing");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.dkohl.wdp.io;

public interface AudioWritingUpdate {
public void progress(int percentageDone, String filename);
}
57 changes: 57 additions & 0 deletions dolphin_mnist/src/main/java/org/dkohl/wdp/io/Properties.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package org.dkohl.wdp.io;

public class Properties {

private int fftWin;
private int fftStep;
private int buffer;
private int mnistWin;
private int mnistStep;
private int mnistWinDFT;
private int mnistStepDFT;

public Properties(int fftWin, int fftStep, int buffer, int mnistWin, int mnistStep) {
this.fftWin = fftWin;
this.fftStep = fftStep;
this.buffer = buffer;
this.mnistWin = mnistWin;
this.mnistStep = mnistStep;
this.mnistWinDFT = mnistWin / fftStep;
this.mnistStepDFT = mnistStep / fftStep;

}

public static Properties defaultProperties() {
int fftWin = 1024;
int fftStep = 512;
int buffer = 8 * 44100;
int mnistWin = 44100 / 8;
int mnistStep = 44100 / 32;
return new Properties(fftWin, fftStep, buffer, mnistWin, mnistStep);
}

public int getFftWin() {
return fftWin;
}

public int getFftStep() {
return fftStep;
}

public int getBuffer() {
return buffer;
}

public int getMnistWin() {
return mnistWin;
}

public int getMnistStep() {
return mnistStep;
}

public int getMnistStepDFT() { return mnistStepDFT; }

public int getMnistWinDFT() { return mnistWinDFT; }

}
103 changes: 103 additions & 0 deletions dolphin_mnist/src/main/java/org/dkohl/wdp/io/StdAudio.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package org.dkohl.wdp.io;
/******************************************************************************
* Compilation: javac StdAudio.java
* Execution: java StdAudio
* Dependencies: none
*
* Simple library for reading, writing, and manipulating .wav files.
*
* https://introcs.cs.princeton.edu/java/15inout/StdAudio.java
*
* Limitations
* -----------
* - Assumes the audio is monaural, little endian, with sampling rate
* of 44,100
* - check when reading .wav files from a .jar file ?
*
******************************************************************************/

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;

/**
* <i>Standard audio</i>. This class provides a basic capability for
* creating, reading, and saving audio.
* <p>
* The audio format uses a sampling rate of 44,100 Hz, 16-bit, monaural.
*
* <p>
* For additional documentation, see <a href="https://introcs.cs.princeton.edu/15inout">Section 1.5</a> of
* <i>Computer Science: An Interdisciplinary Approach</i> by Robert Sedgewick and Kevin Wayne.
*
* @author Robert Sedgewick
* @author Kevin Wayne
*/
public final class StdAudio {

public static final int SAMPLE_RATE = 44100;

private static final int BYTES_PER_SAMPLE = 2; // 16-bit audio
private static final int BITS_PER_SAMPLE = 16; // 16-bit audio
private static final double MAX_16_BIT = 32768;
private static final int SAMPLE_BUFFER_SIZE = 4096;

private static final int MONO = 1;
private static final int STEREO = 2;
private static final boolean LITTLE_ENDIAN = false;
private static final boolean BIG_ENDIAN = true;
private static final boolean SIGNED = true;
private static final boolean UNSIGNED = false;


private static SourceDataLine line; // to play the sound
private static byte[] buffer; // our internal buffer
private static int bufferSize = 0; // number of samples currently in internal buffer

private StdAudio() { }

static { init(); }

private static void init() {
try {
AudioFormat format = new AudioFormat((float) SAMPLE_RATE, BITS_PER_SAMPLE, MONO, SIGNED, LITTLE_ENDIAN);
DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
line = (SourceDataLine) AudioSystem.getLine(info);
line.open(format, SAMPLE_BUFFER_SIZE * BYTES_PER_SAMPLE);
buffer = new byte[SAMPLE_BUFFER_SIZE * BYTES_PER_SAMPLE/3];
}
catch (LineUnavailableException e) {
System.out.println(e.getMessage());
}
line.start();
}

public static void close() {
line.drain();
line.stop();
}

public static void play(double sample) {
if (Double.isNaN(sample)) throw new IllegalArgumentException("sample is NaN");
if (sample < -1.0) sample = -1.0;
if (sample > +1.0) sample = +1.0;
short s = (short) (MAX_16_BIT * sample);
if (sample == 1.0) s = Short.MAX_VALUE; // special case since 32768 not a short
buffer[bufferSize++] = (byte) s;
buffer[bufferSize++] = (byte) (s >> 8); // little endian
if (bufferSize >= buffer.length) {
line.write(buffer, 0, buffer.length);
bufferSize = 0;
}
}

public static void play(double[] samples) {
if (samples == null) throw new IllegalArgumentException("argument to play() is null");
for (int i = 0; i < samples.length; i++) {
play(samples[i]);
}
}

}
Loading

0 comments on commit 5e82fb4

Please sign in to comment.