📄 240-243.html
字号:
<HTML>
<HEAD>
<META name=vsisbn content="1558515682"><META name=vstitle content="Java Digital Signal Processing"><META name=vsauthor content="Douglas A. Lyon"><META name=vsimprint content="M&T Books"><META name=vspublisher content="IDG Books Worldwide, Inc."><META name=vspubdate content="11/01/97"><META name=vscategory content="Web and Software Development: Programming, Scripting, and Markup Languages: Java"><TITLE>Java Digital Signal Processing:Digital Audio Processing</TITLE>
<!-- HEADER --><STYLE type="text/css"> <!-- A:hover { color : Red; } --></STYLE><META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">
<!--ISBN=1558515682//-->
<!--TITLE=Java Digital Signal Processing//-->
<!--AUTHOR=Douglas A. Lyon//-->
<!--PUBLISHER=IDG Books Worldwide, Inc.//-->
<!--IMPRINT=M & T Books//-->
<!--CHAPTER=5//-->
<!--PAGES=240-243//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="236-240.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="243-248.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="LEFT"><A NAME="Heading32"></A><FONT COLOR="#000077">Class Examples</FONT></H4>
<P>The <I>AudioFrame</I> class generates a series of waveforms using the <I>Oscillator</I> class and the <I>UlawCodec</I> class.</P>
<!-- CODE //-->
<PRE>
public class AudioFrame extends ClosableFrame {
private UlawCodec ulc;
private Oscillator osc =
new Oscillator(440,4000);
...
public class AudioFrame extends ClosableFrame {
private UlawCodec ulc;
private Oscillator osc =
new Oscillator(440,4000);
...
public void play() {
ulc.playSync();
}
public void sineWave() {
ulc = new UlawCodec(
osc.getSineWave());
play();
}
public void squareWave() {
ulc = new UlawCodec(
osc.getSquareWave());
play();
}
public void sawWave() {
ulc = new UlawCodec(
osc.getSawWave());
play();
}
public void triangleWave() {
ulc = new UlawCodec(
osc.getTriangleWave());
play();
}
public void am() {
osc.setModulationIndex(0.5d);
osc.setModulationFrequency(200d);
ulc = new UlawCodec(
osc.getAM());
play();
}
public void fm() {
osc.setModulationIndex(0.5d);
osc.setModulationFrequency(200d);
ulc = new UlawCodec(
osc.getFM());
play();
}
</PRE>
<!-- END CODE //-->
<H4 ALIGN="LEFT"><A NAME="Heading33"></A><FONT COLOR="#000077">Class Implementation</FONT></H4>
<P>An <I>Oscillator</I> generates repeated waveforms by constructing a single cycle of the waveform into a wavetable. The wavetable is copied repeatedly into an array of double data known internally as <I>audioData</I>.</P>
<P>The <I>Oscillator</I> class is implemented with a series of private class variables:</P>
<!-- CODE SNIP //-->
<PRE>
1. package lyon.audio;
2. import futils.utils.Computation;
3. public class Oscillator {
4. private double audioData[];
5. private double waveTable[];
</PRE>
<!-- END CODE SNIP //-->
<P>Lines 4 and 5 show that <I>audioData waveTable</I> are unallocated until the constructor is invoked. Line 6 shows the <I>sampleRate</I>. The constructor could be overloaded to take other sample rates, but 8,000 Hz is the default for Java’s current API.</P>
<!-- CODE SNIP //-->
<PRE>
6. private int sampleRate = 8000;
</PRE>
<!-- END CODE SNIP //-->
<P>Line 7 shows the <I>frequency</I>, in Hz. For a sine wave, this is the number of wavetable cycles per second that must be clocked out. <I>Lambda</I> is the number of seconds in one cycle. Line 9 shows the number of samples in a single cycle of the wavetable, if this were computed with precision. Keep in mind that the length of a wavetable is always an integer, and as a result the <I>samplesPerCycle</I> must be converted.</P>
<!-- CODE SNIP //-->
<PRE>
7. private double frequency;
8. private double lambda;
9. private double samplesPerCycle;
</PRE>
<!-- END CODE SNIP //-->
<P>Oscillator construction requires that the carrier frequency and number of cycles be known. Line 3 shows the memory allocation for <I>audioData</I>.</P>
<!-- CODE SNIP //-->
<PRE>
1. public Oscillator(double frequency_, int length) {
2. frequency = frequency_;
3. audioData = new double[length];
</PRE>
<!-- END CODE SNIP //-->
<P>Once the period of the waveform is computed, on line 5, we can compute the number of samples in a cycle of the wavetable. This is the <I>samplesPerCycle</I> variable, which is cast into an integer. Given the integral approximation, the computation of the actual frequency at which the wavetable is clocked out is as follows:</P>
<P>Where the wavetable length is:</P>
<P ALIGN="CENTER"><I><B>f<SUB>actual</SUB> = sampleRate/waveTablelength</B></I></P>
<P>(5.30)</P>
<P>Where the wavetable length is:</P>
<P ALIGN="CENTER"><I><B>waveTablelength = round(sampleRate/f)</B></I></P>
<P>(5.31)</P>
<P>To compute the error in the digital oscillator instance, we subtract the frequency that we wanted from the rate cycles that are clocked out of the wavetable.</P>
<P ALIGN="CENTER"><I><B>f<SUB>e</SUB> = f - f<SUB>actual</SUB></B></I></P>
<P>For example, for a frequency of 440 Hz, <I>waveTable.length</I> = 18, <I>sampleRate</I> = 8,000 <I>audioData</I>.<I>length</I> = 4,000, and the actual frequency = 444.444 Hz. Exact frequencies can result when the desired frequency is an exact multiple of the <I>sampleRate</I>. For example, 400 will be reproduced with precision because 8,000/400 = <I>a waveTable.length</I> of 20.</P>
<!-- CODE //-->
<PRE>
4. //the period of the wave form is
5. lambda = 1/frequency;
6. //The number of samples per period is
7. samplesPerCycle = sampleRate * lambda;
8. delta_freq = 1/samplesPerCycle;
9. waveTable =
10. new double[(int) samplesPerCycle];
11. }
</PRE>
<!-- END CODE //-->
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="236-240.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="243-248.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<hr width="90%" size="1" noshade><div align="center"><font face="Verdana,sans-serif" size="1">Copyright © <a href="/reference/idgbooks00001.html">IDG Books Worldwide, Inc.</a></font></div>
<!-- all of the reference materials (books) have the footer and subfoot reveresed --><!-- reference_subfoot = footer --><!-- reference_footer = subfoot --></BODY></HTML><!-- END FOOTER -->
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -