SYS-CON MEDIA Authors: Pat Romanski, Gary Arora, Zakia Bouachraoui, Yeshim Deniz, Liz McMillan

Article

Migration from the SUN.AUDIO Packages

ULawCodec and the JAVAX.SUN API

Abstract

In Java's beginning, the only option for playing audio through the speakers was to use the "SUN.AUDIO" packages. These packages have been with us since jdk1.0 (1996). As of jdk9 (2017-09-21) the SUN packages are longer available. This paper addresses the migration toward the Java Sound API.

1. Introduction

We are faced with a large and useful audio API whose support is being withdrawn. More over, we have a large legacy code base with little support for migration to the new jdk9 compatible APIs.

The following sections outline the problem and our proposed solution.
Section 2 discusses File Formats. Section 3 discusses the SUN.AUDIO API. Section 4 discusses the JAVAX.SOUND API. Section 5 discusses the refactoring of the existing code so as to make minimal changes to the code base. Finally, Section 6 summarizes our findings.

2. File Formats

In the past, audio was played through speakers using an 8-bit, 8khz sample rate companded format called the "mu-law" encoding [Lyon97]. The basic idea behind this format was that low-level amplitudes would be given more bits of dynamic range than the high-level amplitudes (this is known as perceptual coding).

These packages were capable of reading and playing 1 byte per sample
ITU G.711 μ-law, mono 8000 Hz encoded files. The companding was first described in 1965 by a phone company publication and was designed for voice grade audio [BTL]. The compression is given by:

