SYS-CON MEDIA Authors: Elizabeth White, Zakia Bouachraoui, Liz McMillan, Janakiram MSV, Carmen Gonzalez

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
AI and machine learning disruption for Enterprises started happening in the areas such as IT operations management (ITOPs) and Cloud management and SaaS apps. In 2019 CIOs will see disruptive solutions for Cloud & Devops, AI/ML driven IT Ops and Cloud Ops. Customers want AI-driven multi-cloud operations for monitoring, detection, prevention of disruptions. Disruptions cause revenue loss, unhappy users, impacts brand reputation etc.
Platform-as-a-Service (PaaS) is a technology designed to make DevOps easier and allow developers to focus on application development. The PaaS takes care of provisioning, scaling, HA, and other cloud management aspects. Apache Stratos is a PaaS codebase developed in Apache and designed to create a highly productive developer environment while also supporting powerful deployment options. Integration with the Docker platform, CoreOS Linux distribution, and Kubernetes container management system ...
Because Linkerd is a transparent proxy that runs alongside your application, there are no code changes required. It even comes with Prometheus to store the metrics for you and pre-built Grafana dashboards to show exactly what is important for your services - success rate, latency, and throughput. In this session, we'll explain what Linkerd provides for you, demo the installation of Linkerd on Kubernetes and debug a real world problem. We will also dig into what functionality you can build on ...
In a recent survey, Sumo Logic surveyed 1,500 customers who employ cloud services such as Amazon Web Services (AWS), Microsoft Azure, and Google Cloud Platform (GCP). According to the survey, a quarter of the respondents have already deployed Docker containers and nearly as many (23 percent) are employing the AWS Lambda serverless computing framework. It's clear: serverless is here to stay. The adoption does come with some needed changes, within both application development and operations. Th...
After years of investments and acquisitions, CloudBlue was created with the goal of building the world's only hyperscale digital platform with an increasingly infinite ecosystem and proven go-to-market services. The result? An unmatched platform that helps customers streamline cloud operations, save time and money, and revolutionize their businesses overnight. Today, the platform operates in more than 45 countries and powers more than 200 of the world's largest cloud marketplaces, managing mo...
The standardization of container runtimes and images has sparked the creation of an almost overwhelming number of new open source projects that build on and otherwise work with these specifications. Of course, there's Kubernetes, which orchestrates and manages collections of containers. It was one of the first and best-known examples of projects that make containers truly useful for production use. However, more recently, the container ecosystem has truly exploded. A service mesh like Istio a...
Containerized software is riding a wave of growth, according to latest RightScale survey. At Sematext we see this growth trend via our Docker monitoring adoption and via Sematext Docker Agent popularity on Docker Hub, where it crossed 1M+ pulls line. This rapid rise of containers now makes Docker the top DevOps tool among those included in RightScale survey. Overall Docker adoption surged to 35 percent, while Kubernetes adoption doubled, going from 7% in 2016 to 14% percent.
Technology has changed tremendously in the last 20 years. From onion architectures to APIs to microservices to cloud and containers, the technology artifacts shipped by teams has changed. And that's not all - roles have changed too. Functional silos have been replaced by cross-functional teams, the skill sets people need to have has been redefined and the tools and approaches for how software is developed and delivered has transformed. When we move from highly defined rigid roles and systems to ...
Even if your IT and support staff are well versed in agility and cloud technologies, it can be an uphill battle to establish a DevOps style culture - one where continuous improvement of both products and service delivery is expected and respected and all departments work together throughout a client or service engagement. As a service-oriented provider of cloud and data center technology, Green House Data sought to create more of a culture of innovation and continuous improvement, from our helpd...
Docker and Kubernetes are key elements of modern cloud native deployment automations. After building your microservices, common practice is to create docker images and create YAML files to automate the deployment with Docker and Kubernetes. Writing these YAMLs, Dockerfile descriptors are really painful and error prone.Ballerina is a new cloud-native programing language which understands the architecture around it - the compiler is environment aware of microservices directly deployable into infra...
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 produce software that is obsolete at launch. DevOps may be disruptive, but it is essential. DevOpsSUMMIT at CloudEXPO expands the DevOps community, enable a wide sharing of knowledge, and educate delegates and technology providers alike.
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 ...
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...
The platform combines the strengths of Singtel's extensive, intelligent network capabilities with Microsoft's cloud expertise to create a unique solution that sets new standards for IoT applications," said Mr Diomedes Kastanis, Head of IoT at Singtel. "Our solution provides speed, transparency and flexibility, paving the way for a more pervasive use of IoT to accelerate enterprises' digitalisation efforts. AI-powered intelligent connectivity over Microsoft Azure will be the fastest connected pat...
While more companies are now leveraging the cloud to increase their level of data protection and management, there are still many wondering “why?” The answer: the cloud actually brings substantial advancements to the data protection and management table that simply aren’t possible without it. The easiest advantage to envision? Unlimited scalability. If a data protection tool is properly designed, the capacity should automatically expand to meet any customer’s needs. The second advantage: the ...