SYS-CON MEDIA Authors: Liz McMillan, Elizabeth White, Pat Romanski, Gary Arora, Zakia Bouachraoui

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
Most modern computer languages embed a lot of metadata in their application. We show how this goldmine of data from a runtime environment like production or staging can be used to increase profits. Adi conceptualized the Crosscode platform after spending over 25 years working for large enterprise companies like HP, Cisco, IBM, UHG and personally experiencing the challenges that prevent companies from quickly making changes to their technology, due to the complexity of their enterprise. An accomp...
Alan Hase is Vice President of Engineering and Chief Development Officer at Big Switch. Alan has more than 20 years of experience in the networking industry and leading global engineering teams which have delivered industry leading innovation in high end routing, security, fabric and wireless technologies. Alan joined Big Switch from Extreme Networks where he was responsible for product strategy for its secure campus switching, intelligent mobility and campus orchestration products. Prior to Ext...
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...
On-premise or off, you have powerful tools available to maximize the value of your infrastructure and you demand more visibility and operational control. Fortunately, data center management tools keep a vigil on memory contestation, power, thermal consumption, server health, and utilization, allowing better control no matter your cloud's shape. In this session, learn how Intel software tools enable real-time monitoring and precise management to lower operational costs and optimize infrastructure...
Public clouds dominate IT conversations but the next phase of cloud evolutions are "multi" hybrid cloud environments. The winners in the cloud services industry will be those organizations that understand how to leverage these technologies as complete service solutions for specific customer verticals. In turn, both business and IT actors throughout the enterprise will need to increase their engagement with multi-cloud deployments today while planning a technology strategy that will constitute a ...
Cloud-Native thinking and Serverless Computing are now the norm in financial services, manufacturing, telco, healthcare, transportation, energy, media, entertainment, retail and other consumer industries, as well as the public sector. The widespread success of cloud computing is driving the DevOps revolution in enterprise IT. Now as never before, development teams must communicate and collaborate in a dynamic, 24/7/365 environment. There is no time to wait for long development cycles that pro...
At CloudEXPO Silicon Valley, June 24-26, 2019, Digital Transformation (DX) is a major focus with expanded DevOpsSUMMIT and FinTechEXPO programs within the DXWorldEXPO agenda. Successful transformation requires a laser focus on being data-driven and on using all the tools available that enable transformation if they plan to survive over the long term. A total of 88% of Fortune 500 companies from a generation ago are now out of business. Only 12% still survive. Similar percentages are found throug...
Every organization is facing their own Digital Transformation as they attempt to stay ahead of the competition, or worse, just keep up. Each new opportunity, whether embracing machine learning, IoT, or a cloud migration, seems to bring new development, deployment, and management models. The results are more diverse and federated computing models than any time in our history.
Andrew Keys is co-founder of ConsenSys Enterprise. He comes to ConsenSys Enterprise with capital markets, technology and entrepreneurial experience. Previously, he worked for UBS investment bank in equities analysis. Later, he was responsible for the creation and distribution of life settlement products to hedge funds and investment banks. After, he co-founded a revenue cycle management company where he learned about Bitcoin and eventually Ethereum.
Data center, on-premise, public-cloud, private-cloud, multi-cloud, hybrid-cloud, IoT, AI, edge, SaaS, PaaS... it's an availability, security, performance and integration nightmare even for the best of the best IT experts. Organizations realize the tremendous benefits of everything the digital transformation has to offer. Cloud adoption rates are increasing significantly, and IT budgets are morphing to follow suit. But distributing applications and infrastructure around increases risk, introdu...
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].
Financial enterprises in New York City, London, Singapore, and other world financial capitals are embracing a new generation of smart, automated FinTech that eliminates many cumbersome, slow, and expensive intermediate processes from their businesses. Accordingly, attendees at the upcoming 23rd CloudEXPO, June 24-26, 2019 at Santa Clara Convention Center in Santa Clara, CA will find fresh new content in full new FinTech & Enterprise Blockchain track.
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.
Bill Schmarzo, author of "Big Data: Understanding How Data Powers Big Business" and "Big Data MBA: Driving Business Strategies with Data Science" is responsible for guiding the technology strategy within Hitachi Vantara for IoT and Analytics. Bill brings a balanced business-technology approach that focuses on business outcomes to drive data, analytics and technology decisions that underpin an organization's digital transformation strategy. Bill has a very impressive background which includes ...
On-premise or off, you have powerful tools available to maximize the value of your infrastructure and you demand more visibility and operational control. Fortunately, data center management tools keep a vigil on memory contestation, power, thermal consumption, server health, and utilization, allowing better control no matter your cloud's shape. In this session, learn how Intel software tools enable real-time monitoring and precise management to lower operational costs and optimize infrastructure...