Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
GIGNIGHT committed Nov 2, 2019
0 parents commit a76774f
Show file tree
Hide file tree
Showing 13 changed files with 456 additions and 0 deletions.
13 changes: 13 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/build/
/bundle/
/.idea
/.gradle/
/gradle/
/gradlew
/gradlew.*

/*.phpintel

/package.hub.yml
/package-lock.php.yml
/vendor/*
24 changes: 24 additions & 0 deletions README.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
## Changelog

```
--- 1.1 ---
[Add] Class Yandex
[Add] Class BaseQuery
[Add] PHPDoc hints
[Fix] Auto-detect language
[Del] Function Translate->query()
[Change] Package name
[Rewrite] All code
```

## Install package via jppm

```bash
jppm add windows@git+https://github.com/GIGNIGHT/jphp-yandex-ext
```

## Build bundle

```
jppm bundle:build
```
45 changes: 45 additions & 0 deletions package.php.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: Yandex
version: 1.1
description: Yandex Package

plugins:
- Hub
- Doc

sources:
- src

doc:
stub-dirs: ['./src/']
langs: {en: English, ru: Русский}

devDeps:
dn-bundle-plugin: '*'

config:
ignore:
- /*.iml
- /.git/**
- /package.hub.yml
- /.gitignore
- /gradle/**
- /**.phpintel
- /bundle/**
- /src-bundle/**
- /build/**
- /vendor/**
- /jars/**
- /src-jvm/**
- /.gitignore
- /**.gradle
- /gradlew
- /gradlew.**

develnext-bundle:
version: 1.1
name: Yandex
description: Yandex Package
author: GIGNIGHT
icon: "develnext/bundle/yandex/yandex.png"
group: "system"
class: "develnext\\bundle\\yandex\\YandexBundle"
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rootProject.name = 'dn-yandex-bundle'
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions src-bundle/develnext/bundle/yandex/YandexBundle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
namespace develnext\bundle\yandex;

use ide\bundle\AbstractBundle;
use ide\bundle\AbstractJarBundle;
use ide\project\Project;

/**
* Class YandexBundle
*
* @author GIGNIGHT
* @link gignight.ru / vk.com/gignight1337
*/
class YandexBundle extends AbstractJarBundle
{
public function onAdd(Project $project, AbstractBundle $owner = null)
{
parent::onAdd($project, $owner);
}

public function onRemove(Project $project, AbstractBundle $owner = null)
{
parent::onRemove($project, $owner);
}
}
3 changes: 3 additions & 0 deletions src-bundle/develnext/bundle/yandex/description.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<ul>
<li style="line-height: 19px; color: gray;"><font face="monospace" style="line-height: 19px;"><span style="line-height: 19px;"><b style="line-height: 19px;">Yandex\Translate (переводчик)</b></span></font></li><li style="line-height: 19px; color: gray;"><b style="line-height: 19px; font-family: monospace;">Yandex\SpeechKit (синтезатор речи)</b></li>
</ul>
14 changes: 14 additions & 0 deletions src/Yandex/Exception/BaseException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Yandex\Exception;

/**
* Class BaseException
*
* @author GIGNIGHT
* @link gignight.ru / vk.com/gignight1337
*/
abstract class BaseException extends \Exception
{

}
30 changes: 30 additions & 0 deletions src/Yandex/Exception/TranslateException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Yandex\Exception;

/**
* Class TranslateException
*
* @author GIGNIGHT
* @link gignight.ru / vk.com/gignight1337
*/
class TranslateException extends BaseException
{
/**
* @var array
*/
private $errorMessage =
[
401 => 'Неправильный API-ключ',
402 => 'API-ключ заблокирован',
404 => 'Превышено суточное ограничение на объем переведенного текста',
413 => 'Превышен максимально допустимый размер текста',
422 => 'Текст не может быть переведен',
501 => 'Указанное направление перевода не поддерживается'
];

public function __construct($errorCode)
{
parent::__construct($this->errorMessage[$errorCode], $errorCode);
}
}
119 changes: 119 additions & 0 deletions src/Yandex/SpeechKit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php
namespace Yandex;

use Yandex\classes\Yandex;
use script\MediaPlayerScript;

/**
* Class Yandex SpeechKit Cloud
*
* @author GIGNIGHT
* @link gignight.ru / vk.com/gignight1337
*/
class SpeechKit extends BaseQuery
{

/**
* @var string
*/
public $key = '';

/**
* Голос синтезированной речи
* jane | oksana | alyss | omazh | zahar | ermil
*
* @var string
*/
public $speaker = 'zahar';

/**
* Расширение синтезируемого файла
* mp3 | wav | opus
*
* @var string
*/
public $format = 'mp3';

/**
* Язык текста
* ru-RU | en-US | uk-UK | tr-TR
*
* @var string
*/
public $language = 'ru-RU';

/**
* Скорость воспроизведения
* Max — 3.0
* Min — 0.1
* Normal — 1.0
*
* @var double
*/
public $speed = 1.0;

/**
* Эмоциональная окраска голоса
* good — радостный
* evil — раздраженный
* neutral — нейтральный
*
* @var string
*/
public $emotion = 'good';

/**
* Частота дискретизации и битрейт синтезируемого PCM-аудио
* hi — частота дискретизации 48 кГц и битрейт 768 кб/c
* lo — частота дискретизации 8 кГц и битрейт 128 кб/c
*
* @var string
*/
public $quality = null;

/**
* Автоопределение языка
*
* @var bool
*/
public $autoDetect = true;

/**
* @var MediaPlayerScript
*/
protected $media;

public function __construct($key)
{
$this->api->setUrl(Yandex::SPEECHKIT_URL);
$this->key = $key;
$this->media = new MediaPlayerScript();
}

/**
* Воспроизвести
*
* @param string $text
*/
public function speak($text)
{
$params =
[
'key' => $this->key, 'text' => urlencode($text),
'format' => $this->format, 'quality' => $this->quality,
'lang' => $this->language, 'speaker' => $this->speaker,
'speed' => $this->speed, 'emotion' => $this->emotion
];
$speak = $this->api->query('generate', $params)->body();

/*if ($this->autoDetect)
{
$detect = new Translate()->detect($text);
$this->language = $detect . '-' . strtoupper($detect);
}*/

$file = sprintf("%s\\%s.%s", $_ENV['TEMP'], md5('yandex_speech'), $this->format);
file_put_contents($file, $speak);
$this->media->open($file); $this->media->play();
}
}
Loading

0 comments on commit a76774f

Please sign in to comment.