GenMessage.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. namespace App\Livewire;
  3. use Livewire\Component;
  4. use Google\Cloud\TextToSpeech\V1\AudioConfig;
  5. use Google\Cloud\TextToSpeech\V1\AudioEncoding;
  6. use Google\Cloud\TextToSpeech\V1\SynthesisInput;
  7. use Google\Cloud\TextToSpeech\V1\TextToSpeechClient;
  8. use Google\Cloud\TextToSpeech\V1\VoiceSelectionParams;
  9. use Symfony\Component\Process\Process;
  10. use Symfony\Component\Process\Exception\ProcessFailedException;
  11. use Illuminate\Support\Facades\Storage;
  12. use Livewire\Attributes\On;
  13. class GenMessage extends Component
  14. {
  15. public $name;
  16. public $text;
  17. public $mail;
  18. public $start;
  19. public $end;
  20. public function render()
  21. {
  22. return view('livewire.gen-message');
  23. }
  24. public function generate(){
  25. $id = uniqid();
  26. $start = date("d/m/y H:i",strtotime($this->start));
  27. $end = date("d/m/y H:i",strtotime($this->end));
  28. $textToSpeechClient = new TextToSpeechClient([
  29. 'credentials' => json_decode(file_get_contents('/home/alexis/tts/tts3cx-364517-c2071a1937ae.json'), true)
  30. ]);
  31. $input = new SynthesisInput();
  32. $input->setText(strtolower($this->text));
  33. $voice = new VoiceSelectionParams();
  34. $voice->setLanguageCode('fr-FR');
  35. $voice->setName('fr-FR-Neural2-B');
  36. $audioConfig = new AudioConfig();
  37. $audioConfig->setAudioEncoding(AudioEncoding::LINEAR16);
  38. $resp = $textToSpeechClient->synthesizeSpeech($input, $voice, $audioConfig);
  39. $filename = preg_replace('/\s+/', '', $this->name).$id;
  40. Storage::disk('public')->put($filename.'.mp3',$resp->getAudioContent());
  41. shell_exec("ffmpeg -i storage/".$filename.".mp3 -ar 8000 -ac 1 -y storage/".$filename.".wav && rm storage/".$filename.".mp3");
  42. // $process = new Process(['ffmpeg','-i','storage/app/'.$filename.'.mp3', '-ar', '8000','-ac','1','-y', 'storage/app/'.$filename.'.wav']);
  43. // $process->run();
  44. if(null != session('audio')){
  45. session()->pull('audio');
  46. }
  47. session([
  48. 'audio' => $filename.'.wav',
  49. 'name' => $this->name,
  50. 'mail' => $this->mail,
  51. 'dates' => "Du ".$start.' jusqu\'au '.$end,
  52. 'text' => $this->text
  53. ]);
  54. $this->dispatch('msg-gen');
  55. }
  56. }