Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

154
LINES

< > BotCompany Repo | #1009655 // Sine - plays a sine wave through JavaSound or JACK [Linux]

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Uses 6845K of libraries. Click here for Pure Java version (253L/3K/6K).

1  
!7
2  
3  
sS lib = "JavaSound"; // "JavaSound" or "JACK"
4  
5  
lib 1009653 lib 1009652 lib 1009650 // JACK
6  
lib 1009654 // JavaSound
7  
lib 1009055 lib 1009056 // JNA
8  
9  
import java.nio.FloatBuffer;
10  
import java.util.logging.Level;
11  
import java.util.logging.Logger;
12  
import org.jaudiolibs.audioservers.AudioClient;
13  
import org.jaudiolibs.audioservers.AudioConfiguration;
14  
import org.jaudiolibs.audioservers.AudioServer;
15  
import org.jaudiolibs.audioservers.AudioServerProvider;
16  
import org.jaudiolibs.audioservers.ext.ClientID;
17  
import org.jaudiolibs.audioservers.ext.Connections;
18  
19  
p { SineAudioClient.main(args); }
20  
21  
/**
22  
 * Basic example for processing audio using the AudioServer API.
23  
 * 
24  
 * A simple AudioClient that outputs a sine wave.
25  
 * 
26  
 * Run main() - if not using NetBeans, make sure to configure JVM arguments.
27  
 * -Xincgc is recommended.
28  
 * -Djna.nosys=true may be required if using the JACK AudioServer and 
29  
 * an older version of JNA is installed on your system.
30  
 * 
31  
 * @author Neil C Smith
32  
 */
33  
sclass SineAudioClient implements AudioClient {
34  
35  
    
36  
    public static void main(String[] args) throws Exception {
37  
      fixContextClassLoader();
38  
      
39  
        
40  
        /* Search for an AudioServerProvider that matches the required library name
41  
         * using the ServiceLoader mechanism. This removes the need for a direct
42  
         * dependency on any particular server implementation.
43  
         * 
44  
         * It is also possible to create particular AudioServerProvider's 
45  
         * directly. 
46  
         */
47  
48  
        AudioServerProvider provider = null;
49  
        for (AudioServerProvider p : ServiceLoader.load(AudioServerProvider.class)) {
50  
            if (lib.equals(p.getLibraryName())) {
51  
                provider = p;
52  
                break;
53  
            }
54  
        }
55  
        if (provider == null) {
56  
            throw new NullPointerException("No AudioServer found that matches : " + lib);
57  
        }
58  
        
59  
        /* Create an instance of our client - see methods in the implementation 
60  
         * below for more information.
61  
         */
62  
        AudioClient client = new SineAudioClient();
63  
        
64  
        /* Create an audio configuration.
65  
         * 
66  
         * The configuration is a hint to the AudioServer. Some servers (eg. JACK)
67  
         * will ignore the sample rate and buffersize here.
68  
         * The correct values will be passed to the client during configuration.
69  
         * 
70  
         * Various extension objects can be added to the AudioConfiguration.
71  
         * The ClientID and Connections parameters here will be used by the JACK server.
72  
         * 
73  
         */
74  
        AudioConfiguration config = new AudioConfiguration(
75  
                44100.0f, //sample rate
76  
                0, // input channels
77  
                2, // output channels
78  
                256, //buffer size
79  
                // extensions
80  
                new ClientID("Sine"),
81  
                Connections.OUTPUT);
82  
        
83  
        
84  
        /* Use the AudioServerProvider to create an AudioServer for the client. 
85  
         */
86  
        final AudioServer server = provider.createServer(config, client);
87  
        
88  
        /* Create a Thread to run our server. All servers require a Thread to run in.
89  
         */   
90  
        Thread runner = new Thread(new Runnable() {
91  
            public void run() {
92  
                // The server's run method can throw an Exception so we need to wrap it
93  
                try {
94  
                    server.run();
95  
                } catch (Exception ex) {
96  
                    Logger.getLogger(SineAudioClient.class.getName()).log(Level.SEVERE, null, ex);
97  
                }
98  
            }
99  
        });
100  
        // set the Thread priority as high as possible.
101  
        runner.setPriority(Thread.MAX_PRIORITY);
102  
        // and start processing audio - you'll have to kill the program manually!
103  
        runner.start();
104  
105  
    }
106  
107  
    // AudioClient implementation
108  
    
109  
    private final static float FREQ = 440.0f;
110  
    private float[] data;
111  
    private int idx;
112  
113  
    public void configure(AudioConfiguration context) throws Exception {
114  
        /* Check the configuration of the passed in context, and set up any
115  
         * necessary resources. Throw an Exception if the sample rate, buffer
116  
         * size, etc. cannot be handled. DO NOT assume that the context matches
117  
         * the configuration you passed in to create the server - it will
118  
         * be a best match.
119  
         */
120  
        if (context.getOutputChannelCount() != 2) {
121  
            throw new IllegalArgumentException("SineAudioClient can only work with stereo output");
122  
        }
123  
        
124  
        int size = (int) (context.getSampleRate() / FREQ);
125  
        data = new float[size];
126  
        for (int i = 0; i < size; i++) {
127  
            data[i] = (float) (0.2 * Math.sin(((double) i / (double) size) * Math.PI * 2.0));
128  
        }
129  
    }
130  
131  
    public boolean process(long time, List<FloatBuffer> inputs, List<FloatBuffer> outputs, int nframes) {
132  
        // get left and right channels from array list
133  
        FloatBuffer left = outputs.get(0);
134  
        FloatBuffer right = outputs.get(1);
135  
        
136  
        // always use nframes as the number of samples to process
137  
        for (int i = 0; i < nframes; i++) {
138  
            left.put(data[idx]);
139  
            right.put(data[idx]);
140  
            idx++;
141  
            if (idx == data.length) {
142  
                idx = 0;
143  
            }
144  
145  
        }
146  
        return true;
147  
    }
148  
149  
    public void shutdown() {
150  
        //dispose resources.
151  
        data = null;
152  
    }
153  
154  
}

Author comment

Began life as a copy of #1009651

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1009655
Snippet name: Sine - plays a sine wave through JavaSound or JACK [Linux]
Eternal ID of this version: #1009655/5
Text MD5: 16249a4f5b8f9c2f798f98fdd3fc709e
Transpilation MD5: eaf36527a91b8f71b0326a8872ab9a85
Author: stefan
Category: javax / audio
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-09-03 03:01:01
Source code size: 5584 bytes / 154 lines
Pitched / IR pitched: No / No
Views / Downloads: 420 / 528
Version history: 4 change(s)
Referenced in: [show references]