[315] | 1 | package org.apollo.meldex;
|
---|
| 2 |
|
---|
| 3 | import java.io.File;
|
---|
| 4 | import java.io.IOException;
|
---|
| 5 |
|
---|
| 6 | import javax.sound.sampled.AudioFormat;
|
---|
| 7 |
|
---|
| 8 | import org.apollo.io.AudioIO;
|
---|
| 9 | import org.apollo.widgets.SampledTrackEvo4;
|
---|
| 10 |
|
---|
| 11 | /**
|
---|
| 12 | * A helpful util for conversion in the apollo project
|
---|
| 13 | *
|
---|
| 14 | * @author Brook Novak
|
---|
| 15 | */
|
---|
| 16 | public class MeldexConversion {
|
---|
| 17 |
|
---|
| 18 | private MeldexConversion() {}
|
---|
| 19 |
|
---|
| 20 | /**
|
---|
| 21 | *
|
---|
| 22 | * @param trackWidget
|
---|
| 23 | * @return
|
---|
| 24 | * @throws IOException
|
---|
| 25 | * If conversion fails.
|
---|
| 26 | */
|
---|
| 27 | public static Melody toMelody(SampledTrackEvo4 trackWidget) throws IOException {
|
---|
| 28 |
|
---|
| 29 | assert(trackWidget != null);
|
---|
| 30 |
|
---|
| 31 | byte[] bytes = trackWidget.getAudioBytes();
|
---|
| 32 | AudioFormat format = trackWidget.getAudioFormat();
|
---|
| 33 |
|
---|
| 34 | if (bytes != null && format != null) {
|
---|
| 35 | return toMelody(bytes, format);
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | String path = trackWidget.getLatestSavedAudioPath();
|
---|
| 39 | assert(path != null);
|
---|
| 40 |
|
---|
| 41 | File waveFile = new File(path);
|
---|
| 42 | if (!waveFile.exists()) return null;
|
---|
| 43 |
|
---|
| 44 | WavSample sampleTrack = new WavSample();
|
---|
| 45 |
|
---|
| 46 | if (!sampleTrack.loadFromFile(waveFile)) {
|
---|
| 47 | return null;
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | return toMelody(sampleTrack);
|
---|
| 51 |
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | /**
|
---|
| 55 | *
|
---|
| 56 | * @param bytes
|
---|
| 57 | * @param format
|
---|
| 58 | * @return
|
---|
| 59 | * @throws IOException
|
---|
| 60 | * If conversion fails.
|
---|
| 61 | */
|
---|
| 62 | public static Melody toMelody(byte[] bytes, AudioFormat format) throws IOException {
|
---|
| 63 | assert(bytes != null && bytes.length > 0);
|
---|
| 64 | assert(format != null);
|
---|
| 65 | WavSample sampleTrack = new WavSample(bytes, format);
|
---|
| 66 | return toMelody(sampleTrack);
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | /**
|
---|
| 70 | *
|
---|
| 71 | * @param sampleTrack
|
---|
| 72 | * @return
|
---|
| 73 | *
|
---|
| 74 | * @throws IOException
|
---|
| 75 | * If conversion fails.
|
---|
| 76 | */
|
---|
| 77 | public static Melody toMelody(WavSample sampleTrack) throws IOException {
|
---|
| 78 | assert(sampleTrack != null);
|
---|
| 79 |
|
---|
| 80 | Transcriber transcriber = new Transcriber();
|
---|
| 81 |
|
---|
| 82 | RogTrack transcribedQuery = transcriber.transcribeSample(sampleTrack);
|
---|
| 83 | System.out.println("toMelody: " + transcribedQuery);
|
---|
| 84 |
|
---|
| 85 | if (transcribedQuery != null) {
|
---|
| 86 | return transcribedQuery.toMelody();
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | return null;
|
---|
| 90 |
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | /**
|
---|
| 94 | * Converts PCM audio bytes to the corerct format for transcribing raw audio.
|
---|
| 95 | *
|
---|
| 96 | * @param source
|
---|
| 97 | * @param sourceFormat
|
---|
| 98 | * @return
|
---|
| 99 | * @throws IOException
|
---|
| 100 | */
|
---|
| 101 | public static byte[] toStandardizedFormat(byte[] source, AudioFormat sourceFormat, float sampleRate)
|
---|
| 102 | throws IOException {
|
---|
| 103 |
|
---|
| 104 | assert(source != null);
|
---|
| 105 | assert(sourceFormat != null);
|
---|
| 106 |
|
---|
| 107 |
|
---|
| 108 | // Get the standardised unsigned 8-bit mono data
|
---|
| 109 | AudioFormat partialStandardizedFormat = new AudioFormat(
|
---|
| 110 | sampleRate,
|
---|
| 111 | sourceFormat.getSampleSizeInBits(),
|
---|
| 112 | 1, // mono
|
---|
| 113 | sourceFormat.getSampleSizeInBits() != 8,
|
---|
| 114 | true);
|
---|
| 115 |
|
---|
| 116 | byte[] stdData = AudioIO.convertAudioBytes(
|
---|
| 117 | source,
|
---|
| 118 | sourceFormat,
|
---|
| 119 | partialStandardizedFormat);
|
---|
| 120 |
|
---|
| 121 | if (stdData == null) {
|
---|
| 122 | return null;
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | // Perform conversion to 8-bit unsigned
|
---|
| 126 | if (partialStandardizedFormat.getSampleSizeInBits() != 8) {
|
---|
| 127 | byte[] preData = stdData;
|
---|
| 128 | stdData = null;
|
---|
| 129 |
|
---|
| 130 | if (partialStandardizedFormat.getSampleSizeInBits() == 16) {
|
---|
| 131 |
|
---|
| 132 | int newSize = preData.length >> 1;
|
---|
| 133 | stdData = new byte[newSize];
|
---|
| 134 | int sample;
|
---|
| 135 | float fSample;
|
---|
| 136 |
|
---|
| 137 | for (int i = 0; i < newSize; i++) {
|
---|
| 138 | sample = preData[(i << 1)] << 8; // MSB - big endian
|
---|
| 139 | sample |= (preData[(i << 1) + 1] & 0xFF); // LSB - big endian
|
---|
| 140 |
|
---|
| 141 | fSample = sample;
|
---|
| 142 | fSample /= 256.0f;
|
---|
| 143 | fSample += Byte.MAX_VALUE;
|
---|
| 144 | sample = (int)fSample;
|
---|
| 145 | stdData[i] = (byte)(sample & 0xFF);
|
---|
| 146 |
|
---|
| 147 | //System.out.println(sample + " -> " + fSample + " -> " + stdData[i]);
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | preData = null;
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | return stdData;
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 |
|
---|
| 159 |
|
---|
| 160 |
|
---|
| 161 | }
|
---|