Visual_Novel_iOS/crush/Crush/Src/Modules/Chat/Util/IMHelperUtil.swift

115 lines
4.1 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// IMHelperUtil.swift
// Crush
//
// Created by Leon on 2025/8/31.
//
import Foundation
// MARK: - IMHelperUtil
class IMHelperUtil{
static func getAIIdByConversationId(_ conversationId: String?) -> Int?{
guard let imConversationId = conversationId else{return nil}
let stings = imConversationId.components(separatedBy: "|")
guard let last = stings.last else{return nil}
// let accountId = last
let strings2 = last.components(separatedBy:"@")
if let userIdStr = strings2.first {
if let userid = Int(userIdStr) {
return userid
}
}
return nil
}
}
// MARK: - IMAudioHelper
class IMAudioHelper{
/// emoji
func extractTextForVoice(_ text: String) -> String {
var result = text
//
let bracketPattern = #"\(.*?\)|.*?"#
if let regex = try? NSRegularExpression(pattern: bracketPattern, options: []) {
result = regex.stringByReplacingMatches(in: result, options: [], range: NSRange(location: 0, length: result.utf16.count), withTemplate: "")
}
// emoji
// 使 unicode emoji
let emojiPattern = "[\\p{Emoji}]"
if let regex = try? NSRegularExpression(pattern: emojiPattern, options: []) {
result = regex.stringByReplacingMatches(in: result, options: [], range: NSRange(location: 0, length: result.utf16.count), withTemplate: "")
}
return result.trimmingCharacters(in: .whitespacesAndNewlines)
}
///
/// - Parameters:
/// - text:
/// - speechRate: (-50 50 0)
/// - baseWordsPerMinute: 200
/// - Returns: TimeInterval
func calculateAudioDuration(
text: String,
speechRate: Int = 0,
baseWordsPerMinute: Double = 200.0
) -> TimeInterval {
guard !text.isEmpty else { return 0 }
let voiceText = extractTextForVoice(text)
guard !voiceText.isEmpty else { return 0 }
//
let chineseRegex = try! NSRegularExpression(pattern: "[\\u4e00-\\u9fff]")
let chineseCharCount = chineseRegex.numberOfMatches(in: voiceText, range: NSRange(location: 0, length: voiceText.utf16.count))
//
let textWithoutChinese = chineseRegex.stringByReplacingMatches(in: voiceText, range: NSRange(location: 0, length: voiceText.utf16.count), withTemplate: " ")
let wordRegex = try! NSRegularExpression(pattern: "\\b\\w+\\b")
let englishWordCount = wordRegex.numberOfMatches(in: textWithoutChinese, range: NSRange(location: 0, length: textWithoutChinese.utf16.count))
let totalWordCount = chineseCharCount + englishWordCount
if totalWordCount == 0 { return 0 }
//
let speedMultiplier = 1.0 + (Double(speechRate) / 100.0) // -50 0.5, 50 1.5
let adjustedWordsPerMinute = baseWordsPerMinute * speedMultiplier
//
let durationInMinutes = Double(totalWordCount) / adjustedWordsPerMinute
let durationInSeconds = durationInMinutes * 60.0
return max(0.5, durationInSeconds)
}
// ///
// /// - Parameter durationInSeconds:
// /// - Returns: "2''" "1'23''"
// func formatAudioDuration(_ durationInSeconds: Double) -> String {
// if durationInSeconds < 1 {
// return "1''"
// }
//
// let totalSeconds = Int(round(durationInSeconds))
//
// if totalSeconds < 60 {
// return "\(totalSeconds)''"
// }
//
// let minutes = totalSeconds / 60
// let seconds = totalSeconds % 60
//
// if seconds == 0 {
// return "\(minutes)'"
// }
//
// return String(format: "%d'%02d''", minutes, seconds)
// }
}