Skip to content

Commit

Permalink
Using offline-tts-cahce-mechanism-config in both SherpaOnnxTts and Sh…
Browse files Browse the repository at this point in the history
…erpaOnnxEngine

Removing cache_dir from OfflineTts
Removing function implementations from OfflineTts
Passing config pointer  to OfflineTts
Added cache_mechanism_ to OfflineTts (as public, might not be so neat)
Changing all cache units in kotlin to Bytes from MB to reduce confusion. Showing is still in MBs
  • Loading branch information
Your Name committed Jan 25, 2025
1 parent f6e11ed commit 521e900
Show file tree
Hide file tree
Showing 10 changed files with 167 additions and 115 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,12 @@ class MainActivity : AppCompatActivity() {
ruleFars = ruleFars ?: "",
)!!

tts = OfflineTts(assetManager = assets, config = config)
val cacheConfig = getOfflineTtsCacheMechanismConfig(
dataDir = dataDir ?: "",
cacheSize = 20*1024*1024, // Fixed to 20 MBs
)!!

tts = OfflineTts(assetManager = assets, config = config, cacheConfig = cacheConfig)
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class MainActivity : ComponentActivity() {

val preferenceHelper = PreferenceHelper(this)

TtsEngine.cacheSize = preferenceHelper.getCacheSizeInMB()
TtsEngine.cacheSize = preferenceHelper.getTtsMechanismCacheSize()

setContent {
SherpaOnnxTtsEngineTheme {
Expand All @@ -95,12 +95,12 @@ class MainActivity : ComponentActivity() {
Box(modifier = Modifier.padding(it)) {
Column(modifier = Modifier.padding(16.dp)) {
// Track used cache size in a mutable state
var usedCacheSizeMB by remember { mutableStateOf(0) }
var usedCacheSize by remember { mutableStateOf(0) }

// LaunchedEffect to periodically update the used cache size
LaunchedEffect(Unit) {
while (true) {
usedCacheSizeMB = TtsEngine.tts?.getTotalUsedCacheSizeInMB() ?: 0
usedCacheSize = TtsEngine.tts?.getTotalUsedCacheSize() ?: 0
delay(5000) // Update every 5 seconds
}
}
Expand All @@ -113,23 +113,23 @@ class MainActivity : ComponentActivity() {
TtsEngine.speed = it
preferenceHelper.setSpeed(it)
TtsEngine.tts?.clearCache() // Call the clearCache method
usedCacheSizeMB = 0 // Reset used cache size
usedCacheSize = 0 // Reset used cache size
},
valueRange = 0.2F..3.0F,
modifier = Modifier.fillMaxWidth()
)

Text("Cache Size: ${TtsEngine.cacheSize}MB (${usedCacheSizeMB}MB used)")
Text("Cache Size: ${TtsEngine.cacheSize / (1024 * 1024)}MB (${usedCacheSize / (1024 * 1024)}MB used)")
Slider(
value = TtsEngine.cacheSizeState.value.toFloat(),
onValueChange = { newValue ->
// Round the value to the nearest multiple of 10
val roundedValue = (newValue / 5).roundToInt() * 5
// Round the value to the nearest multiple of 5MB
val roundedValue = (newValue / (5 * 1024 * 1024)).roundToInt() * (5 * 1024 * 1024)
TtsEngine.cacheSize = roundedValue
preferenceHelper.setCacheSizeInMB(roundedValue)
TtsEngine.tts?.setCacheSizeInMB(roundedValue)
preferenceHelper.setCacheSize(roundedValue)
TtsEngine.tts?.setCacheSize(roundedValue)
},
valueRange = 0f..100f,
valueRange = 0f..209715200f, // 200MB
modifier = Modifier.fillMaxWidth()
)
}
Expand Down Expand Up @@ -308,8 +308,8 @@ class MainActivity : ComponentActivity() {
override fun onResume() {
super.onResume()
// Update used cache size when the app is resumed
val usedCacheSizeMB = TtsEngine.tts?.getTotalUsedCacheSizeInMB() ?: 0
Log.i(TAG, "App resumed. Used cache size: ${usedCacheSizeMB}MB")
val usedCacheSize = (TtsEngine.tts?.getTotalUsedCacheSize() ?: 0)
Log.i(TAG, "App resumed. Used cache size: ${usedCacheSize}B")
}

override fun onDestroy() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ class PreferenceHelper(context: Context) {
return sharedPreferences.getInt(SID_KEY, 0)
}

fun setCacheSizeInMB(value: Int) {
fun setCacheSize(value: Int) {
val editor = sharedPreferences.edit()
editor.putInt(CACHE_SIZE_KEY, value)
editor.apply()
}

fun getCacheSizeInMB(): Int {
return sharedPreferences.getInt(CACHE_SIZE_KEY, 20) // Default cache size is 20MB
fun getTtsMechanismCacheSize(): Int {
return sharedPreferences.getInt(CACHE_SIZE_KEY, 20*(1024*1024)) // Default cache size is 20MB
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import androidx.compose.runtime.mutableFloatStateOf
import androidx.compose.runtime.mutableIntStateOf
import com.k2fsa.sherpa.onnx.OfflineTts
import com.k2fsa.sherpa.onnx.getOfflineTtsConfig
import com.k2fsa.sherpa.onnx.getOfflineTtsCacheMechanismConfig
import java.io.File
import java.io.FileOutputStream
import java.io.IOException
Expand Down Expand Up @@ -195,13 +196,17 @@ object TtsEngine {
ruleFars = ruleFars ?: ""
)

cacheSize = PreferenceHelper(context).getTtsMechanismCacheSize()
val cacheConfig = getOfflineTtsCacheMechanismConfig(
dataDir = dataDir ?: "",
cacheSize = cacheSize,
)

speed = PreferenceHelper(context).getSpeed()
speakerId = PreferenceHelper(context).getSid()
cacheSize = PreferenceHelper(context).getCacheSizeInMB()

tts = OfflineTts(assetManager = assets, config = config)

tts?.setCacheSizeInMB(cacheSize)
tts = OfflineTts(assetManager = assets, config = config, cacheConfig = cacheConfig)
}


Expand Down
29 changes: 14 additions & 15 deletions sherpa-onnx/csrc/offline-tts-cache-mechanism.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,14 @@
#include "sherpa-onnx/csrc/wave-reader.h"
#include "sherpa-onnx/csrc/wave-writer.h"

// Platform-specific time functions
#if defined(_WIN32)
#include <windows.h>
#else
#include <sys/time.h>
#include <unistd.h>
#endif

namespace sherpa_onnx {

OfflineTtsCacheMechanism::OfflineTtsCacheMechanism(const std::string &cache_dir,
int32_t cache_size)
: cache_dir_(cache_dir),
cache_size_bytes_(cache_size),
used_cache_size_bytes_(0) {

OfflineTtsCacheMechanism::OfflineTtsCacheMechanism(
const OfflineTtsCacheMechanismConfig &config)
: cache_dir_(config.cache_dir),
cache_size_bytes_(config.cache_size),
used_cache_size_bytes_(0)
{
// Create the cache directory if it doesn't exist
if (!std::filesystem::exists(cache_dir_)) {
bool dir_created = std::filesystem::create_directory(cache_dir_);
Expand All @@ -44,6 +36,9 @@ OfflineTtsCacheMechanism::OfflineTtsCacheMechanism(const std::string &cache_dir,
}
}

if(cache_size_bytes_ == -1)
cache_size_bytes_ = INT32_MAX; // Unlimited cache size

// Load the repeat counts
LoadRepeatCounts();

Expand Down Expand Up @@ -147,7 +142,11 @@ void OfflineTtsCacheMechanism::SetCacheSize(int32_t cache_size) {

cache_size_bytes_ = cache_size;

EnsureCacheLimit();
if(cache_size == 0) {
ClearCache();
} else {
EnsureCacheLimit();
}
}

void OfflineTtsCacheMechanism::ClearCache() {
Expand Down
3 changes: 2 additions & 1 deletion sherpa-onnx/csrc/offline-tts-cache-mechanism.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ namespace sherpa_onnx {

class OfflineTtsCacheMechanism {
public:
OfflineTtsCacheMechanism(const std::string &cache_dir, int32_t cache_size);

explicit OfflineTtsCacheMechanism(const OfflineTtsCacheMechanismConfig &config);
~OfflineTtsCacheMechanism();

// Add a new wav file to the cache
Expand Down
60 changes: 21 additions & 39 deletions sherpa-onnx/csrc/offline-tts.cc
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,24 @@ std::string OfflineTtsConfig::ToString() const {
}

OfflineTts::OfflineTts(const OfflineTtsConfig &config)
: config_(config),
impl_(OfflineTtsImpl::Create(config)),
cache_mechanism_(nullptr) {}
: impl_(OfflineTtsImpl::Create(config)) {}

OfflineTts::OfflineTts(const OfflineTtsConfig &config,
const OfflineTtsCacheMechanismConfig &cache_config)
: impl_(OfflineTtsImpl::Create(config)) {
cache_mechanism_ = std::make_unique<OfflineTtsCacheMechanism>(cache_config);
}

template <typename Manager>
OfflineTts::OfflineTts(Manager *mgr, const OfflineTtsConfig &config)
: config_(config),
impl_(OfflineTtsImpl::Create(mgr, config)),
cache_mechanism_(nullptr) {}
: impl_(OfflineTtsImpl::Create(mgr, config)) {}

template <typename Manager>
OfflineTts::OfflineTts(Manager *mgr, const OfflineTtsConfig &config,
const OfflineTtsCacheMechanismConfig &cache_config)
: impl_(OfflineTtsImpl::Create(mgr, config)) {
cache_mechanism_ = std::make_unique<OfflineTtsCacheMechanism>(cache_config);
}

OfflineTts::~OfflineTts() = default;

Expand Down Expand Up @@ -145,49 +154,22 @@ GeneratedAudio OfflineTts::Generate(

int32_t OfflineTts::SampleRate() const { return impl_->SampleRate(); }

int32_t OfflineTts::CacheSize() const {
return cache_mechanism_ ? cache_mechanism_->GetCacheSize() : 0;
}

void OfflineTts::SetCacheSize(const int32_t cache_size) {
if (cache_size > 0) {
if (!cache_mechanism_) {
// Initialize the cache mechanism if it hasn't been initialized yet
cache_mechanism_ = std::make_unique<OfflineTtsCacheMechanism>(
config_.cache_dir, cache_size);
} else {
// Update the cache size if the cache mechanism is already initialized
cache_mechanism_->SetCacheSize(cache_size);
}
} else if (cache_mechanism_) {
// If cache size is set to 0 or negative, destroy the cache mechanism
cache_mechanism_.reset();
}
}

void OfflineTts::ClearCache() {
if (cache_mechanism_) {
cache_mechanism_->ClearCache();
}
}

int32_t OfflineTts::GetTotalUsedCacheSize() {
if (cache_mechanism_) {
return cache_mechanism_->GetTotalUsedCacheSize();
}
return -1;
}

int32_t OfflineTts::NumSpeakers() const { return impl_->NumSpeakers(); }

#if __ANDROID_API__ >= 9
template OfflineTts::OfflineTts(AAssetManager *mgr,
const OfflineTtsConfig &config);
template OfflineTts::OfflineTts(AAssetManager *mgr,
const OfflineTtsConfig &config,
const OfflineTtsCacheMechanismConfig &cache_config);
#endif

#if __OHOS__
template OfflineTts::OfflineTts(NativeResourceManager *mgr,
const OfflineTtsConfig &config);
template OfflineTts::OfflineTts(NativeResourceManager *mgr,
const OfflineTtsConfig &config,
const OfflineTtsCacheMechanismConfig &cache_config);
#endif

} // namespace sherpa_onnx
26 changes: 9 additions & 17 deletions sherpa-onnx/csrc/offline-tts.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace sherpa_onnx {

struct OfflineTtsConfig {
OfflineTtsModelConfig model;

// If not empty, it contains a list of rule FST filenames.
// Filenames are separated by a comma.
// Example value: rule1.fst,rule2,fst,rule3.fst
Expand All @@ -33,9 +34,6 @@ struct OfflineTtsConfig {
// If you set it to -1, then we process all sentences in a single batch.
int32_t max_num_sentences = 1;

// Path to cache_directory
std::string cache_dir;

OfflineTtsConfig() = default;
OfflineTtsConfig(const OfflineTtsModelConfig &model,
const std::string &rule_fsts, const std::string &rule_fars,
Expand Down Expand Up @@ -67,10 +65,16 @@ class OfflineTts {
public:
~OfflineTts();
explicit OfflineTts(const OfflineTtsConfig &config);
explicit OfflineTts(const OfflineTtsConfig &config,
const OfflineTtsCacheMechanismConfig &cache_config);

template <typename Manager>
OfflineTts(Manager *mgr, const OfflineTtsConfig &config);

template <typename Manager>
OfflineTts(Manager *mgr, const OfflineTtsConfig &config,
const OfflineTtsCacheMechanismConfig &cache_config);

// @param text A string containing words separated by spaces
// @param sid Speaker ID. Used only for multi-speaker models, e.g., models
// trained using the VCTK dataset. It is not used for
Expand All @@ -91,26 +95,14 @@ class OfflineTts {
// Return the sample rate of the generated audio
int32_t SampleRate() const;

// Return the maximum number of cached audio files size
int32_t CacheSize() const;

// Set the maximum number of cached audio files size
void SetCacheSize(const int32_t cache_size);

// Remove all cache data
void ClearCache();

// To get total used cache size(for wav files) in bytes
int32_t GetTotalUsedCacheSize();

// Number of supported speakers.
// If it supports only a single speaker, then it return 0 or 1.
int32_t NumSpeakers() const;

std::unique_ptr<OfflineTtsCacheMechanism> cache_mechanism_; // not owned here

private:
OfflineTtsConfig config_;
std::unique_ptr<OfflineTtsImpl> impl_;
std::unique_ptr<OfflineTtsCacheMechanism> cache_mechanism_;
};

} // namespace sherpa_onnx
Expand Down
Loading

0 comments on commit 521e900

Please sign in to comment.