|
| 1 | +import com.translated.lara.Credentials; |
| 2 | +import com.translated.lara.errors.LaraException; |
| 3 | +import com.translated.lara.errors.S3Exception; |
| 4 | +import com.translated.lara.translator.*; |
| 5 | + |
| 6 | +import java.io.*; |
| 7 | +import java.nio.file.Files; |
| 8 | +import java.nio.file.Paths; |
| 9 | + |
| 10 | +/** |
| 11 | + * Complete audio translation examples for the Lara Java SDK |
| 12 | + * |
| 13 | + * This example demonstrates: |
| 14 | + * - Basic audio translation |
| 15 | + * - Advanced options with memories and glossaries |
| 16 | + * - Step-by-step audio translation with status monitoring |
| 17 | + */ |
| 18 | +public class AudioTranslation { |
| 19 | + |
| 20 | + public static void main(String[] args) { |
| 21 | + // All examples can use environment variables for credentials: |
| 22 | + // export LARA_ACCESS_KEY_ID="your-access-key-id" |
| 23 | + // export LARA_ACCESS_KEY_SECRET="your-access-key-secret" |
| 24 | + |
| 25 | + String accessKeyId = System.getenv("LARA_ACCESS_KEY_ID"); |
| 26 | + String accessKeySecret = System.getenv("LARA_ACCESS_KEY_SECRET"); |
| 27 | + |
| 28 | + Credentials credentials = new Credentials(accessKeyId, accessKeySecret); |
| 29 | + Translator lara = new Translator(credentials); |
| 30 | + |
| 31 | + // Replace with your actual audio file path |
| 32 | + String sampleFilePath = "sample_audio.mp3"; // Create this file with your content |
| 33 | + File sampleFile = new File(sampleFilePath); |
| 34 | + |
| 35 | + if (!sampleFile.exists()) { |
| 36 | + System.out.println("Please create a sample audio file at: " + sampleFilePath); |
| 37 | + System.out.println("Supported formats: .wav, .mp3, .opus, .ogg, .webm\n"); |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + String sourceLang = "en-US"; |
| 42 | + String targetLang = "de-DE"; |
| 43 | + |
| 44 | + // Example 1: Basic audio translation |
| 45 | + System.out.println("=== Basic Audio Translation ==="); |
| 46 | + System.out.println("Translating audio: " + sampleFile.getName() + " from " + sourceLang + " to " + targetLang); |
| 47 | + |
| 48 | + try { |
| 49 | + InputStream audioStream = lara.audio.translate(sampleFile, sourceLang, targetLang); |
| 50 | + |
| 51 | + String outputPath = "sample_audio_translated.mp3"; |
| 52 | + Files.copy(audioStream, Paths.get(outputPath)); |
| 53 | + audioStream.close(); |
| 54 | + |
| 55 | + System.out.println("✅ Audio translation completed"); |
| 56 | + System.out.println("📄 Translated file saved to: " + outputPath + "\n"); |
| 57 | + } catch (LaraException | S3Exception | InterruptedException | IOException e) { |
| 58 | + System.out.println("Error translating audio: " + e.getMessage() + "\n"); |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + // Example 2: Audio translation with advanced options |
| 63 | + System.out.println("=== Audio Translation with Advanced Options ==="); |
| 64 | + try { |
| 65 | + AudioUploadOptions options = new AudioUploadOptions(); |
| 66 | + options.setAdaptTo("mem_1A2b3C4d5E6f7G8h9I0jKl"); // Replace with actual memory IDs |
| 67 | + options.setGlossaries("gls_1A2b3C4d5E6f7G8h9I0jKl"); // Replace with actual glossary IDs |
| 68 | + options.setStyle(TranslationStyle.FLUID); |
| 69 | + options.setNoTrace(false); |
| 70 | + |
| 71 | + InputStream audioStream = lara.audio.translate(sampleFile, sourceLang, targetLang, options); |
| 72 | + |
| 73 | + String outputPath = "advanced_audio_translated.mp3"; |
| 74 | + Files.copy(audioStream, Paths.get(outputPath)); |
| 75 | + audioStream.close(); |
| 76 | + |
| 77 | + System.out.println("✅ Advanced audio translation completed"); |
| 78 | + System.out.println("📄 Translated file saved to: " + outputPath + "\n"); |
| 79 | + } catch (LaraException | S3Exception | InterruptedException | IOException e) { |
| 80 | + System.out.println("Error in advanced translation: " + e.getMessage()); |
| 81 | + return; |
| 82 | + } |
| 83 | + |
| 84 | + // Example 3: Step-by-step audio translation |
| 85 | + System.out.println("=== Step-by-Step Audio Translation ==="); |
| 86 | + try { |
| 87 | + AudioUploadOptions uploadOptions = new AudioUploadOptions(); |
| 88 | + uploadOptions.setAdaptTo("mem_1A2b3C4d5E6f7G8h9I0jKl"); // Replace with actual memory ID |
| 89 | + uploadOptions.setGlossaries("gls_1A2b3C4d5E6f7G8h9I0jKl"); // Replace with actual glossary ID |
| 90 | + uploadOptions.setStyle(TranslationStyle.CREATIVE); |
| 91 | + |
| 92 | + System.out.println("Step 1: Uploading audio..."); |
| 93 | + Audio audio = lara.audio.upload(sampleFile, sourceLang, "es-ES", uploadOptions); |
| 94 | + System.out.println("Audio uploaded with ID: " + audio.getId()); |
| 95 | + System.out.println("Initial status: " + audio.getStatus()); |
| 96 | + |
| 97 | + System.out.println("\nStep 2: Checking status..."); |
| 98 | + Audio status = lara.audio.status(audio.getId()); |
| 99 | + System.out.println("Current status: " + status.getStatus()); |
| 100 | + |
| 101 | + System.out.println("\nStep 3: Waiting for translation to complete..."); |
| 102 | + while (status.getStatus() != Audio.Status.TRANSLATED && status.getStatus() != Audio.Status.ERROR) { |
| 103 | + Thread.sleep(2000); |
| 104 | + status = lara.audio.status(audio.getId()); |
| 105 | + System.out.println("Current status: " + status.getStatus()); |
| 106 | + } |
| 107 | + |
| 108 | + if (status.getStatus() == Audio.Status.ERROR) { |
| 109 | + System.out.println("Translation failed: " + status.getErrorReason()); |
| 110 | + return; |
| 111 | + } |
| 112 | + |
| 113 | + InputStream audioStream = lara.audio.download(audio.getId()); |
| 114 | + |
| 115 | + String outputPath = "step_audio_translated.mp3"; |
| 116 | + Files.copy(audioStream, Paths.get(outputPath)); |
| 117 | + audioStream.close(); |
| 118 | + |
| 119 | + System.out.println("✅ Step-by-step translation completed"); |
| 120 | + System.out.println("📄 Translated file saved to: " + outputPath); |
| 121 | + |
| 122 | + } catch (LaraException | S3Exception | IOException | InterruptedException e) { |
| 123 | + System.out.println("Error in step-by-step process: " + e.getMessage() + "\n"); |
| 124 | + } |
| 125 | + } |
| 126 | +} |
0 commit comments