Skip to content

Commit

Permalink
Merge pull request #20 from umjammer/1.0.20
Browse files Browse the repository at this point in the history
1.0.20
  • Loading branch information
umjammer authored Nov 30, 2024
2 parents 2648174 + 94fec78 commit dfa75dd
Show file tree
Hide file tree
Showing 200 changed files with 1,287 additions and 780 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ A. yes you can, follow those steps
* ~~`tritonus:tritonus-remaining:org.tritonus.sampled.file.WaveAudioFileReader`~~
* use service provider for mfi, smaf sequencer
* service loader instead of vavi.properties
* [RTTTL (Ringing Tones text transfer language)](https://web.archive.org/web/20070704033948/http://www.convertyourtone.com/rtttl.html)
* https://github.com/SatyrDiamond/adpcm

---
<sub>images by <a href="https://www.silhouette-illust.com/illust/49312">melody</a>, <a href="https://www.silhouette-illust.com/illust/257">cellphone</a></sub>
12 changes: 7 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<groupId>vavi</groupId>
<artifactId>vavi-sound</artifactId>
<version>1.0.19</version>
<version>1.0.20</version>

<name>Vavi Sound API</name>
<url>https://github.com/umjammer/vavi-sound</url>
Expand Down Expand Up @@ -135,7 +135,7 @@ TODO
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<version>3.12.1</version>
<configuration>
<release>17</release>
</configuration>
Expand All @@ -160,9 +160,11 @@ TODO
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.2</version>
<version>3.2.5</version>
<configuration>
<argLine>
--add-opens=java.base/java.io=ALL-UNNAMED
--add-opens=java.base/sun.nio.ch=ALL-UNNAMED
-Djava.util.logging.config.file=${project.build.testOutputDirectory}/logging.properties
-Dvavi.test.dev.null=@{dev.null}
-Dvavi.test.volume=@{vavi.test.volume}
Expand Down Expand Up @@ -191,7 +193,7 @@ TODO
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>5.10.2</version>
<version>5.10.3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
Expand All @@ -202,7 +204,7 @@ TODO
<dependency>
<groupId>com.github.umjammer</groupId> <!-- vavi / com.github.umjammer -->
<artifactId>vavi-commons</artifactId>
<version>1.1.13</version>
<version>1.1.14</version>
</dependency>
<dependency>
<groupId>com.github.umjammer</groupId>
Expand Down
61 changes: 61 additions & 0 deletions src/main/java/vavi/sound/SoundUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,18 @@

package vavi.sound;

import java.io.BufferedInputStream;
import java.io.FilterInputStream;
import java.lang.System.Logger;
import java.lang.System.Logger.Level;
import java.lang.reflect.Field;
import java.net.URI;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.FloatControl;

import static java.lang.System.Logger.Level.DEBUG;
import static java.lang.System.getLogger;


/**
* SoundUtil.
Expand All @@ -18,6 +27,8 @@
*/
public final class SoundUtil {

private static final Logger logger = getLogger(SoundUtil.class.getName());

private SoundUtil() {}

/**
Expand All @@ -29,4 +40,54 @@ public static void volume(DataLine line, double gain) {
float dB = (float) (Math.log10(gain) * 20.0);
gainControl.setValue(dB);
}

/**
* Gets a source path from an input stream.
* <p>
* java runtime option
* <ol>
* <ul>{@code --add-opens=java.base/java.io=ALL-UNNAMED}</ul>
* <ul>{@code --add-opens=java.base/sun.nio.ch=ALL-UNNAMED}</ul>
* </ol>
*
* @param object input stream
* @return source object as URI, nullable
*/
public static URI getSource(Object object) {
Class<?> c = object.getClass();
try {
do {
logger.log(DEBUG, "object1: " + c.getName());
if (object instanceof BufferedInputStream) {
Field pathField = FilterInputStream.class.getDeclaredField("in");
pathField.setAccessible(true);
object = pathField.get(object);
}
if (object instanceof java.io.FileInputStream) {
Field pathField = object.getClass().getDeclaredField("path");
pathField.setAccessible(true);
String path = (String) pathField.get(object);
logger.log(DEBUG, "source: java.io.FileInputStream: path : " + path);
return path != null ? URI.create(path) : null;
}
if (object.getClass().getName().equals("sun.nio.ch.ChannelInputStream")) { // because it's package private
Field pathField = object.getClass().getDeclaredField("ch");
pathField.setAccessible(true);
object = pathField.get(object);
}
if (object.getClass().getName().equals("sun.nio.ch.FileChannelImpl")) { // because it's package private
Field pathField = object.getClass().getDeclaredField("path");
pathField.setAccessible(true);
String path = (String) pathField.get(object);
logger.log(DEBUG, "source: sun.nio.ch.FileChannelImpl: path : " + path);
return path != null ? URI.create(path) : null;
}
logger.log(DEBUG, "object2: " + object.getClass().getName());
c = c.getSuperclass();
} while (c.getSuperclass() != null);
} catch (Exception e) {
logger.log(Level.WARNING, e.getMessage(), e);
}
return null;
}
}
12 changes: 6 additions & 6 deletions src/main/java/vavi/sound/adpcm/AdpcmInputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ public AdpcmInputStream(InputStream in, ByteOrder byteOrder, int bits, ByteOrder
super(new BitInputStream(in, bits, bitOrder));
this.byteOrder = byteOrder;
this.decoder = getCodec();
//logger.log(Level.DEBUG, this.in);
//logger.log(Level.TRACE, this.in);
}

/** ADPCM (4bit) length */
@Override
public int available() throws IOException {
//logger.log(Level.DEBUG, "0: " + in.available() + ", " + ((in.available() * 2) + (rest ? 1 : 0)));
//logger.log(Level.TRACE, "0: " + in.available() + ", " + ((in.available() * 2) + (rest ? 1 : 0)));
// TODO "* 2" calc should be in bits?
return (in.available() * 2) + (rest ? 1 : 0);
}
Expand All @@ -77,26 +77,26 @@ public int available() throws IOException {
*/
@Override
public int read() throws IOException {
//logger.log(Level.DEBUG, in);
//logger.log(Level.TRACE, in);
if (!rest) {
int adpcm = in.read();
//logger.log(Level.DEBUG, "0: " + StringUtil.toHex2(adpcm));
//logger.log(Level.TRACE, "0: " + StringUtil.toHex2(adpcm));
if (adpcm == -1) {
return -1;
}

current = decoder.decode(adpcm);

rest = true;
//logger.log(Level.DEBUG, "1: " + StringUtil.toHex2(current & 0xff));
//logger.log(Level.TRACE, "1: " + StringUtil.toHex2(current & 0xff));
if (ByteOrder.BIG_ENDIAN.equals(byteOrder)) {
return (current & 0xff00) >> 8;
} else {
return current & 0xff;
}
} else {
rest = false;
//logger.log(Level.DEBUG, "2: " + StringUtil.toHex2((current & 0xff00) >> 8));
//logger.log(Level.TRACE, "2: " + StringUtil.toHex2((current & 0xff00) >> 8));
if (ByteOrder.BIG_ENDIAN.equals(byteOrder)) {
return current & 0xff;
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/vavi/sound/adpcm/AdpcmOutputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public AdpcmOutputStream(OutputStream out, ByteOrder byteOrder, int bits, ByteOr
super(new BitOutputStream(out, bits, bitOrder));
this.byteOrder = byteOrder;
this.encoder = getCodec();
//logger.log(Level.DEBUG, this.out);
//logger.log(Level.TRACE, this.out);
}

/** remaining or not (having PCM L or H at least one side) */
Expand All @@ -74,7 +74,7 @@ public void write(int b) throws IOException {
current -= 0x10000;
}
}
//logger.log(Level.DEBUG, "current: " + StringUtil.toHex4(current));
//logger.log(Level.TRACE, "current: " + StringUtil.toHex4(current));
out.write(encoder.encode(current)); // BitOutputStream write 4bit

flushed = true;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/vavi/sound/adpcm/ccitt/G711.java
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ protected int ulaw2alaw(int uval) {
0x55 ^ (_u2a[0x7f ^ uval] - 1);
}

//-------------------------------------------------------------------------
// ----

/**
* Quantizes the input val against the table of size short integers.
Expand Down
22 changes: 11 additions & 11 deletions src/main/java/vavi/sound/adpcm/ccitt/G721.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,11 @@ public int encode(int sl) {
} else if (AudioFormat.Encoding.ULAW.equals(encoding)) {
sl = ulaw2linear(sl) >> 2;
} else if (AudioFormat.Encoding.PCM_SIGNED.equals(encoding)) {
//logger.log(Level.DEBUG, "---- " + ccc + " ----");
//logger.log(Level.TRACE, "---- " + ccc + " ----");
//ccc++;
//logger.log(Level.DEBUG, "sl:B:\t" + sl);
//logger.log(Level.TRACE, "sl:B:\t" + sl);
sl >>= 2; // 14-bit dynamic range
//logger.log(Level.DEBUG, "sl:A:\t" + sl);
//logger.log(Level.TRACE, "sl:A:\t" + sl);
} else {
throw new IllegalArgumentException(encoding.toString());
}
Expand All @@ -109,8 +109,8 @@ public int encode(int sl) {
int sez = sezi >> 1;
int se = (sezi + state.getPolePredictor()) >> 1; // estimated signal

//logger.log(Level.DEBUG, "sl:\t" + sl);
//logger.log(Level.DEBUG, "se:\t" + se);
//logger.log(Level.TRACE, "sl:\t" + sl);
//logger.log(Level.TRACE, "se:\t" + se);
// SUBTA
int d = sl - se; // estimation difference

Expand Down Expand Up @@ -153,12 +153,12 @@ public int decode(int i) {
int sei = sezi + state.getPolePredictor();
int se = sei >> 1; // se = estimated signal

//logger.log(Level.DEBUG, "---- (" + (ccc++) + ")");
//logger.log(Level.DEBUG, "i:\t" + StringUtil.toHex2(i));
//logger.log(Level.DEBUG, "sezi:\t" + sezi);
//logger.log(Level.DEBUG, "sez:\t" + sez);
//logger.log(Level.DEBUG, "sei:\t" + sei);
//logger.log(Level.DEBUG, "se:\t" + se);
//logger.log(Level.TRACE, "---- (" + (ccc++) + ")");
//logger.log(Level.TRACE, "i:\t" + StringUtil.toHex2(i));
//logger.log(Level.TRACE, "sezi:\t" + sezi);
//logger.log(Level.TRACE, "sez:\t" + sez);
//logger.log(Level.TRACE, "sei:\t" + sei);
//logger.log(Level.TRACE, "se:\t" + se);

// MIX
int y = state.getStepSize(); // dynamic quantizer step size
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/vavi/sound/adpcm/ccitt/G723_16InputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ protected Codec getCodec() {
public G723_16InputStream(InputStream in, ByteOrder byteOrder) {
super(in, byteOrder, 2, ByteOrder.LITTLE_ENDIAN);
((G723_16) decoder).setEncoding(encoding);
//logger.log(Level.DEBUG, this.in);
//logger.log(Level.TRACE, this.in);
}

/** ADPCM (4bit) length */
@Override
public int available() throws IOException {
//logger.log(Level.DEBUG, "0: " + in.available() + ", " + ((in.available() * 2) + (rest ? 1 : 0)));
//logger.log(Level.TRACE, "0: " + in.available() + ", " + ((in.available() * 2) + (rest ? 1 : 0)));
return (in.available() * 4) + (rest ? 1 : 0); // TODO check * 4 ???
}
}
26 changes: 13 additions & 13 deletions src/main/java/vavi/sound/adpcm/ccitt/State.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class State {
/** delayed tone detect, new in 1988 version */
private int td;

//----
// ----

/** */
private static final int[] power2 = {
Expand Down Expand Up @@ -147,7 +147,7 @@ private static int fmult(int an, int srn) {
int retval = (wanexp >= 0) ? ((wanmant << wanexp) & 0x7fff) :
(wanmant >> -wanexp);

//logger.log(Level.DEBUG, an + ", " + srn + ": " + ((an ^ srn) < 0 ? -retval : retval));
//logger.log(Level.TRACE, an + ", " + srn + ": " + ((an ^ srn) < 0 ? -retval : retval));
return (an ^ srn) < 0 ? -retval : retval;
}

Expand Down Expand Up @@ -208,10 +208,10 @@ public int getStepSize() {
*/
public void update(int code_size, int y, int wi, int fi, int _dq, int _sr, int dqsez) {

//logger.log(Level.DEBUG, "y:\t" + y);
//logger.log(Level.DEBUG, "dq:\t" + _dq);
//logger.log(Level.DEBUG, "sr:\t" + _sr);
//logger.log(Level.DEBUG, "dqsez:\t" + dqsez);
//logger.log(Level.TRACE, "y:\t" + y);
//logger.log(Level.TRACE, "dq:\t" + _dq);
//logger.log(Level.TRACE, "sr:\t" + _sr);
//logger.log(Level.TRACE, "dqsez:\t" + dqsez);

// needed in updating predictor poles
int pk0 = (dqsez < 0) ? 1 : 0;
Expand Down Expand Up @@ -346,33 +346,33 @@ public void update(int code_size, int y, int wi, int fi, int _dq, int _sr, int d
// FLOAT A : convert dq[0] to 4-bit exp, 6-bit mantissa f.p.
if (mag == 0) {
dq[0] = (_dq >= 0) ? 0x20 : -992;
//logger.log(Level.DEBUG, "dq[0]:1: " + dq[0]);
//logger.log(Level.TRACE, "dq[0]:1: " + dq[0]);
} else {
exp = quan(mag);
dq[0] = (_dq >= 0) ?
(exp << 6) + ((mag << 6) >> exp) :
(exp << 6) + ((mag << 6) >> exp) - 0x400;
//logger.log(Level.DEBUG, "dq[0]:2: " + dq[0] + ", " + _dq + ", " + exp + ", " + mag);
//logger.log(Level.DEBUG, "dq[0]:-: " + (exp << 6) + ", " + ((mag << 6) >> exp));
//logger.log(Level.TRACE, "dq[0]:2: " + dq[0] + ", " + _dq + ", " + exp + ", " + mag);
//logger.log(Level.TRACE, "dq[0]:-: " + (exp << 6) + ", " + ((mag << 6) >> exp));
}

sr[1] = sr[0];
// FLOAT B : convert sr to 4-bit exp., 6-bit mantissa f.p.
if (_sr == 0) {
sr[0] = 0x20;
//logger.log(Level.DEBUG, "sr[0]:1: " + sr[0]);
//logger.log(Level.TRACE, "sr[0]:1: " + sr[0]);
} else if (_sr > 0) {
exp = quan(_sr);
sr[0] = (exp << 6) + ((_sr << 6) >> exp);
//logger.log(Level.DEBUG, "sr[0]:2: " + sr[0]);
//logger.log(Level.TRACE, "sr[0]:2: " + sr[0]);
} else if (_sr > -32768) {
mag = -_sr;
exp = quan(mag);
sr[0] = (exp << 6) + ((mag << 6) >> exp) - 0x400;
//logger.log(Level.DEBUG, "sr[0]:3: " + sr[0]);
//logger.log(Level.TRACE, "sr[0]:3: " + sr[0]);
} else {
sr[0] = -992;
//logger.log(Level.DEBUG, "sr[0]:4: " + sr[0]);
//logger.log(Level.TRACE, "sr[0]:4: " + sr[0]);
}

// DELAY A
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/vavi/sound/adpcm/ccitt/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ Provides CCITT recommendation audio compression related classes.
* encoder.exe is little endian
* it's necessary to provide '16Bit Signed' value when `PCM_SIGNED`


* G.711 ... VoIP

## TODO

* 3, 5bit
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/vavi/sound/adpcm/dvi/Dvi.java
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public int decode(int input) {

//
int valPrev = state.valPrev;
//logger.log(Level.DEBUG, "{}", valPrev);
//logger.log(Level.TRACE, "{}", valPrev);
// Current step change index
int index = state.index;

Expand Down
Loading

0 comments on commit dfa75dd

Please sign in to comment.