Ací es mostren les diferències entre la revisió seleccionada i la versió actual de la pàgina.
| Ambdós costats versió prèvia Revisió prèvia Següent revisió | Revisió prèvia | ||
|
libgdx-so [2025/02/27 22:51] enric_mieza_sanchez [Exemple] |
libgdx-so [2025/03/02 23:26] (actual) enric_mieza_sanchez [So en libGDX] |
||
|---|---|---|---|
| Línia 1: | Línia 1: | ||
| ====== So en libGDX ====== | ====== So en libGDX ====== | ||
| + | |||
| + | Article de referència: | ||
| Tenim dues opcions per tractar el so en libGDX: | Tenim dues opcions per tractar el so en libGDX: | ||
| - | * La fàcil: emprant els [[https:// | + | * La fàcil: emprant els [[https:// |
| - | * La complicada: emprant [[https:// | + | * La complicada: emprant [[https:// |
| - | {{tag> #FpInfor #Dam #DamMp08 #DamMp08Uf3 # | + | Pots veure com utilitzar el sintetitzador de sons [[libGDX_JSYn|JSyn en libGDX]] en aquesta wiki. |
| + | {{tag> #FpInfor #Dam #DamMp08 #DamMp08Uf3 # | ||
| - | ===== Exemple ===== | + | \\ |
| - | En la nostra aplicació libGDX: | + | ===== Exemple amb AudioDevice ===== |
| + | |||
| + | En aquest exemple generem una sinusoide (funció sinus, | ||
| + | |||
| + | Al tocar la pantalla ('' | ||
| <code java> | <code java> | ||
| - | float freq = 440.0f; | ||
| - | float sampleRate = 44100; | ||
| - | float step = 0.1f; // fragment minim de so | ||
| - | boolean sona = true; // determina si es genera so o no | ||
| - | boolean running = true; // manté el thread de so actiu | ||
| - | AudioDevice audioDevice; | ||
| - | public static | + | float freq = 440.0f; // en Hz |
| - | int numSamples | + | float sampleRate |
| - | float[] samples | + | float step = 0.1f; // fragment minim de so |
| - | for (int i = 0; i < numSamples; i++) { | + | boolean sona = true; |
| - | float t = i / sampleRate; | + | boolean running |
| - | | + | AudioDevice audioDevice; |
| - | } | + | AudioThread audioThread; |
| - | | + | |
| - | } | + | |
| - | | + | public static float[] |
| - | int numSamples = (int) (sampleRate * durationInSeconds); | + | int numSamples = (int) (sampleRate * durationInSeconds); |
| - | float[] samples = new float[numSamples]; | + | float[] samples = new float[numSamples]; |
| - | for (int i = 0; i < numSamples; i++) { | + | for (int i = 0; i < numSamples; i++) { |
| - | samples[i] = (float) | + | float t = i / sampleRate; |
| - | } | + | |
| - | return samples; | + | |
| } | } | ||
| + | return samples; | ||
| + | } | ||
| - | Thread soundThread = new Thread(() -> { | + | class AudioThread extends |
| + | @Override | ||
| + | public void run() { | ||
| + | // creem sinusoide (un sol cop) | ||
| float[] sineWave = generaSinusoide(freq, | float[] sineWave = generaSinusoide(freq, | ||
| - | float[] silenci = generaSilenci(sampleRate, | ||
| while(running) { | while(running) { | ||
| - | if( sona ) { | + | if( sona ) |
| - | // enviem sinusoide al device | + | |
| audioDevice.writeSamples(sineWave, | audioDevice.writeSamples(sineWave, | ||
| - | } | ||
| else { | else { | ||
| - | // enviem silenci | + | // dormim una estona |
| - | | + | |
| + | Thread.sleep((long) (step*1000) ); | ||
| + | } catch(Exception e) { | ||
| + | e.printStackTrace(); | ||
| + | } | ||
| } | } | ||
| } | } | ||
| - | }); | + | } |
| + | } | ||
| - | | + | @Override |
| - | public void create() { | + | public void create() { |
| - | // ... | + | // ...elements GUI... |
| - | | + | |
| - | audioDevice = Gdx.audio.newAudioDevice((int) sampleRate, true); | + | audioDevice = Gdx.audio.newAudioDevice((int) sampleRate, true); |
| - | // posem en marxa el thread d' | + | |
| - | | + | |
| - | } | + | |
| + | audioThread.start(); | ||
| + | } | ||
| - | | + | @Override |
| - | public void render() { | + | public void render() { |
| - | // ...activitat GUI... | + | // ...activitat GUI... |
| - | + | ||
| - | // al detectar un touch apaguem o activem el so | + | |
| - | if( Gdx.input.justTouched() ) { | + | |
| - | sona = ! sona; | + | |
| - | } | + | |
| - | } | + | |
| - | | + | // al detectar un touch apaguem o engeguem |
| - | public void dispose() { | + | if( Gdx.input.justTouched() ) { |
| - | | + | sona = ! sona; |
| - | | + | |
| - | + | ||
| - | // destruim objectes GUI | + | |
| - | batch.dispose(); | + | |
| - | image.dispose(); | + | |
| - | + | ||
| - | // IMPORTANT: esperem a que acabi de sortir el thread d' | + | |
| - | try { | + | |
| - | soundThread.join(); | + | |
| - | } catch(Exception e) { | + | |
| - | | + | |
| - | } | + | |
| } | } | ||
| } | } | ||
| + | @Override | ||
| + | public void dispose() { | ||
| + | // indiquem al thread de so que acabi | ||
| + | running = false; | ||
| + | |||
| + | // destruim objectes GUI | ||
| + | batch.dispose(); | ||
| + | image.dispose(); | ||
| + | |||
| + | // IMPORTANT: esperem a que acabi de sortir el thread d' | ||
| + | try { | ||
| + | audioThread.join(); | ||
| + | } catch(Exception e) { | ||
| + | e.printStackTrace(); | ||
| + | } | ||
| + | } | ||
| </ | </ | ||