F(x)=sgn(x)(ln(1+μ*|x|)/ln(1+μ)
where u = 255 (8 bits) and x ranges from [-1,1].
The inverse function is:
f(y)=sgn(y)(1/μ)((1+μ)|y| -1)
where the range on y is [-1,1].
In 1989, the Sun SPARCstation 1 had hardware for playing mu-law files. As a result, μ-law was incorporated into the sun.audio API and this quickly became a Java standard. For speech, mu-law is fine, but CD audio (44.1 KHz samples/second in 16 bit stereo) is now far more common. For 8-bit linear PCM (Pulse Code Modulation), we expect an SNR (Signal to Noise Ratio) of 4.8 + 6*8=52.8 dB. For the 16 bit linear PCM, we expect an SNR of around 4.8 + 6*16 = 100.8 dB of SNR. Companding can make things better (or worse) depending on the audio level (lower amplitudes get a better SNR than higher ones) [Carlson].


3. The SUN.AUDIO API


In the mid to late 90's, the sun.audio package was the only way to play audio. The mu-law encoded system, native to the SUN.AUDIO package, relied upon C and C++ libraries to emit and digitize sound. In support of the new package, we authored a toolkit embodied in the ULawCodec class. This was was first published in 1997 and then updated in 2008 [Lyon97] [Lyon08G]. For example:

Listing 1, ULawCodec.java, Excerpt

import sun.audio.AudioData;
import sun.audio.AudioDataStream;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;
.....
public static byte[] readData(InputStream is) throws IOException {
AudioStream as = new AudioStream(is);
// AudioStream constructor
// expects data stream from AU file as input
int length = as.getLength();
if (length < 0) return null;
byte b[] = new byte[length];
as.read(b, 0, length);
return b;
}
//to save an au file, we used:
public void saveAuFile(String fileName) {
try {
FileOutputStream fos = new FileOutputStream(fileName);
DataOutputStream os = new
DataOutputStream(fos);

// not too much magic about it,
// just ".snd" ASCII string represented as
//integer

os.writeInt(0x2E736E64); // magic
os.writeInt(0x00000020); // offset of the data
os.writeInt(ulawData.length); // data size

// good old 8 bit per sample u-law encoded data format (code = 1)

os.writeInt(0x00000001); // format code
os.writeInt(0x00001F40); // sampling rate
os.writeInt(0x00000001); // channel count
os.writeInt(0x00000000); // reserved
os.writeInt(0x00000000); // reserved
int off = 0;
os.write(ulawData, off, ulawData.length);
fos.close();
} catch (Exception e) {
System.out.println(e);
}
}
and to play an au file, we used:
public void run() {
try {
stop();
AudioData audioData = new AudioData(ulawData);
audioDataStream =
new AudioDataStream(audioData);
AudioPlayer.player.start(audioDataStream);
Thread.sleep(ulawData.length / 8 + 100);
} catch (Exception ignored) {
}
}


The reader may be tempted (as we were) to make use of the source code in the SUN.AUDIO package with renaming to avoid deprecation signals and to enable maintenance. This type of a port will fail because jdk9 because attempts to load native methods that are not present. In short, we need to find a toolkit compatible way port our code from the SUN.AUDIO API to the JAVAX.SOUND API.

4. The JAVAX.SOUND API

In this section we show how to make use of the JAVAX.SOUND API to convert mu-law from a file and play it. For our test data, we start with raw u-law data of type:
ULAW 8000.0 Hz, 8 bit, mono, 1 bytes/frame. See Listing 2:

import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
//note, the sun.audio is now removed...
public static void playUlaw(byte[] b)
throws UnsupportedAudioFileException, IOException,
LineUnavailableException {
ByteArrayInputStream bis = new ByteArrayInputStream(b);
AudioInputStream ais = AudioSystem.getAudioInputStream(bis);
int sampleRate = 8000;
int sampleSizeInBits = 16;
int channels = 1;
boolean signed = true;
boolean bigEndian = false;
Clip clip = AudioSystem.getClip();
AudioFormat targetFormat = new AudioFormat(sampleRate,
sampleSizeInBits,
channels, signed,
bigEndian
);
AudioInputStream convertedStream = AudioSystem
.getAudioInputStream(targetFormat, ais);
// open audioInputStream to the clip
clip.open(convertedStream);
clip.setFramePosition(0);
clip.loop(0);
clip.start();
long timeInMs = 1000*b.length/sampleRate;
try {
Thread.sleep(timeInMs);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

The key here is we are converting the companded data into linear PCM type data, prior to playing. This enables the JAVAX.SOUND API to play the files.

5. Refactoring Under the Hood

After creating and testing the JAVAX.SOUND-based ULawCodec, called ULawCodec2, I performed a local rename of ULawCodec to ULawCodecOld (that is, a simple string change and file name update). Naturally, all classes that make reference to ULawCodec now will point to the new version (another local string change on UlawCodec2).With all the references now satisfied, I retired the ULawCodecOld, thus completing the port.The UlawCodec is used in 200 places in my code base (8,000+ Java files and 13k class files). A rescue of the old code is not just a preservation of legacy code, but a way to help salvage an a 20+ year-old book [Lyon97].

6. Summary

After a great deal of time an effort, we have been able to salvage what was left of a bad situation, Oracles' deprecation and removal of the SUN.AUDIO package from modern jdks. The cost to the computer science community for this type of deprecation is hard to estimate, but was anticipates [Lyon12A]. The JavaSound API dates from 2004, it is past time for us to update it with a new API. Keeping up with the deprecations from Oracle is a full-time job (what a shame Oracle is not more active in supporting the Java API). Saving Java3D with an update for JOGL, for example was the subject of [Lyon18].


References Cited

[BTL] 1965. "Transmission Systems for Communications", by Members of the Technical Staff, Bell Telephone Laboratories, Western Electric Company, Inc., Technical Publications, Winston-Salem, NC. Mu-law companding is described on page 577-580.

[Carlson] Communication Systems, by A. Bruce Carlson, McGraw Hill, 1986.

[Lyon18] "Jogl and Java3D: The State of the Java Graphics Libraries" by Douglas A. Lyon, Java Enterprise Edition Journal", Feb. 15,. 2018

[Lyon12A] "The Java Tree Withers" by Douglas A. Lyon, IEEE Computer, Jan. 2012, pp. 83-85.

[Lyon08G] "The U-Law CODEC",by Douglas A. Lyon, Journal of Object Technology, vol. 7, no. 8, November-December 2008, pp. 17-31.

[Lyon97] "Java Digital Signal Processing", Douglas A. Lyon and H. Rao, Henry Holt. November 1997.

More Stories By Douglas Lyon

Douglas A. Lyon is a Professor in the Electrical and Computer Systems Engineering department at Fairfield University, in Fairfield Connecticut, a licensed professional engineer, a senior member of the IEEE, President of DocJava, Inc. and President of the Inventors Association of Connecticut. Dr. Lyon teaches Engineering Enpreneurship and has brought one successful kickstarter project to market. He received the Ph.D., M.S. and B.S.degrees in computer and systems engineering from Rensselaer Polytechnic Institute (1991, 1985 and 1983). Dr. Lyon has worked at AT&T Bell Laboratories and the Jet Propulsion Laboratory. Dr. Lyon has authored or co-authored three books (Java Digital Signal Processing, Image Processing in Java and Java for Programmers). He has authored over 49 journal publications.

Latest Stories
While a hybrid cloud can ease that transition, designing and deploy that hybrid cloud still offers challenges for organizations concerned about lack of available cloud skillsets within their organization. Managed service providers offer a unique opportunity to fill those gaps and get organizations of all sizes on a hybrid cloud that meets their comfort level, while delivering enhanced benefits for cost, efficiency, agility, mobility, and elasticity.
Isomorphic Software is the global leader in high-end, web-based business applications. We develop, market, and support the SmartClient & Smart GWT HTML5/Ajax platform, combining the productivity and performance of traditional desktop software with the simplicity and reach of the open web. With staff in 10 timezones, Isomorphic provides a global network of services related to our technology, with offerings ranging from turnkey application development to SLA-backed enterprise support. Leadin...
DevOps has long focused on reinventing the SDLC (e.g. with CI/CD, ARA, pipeline automation etc.), while reinvention of IT Ops has lagged. However, new approaches like Site Reliability Engineering, Observability, Containerization, Operations Analytics, and ML/AI are driving a resurgence of IT Ops. In this session our expert panel will focus on how these new ideas are [putting the Ops back in DevOps orbringing modern IT Ops to DevOps].
Darktrace is the world's leading AI company for cyber security. Created by mathematicians from the University of Cambridge, Darktrace's Enterprise Immune System is the first non-consumer application of machine learning to work at scale, across all network types, from physical, virtualized, and cloud, through to IoT and industrial control systems. Installed as a self-configuring cyber defense platform, Darktrace continuously learns what is ‘normal' for all devices and users, updating its understa...
Enterprises are striving to become digital businesses for differentiated innovation and customer-centricity. Traditionally, they focused on digitizing processes and paper workflow. To be a disruptor and compete against new players, they need to gain insight into business data and innovate at scale. Cloud and cognitive technologies can help them leverage hidden data in SAP/ERP systems to fuel their businesses to accelerate digital transformation success.
Most organizations are awash today in data and IT systems, yet they're still struggling mightily to use these invaluable assets to meet the rising demand for new digital solutions and customer experiences that drive innovation and growth. What's lacking are potent and effective ways to rapidly combine together on-premises IT and the numerous commercial clouds that the average organization has in place today into effective new business solutions.
Concerns about security, downtime and latency, budgets, and general unfamiliarity with cloud technologies continue to create hesitation for many organizations that truly need to be developing a cloud strategy. Hybrid cloud solutions are helping to elevate those concerns by enabling the combination or orchestration of two or more platforms, including on-premise infrastructure, private clouds and/or third-party, public cloud services. This gives organizations more comfort to begin their digital tr...
Keeping an application running at scale can be a daunting task. When do you need to add more capacity? Larger databases? Additional servers? These questions get harder as the complexity of your application grows. Microservice based architectures and cloud-based dynamic infrastructures are technologies that help you keep your application running with high availability, even during times of extreme scaling. But real cloud success, at scale, requires much more than a basic lift-and-shift migrati...
David Friend is the co-founder and CEO of Wasabi, the hot cloud storage company that delivers fast, low-cost, and reliable cloud storage. Prior to Wasabi, David co-founded Carbonite, one of the world's leading cloud backup companies. A successful tech entrepreneur for more than 30 years, David got his start at ARP Instruments, a manufacturer of synthesizers for rock bands, where he worked with leading musicians of the day like Stevie Wonder, Pete Townsend of The Who, and Led Zeppelin. David has ...
Darktrace is the world's leading AI company for cyber security. Created by mathematicians from the University of Cambridge, Darktrace's Enterprise Immune System is the first non-consumer application of machine learning to work at scale, across all network types, from physical, virtualized, and cloud, through to IoT and industrial control systems. Installed as a self-configuring cyber defense platform, Darktrace continuously learns what is ‘normal' for all devices and users, updating its understa...
Dion Hinchcliffe is an internationally recognized digital expert, bestselling book author, frequent keynote speaker, analyst, futurist, and transformation expert based in Washington, DC. He is currently Chief Strategy Officer at the industry-leading digital strategy and online community solutions firm, 7Summits.
Addteq is a leader in providing business solutions to Enterprise clients. Addteq has been in the business for more than 10 years. Through the use of DevOps automation, Addteq strives on creating innovative solutions to solve business processes. Clients depend on Addteq to modernize the software delivery process by providing Atlassian solutions, create custom add-ons, conduct training, offer hosting, perform DevOps services, and provide overall support services.
Contino is a global technical consultancy that helps highly-regulated enterprises transform faster, modernizing their way of working through DevOps and cloud computing. They focus on building capability and assisting our clients to in-source strategic technology capability so they get to market quickly and build their own innovation engine.
When applications are hosted on servers, they produce immense quantities of logging data. Quality engineers should verify that apps are producing log data that is existent, correct, consumable, and complete. Otherwise, apps in production are not easily monitored, have issues that are difficult to detect, and cannot be corrected quickly. Tom Chavez presents the four steps that quality engineers should include in every test plan for apps that produce log output or other machine data. Learn the ste...
Digital Transformation is much more than a buzzword. The radical shift to digital mechanisms for almost every process is evident across all industries and verticals. This is often especially true in financial services, where the legacy environment is many times unable to keep up with the rapidly shifting demands of the consumer. The constant pressure to provide complete, omnichannel delivery of customer-facing solutions to meet both regulatory and customer demands is putting enormous pressure on...