-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated RHVoiceSwift to with ability to play text and added sample
- Loading branch information
1 parent
15c36f7
commit 608c4c6
Showing
23 changed files
with
779 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
// | ||
// RHVoiceWrapper.cpp | ||
// | ||
// | ||
// Created by Ihor Shevchuk on 01.02.2023. | ||
// | ||
// Copyright (C) 2022 Olga Yakovleva <[email protected]> | ||
|
||
#include <stdio.h> | ||
|
||
#include <string> | ||
#include <stdexcept> | ||
#include <iostream> | ||
#include <fstream> | ||
#include <iterator> | ||
#include <algorithm> | ||
|
||
#include "core/engine.hpp" | ||
#include "core/document.hpp" | ||
#include "core/client.hpp" | ||
#include "audio.hpp" | ||
|
||
#include "FilePlaybackStream.h" | ||
|
||
namespace PlayerLib | ||
{ | ||
class FilePlaybackStream::Impl | ||
{ | ||
public: | ||
Impl(const std::string &path) | ||
{ | ||
if (!path.empty()) | ||
{ | ||
stream.set_backend(RHVoice::audio::backend_file); | ||
stream.set_device(path); | ||
} | ||
} | ||
|
||
~Impl() = default; | ||
|
||
bool play_speech(const short *samples, std::size_t count) | ||
{ | ||
try | ||
{ | ||
if (!stream.is_open()) | ||
{ | ||
stream.open(); | ||
} | ||
stream.write(samples, count); | ||
return true; | ||
} | ||
catch (...) | ||
{ | ||
stream.close(); | ||
return false; | ||
} | ||
} | ||
|
||
void finish() | ||
{ | ||
if (stream.is_open()) | ||
{ | ||
stream.drain(); | ||
} | ||
} | ||
|
||
bool set_sample_rate(int sample_rate) | ||
{ | ||
try | ||
{ | ||
if (stream.is_open() && (stream.get_sample_rate() != sample_rate)) | ||
{ | ||
stream.close(); | ||
} | ||
stream.set_sample_rate(sample_rate); | ||
return true; | ||
} | ||
catch (...) | ||
{ | ||
return false; | ||
} | ||
} | ||
bool set_buffer_size(unsigned int buffer_size) | ||
{ | ||
try | ||
{ | ||
if (stream.is_open() && (stream.get_buffer_size() != buffer_size)) | ||
{ | ||
stream.close(); | ||
} | ||
stream.set_buffer_size(buffer_size); | ||
return true; | ||
} | ||
catch (...) | ||
{ | ||
return false; | ||
} | ||
} | ||
|
||
private: | ||
RHVoice::audio::playback_stream stream; | ||
}; | ||
|
||
FilePlaybackStream::FilePlaybackStream(const char *path) : pimpl(new Impl(path)) | ||
{ | ||
} | ||
|
||
bool FilePlaybackStream::set_sample_rate(int sample_rate) | ||
{ | ||
return pimpl->set_sample_rate(sample_rate); | ||
} | ||
|
||
bool FilePlaybackStream::set_buffer_size(unsigned int buffer_size) | ||
{ | ||
return pimpl->set_buffer_size(buffer_size); | ||
} | ||
|
||
bool FilePlaybackStream::play_speech(const short *samples, std::size_t count) | ||
{ | ||
return pimpl->play_speech(samples, count); | ||
} | ||
|
||
void FilePlaybackStream::finish() | ||
{ | ||
pimpl->finish(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* Copyright (C) 2012, 2013, 2018 Olga Yakovleva <[email protected]> */ | ||
|
||
/* This program is free software: you can redistribute it and/or modify */ | ||
/* it under the terms of the GNU General Public License as published by */ | ||
/* the Free Software Foundation, either version 2 of the License, or */ | ||
/* (at your option) any later version. */ | ||
|
||
/* This program is distributed in the hope that it will be useful, */ | ||
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */ | ||
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ | ||
/* GNU General Public License for more details. */ | ||
|
||
/* You should have received a copy of the GNU General Public License */ | ||
/* along with this program. If not, see <https://www.gnu.org/licenses/>. */ | ||
|
||
|
||
#ifndef FilePlaybackStream_h | ||
#define FilePlaybackStream_h | ||
|
||
#include <memory> | ||
|
||
namespace PlayerLib | ||
{ | ||
class FilePlaybackStream | ||
{ | ||
public: | ||
FilePlaybackStream(const char *path); | ||
bool play_speech(const short *samples, std::size_t count); | ||
void finish(); | ||
bool set_sample_rate(int sample_rate); | ||
bool set_buffer_size(unsigned int buffer_size); | ||
|
||
private: | ||
class Impl; | ||
std::shared_ptr<Impl> pimpl; | ||
}; | ||
} | ||
#endif /* FilePlaybackStream_h */ |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
22 changes: 22 additions & 0 deletions
22
Sources/RHVoiceSwift/Extensions/FileManager+TemporaryFolder.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// | ||
// FileManager+TemporaryFolder.swift.swift | ||
// | ||
// | ||
// Created by Ihor Shevchuk on 27.03.2024. | ||
// | ||
|
||
import Foundation | ||
|
||
extension FileManager { | ||
func createTempFolderIfNeeded(at path: String) throws { | ||
if !fileExists(atPath: path) { | ||
try createDirectory(at: URL(fileURLWithPath: path), withIntermediateDirectories: false) | ||
} | ||
} | ||
|
||
func removeTempFolderIfNeeded(at path: String) throws { | ||
if !fileExists(atPath: path) { | ||
try removeItem(atPath: path) | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
Sources/RHVoiceSwift/Extensions/String+TemporaryFiles.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// | ||
// String+TemporaryFiles.swift | ||
// | ||
// | ||
// Created by Ihor Shevchuk on 27.03.2024. | ||
// | ||
|
||
import Foundation | ||
|
||
extension String { | ||
private static let tempFolderName = "RHVoice" | ||
static var temporaryFolderPath: String { | ||
return NSTemporaryDirectory().appending("\(tempFolderName)") | ||
} | ||
|
||
static func temporaryPath(extesnion: String) -> String { | ||
let uuid = UUID().uuidString | ||
return temporaryFolderPath.appending("/\(uuid).\(extesnion)") | ||
} | ||
} |
Oops, something went wrong.