fix_result_v2.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package sub_timeline_fixer
  2. import (
  3. "sync"
  4. "github.com/ChineseSubFinder/ChineseSubFinder/pkg/sub_helper"
  5. "github.com/emirpasic/gods/maps/treemap"
  6. "github.com/grd/stat"
  7. )
  8. type FixResult struct {
  9. StartVADIndex int
  10. EndVADIndex int
  11. OldMean float64
  12. OldSD float64
  13. NewMean float64
  14. NewSD float64
  15. Per float64 // 占比
  16. IsOverParts bool // 是否有越接处
  17. MatchWindowInfos []MatchWindowInfo // 需要从 MatchInfo 的 IndexMatchWindowInfoMap 中按顺序提取
  18. }
  19. func (f FixResult) InRange(baseTimeDouble, timeStartDouble float64) (bool, float64) {
  20. startVad2Second := float64(f.StartVADIndex) / 100.0
  21. // 如果有越接处,因为是滑动窗体的原因,所以这个里的结束 index 并不是 FixResult 的,应该是具体的一个 MatchWindowInfo 的 EndIndex
  22. endVad2Second := float64(f.EndVADIndex) / 100.0
  23. if f.IsOverParts == true {
  24. endVad2Second = float64(f.MatchWindowInfos[0].EndVADIndex) / 100.0
  25. }
  26. if baseTimeDouble+startVad2Second <= timeStartDouble &&
  27. timeStartDouble <= baseTimeDouble+endVad2Second {
  28. // 在当前的范围内
  29. if f.IsOverParts == true {
  30. // 这里需要特殊处理,因为这个越接处,还需要二分
  31. for i := 0; i < len(f.MatchWindowInfos); i++ {
  32. b, newMean := f.chooseWhichWindow2Process(i, baseTimeDouble, timeStartDouble, startVad2Second)
  33. if b == true {
  34. return b, newMean
  35. }
  36. }
  37. return true, f.NewMean
  38. } else {
  39. // 无需特殊处理
  40. return true, f.NewMean
  41. }
  42. } else if timeStartDouble < baseTimeDouble+startVad2Second {
  43. // 小于当前的范围
  44. return true, f.NewMean
  45. } else {
  46. // 大于当前的范围
  47. return false, 0
  48. }
  49. }
  50. func (f FixResult) chooseWhichWindow2Process(index int, baseTimeDouble float64, timeStartDouble float64, startVad2Second float64) (bool, float64) {
  51. if f.MatchWindowInfos[index].OP.XLen <= 0 ||
  52. f.MatchWindowInfos[index].OP.YLen <= 0 {
  53. return false, 0
  54. }
  55. if timeStartDouble <= baseTimeDouble+startVad2Second+f.MatchWindowInfos[index].OP.XLen/100 {
  56. return true, f.MatchWindowInfos[index].OP.XMean
  57. } else {
  58. return true, f.MatchWindowInfos[index].OP.YMean
  59. }
  60. }
  61. // MatchInfo 匹配的信息
  62. type MatchInfo struct {
  63. IndexMatchWindowInfoMap map[int]MatchWindowInfo // 匹配列表的顺序列表
  64. StartDiffTimeList []float64
  65. StartDiffTimeMap *treemap.Map
  66. StartDiffTimeListEx stat.Float64Slice
  67. }
  68. type MatchWindowInfo struct {
  69. TimeDiffStartCorrelation float64 // 对白开始的时间偏移
  70. StartVADIndex int
  71. EndVADIndex int
  72. OP OverParts // 越接处信息
  73. }
  74. // WindowInfo 滑动窗体信息
  75. type WindowInfo struct {
  76. BaseAudioFloatList []float64 // 基准 VAD
  77. BaseUnit *sub_helper.SubUnit // 基准 VAD
  78. SrcUnit *sub_helper.SubUnit // 需要匹配的 VAD
  79. MatchedTimes int // 匹配上的次数
  80. SrcWindowLen int // 滑动窗体长度
  81. SrcSlideStartIndex int // 滑动起始索引
  82. SrcSlideLen int // 滑动距离
  83. OneStep int // 每次滑动的长度
  84. }
  85. // InputData 修复函数传入多线程的数据结构
  86. type InputData struct {
  87. Index int // 为了让并发处理的数据能够按顺序重新排序
  88. BaseUnit sub_helper.SubUnit // 基准 VAD
  89. BaseAudioVADList []float64 // 基准 VAD
  90. SrcUnit sub_helper.SubUnit // 需要匹配的 VAD
  91. OffsetIndex int // 滑动窗体的移动偏移索引
  92. Wg *sync.WaitGroup // 并发锁
  93. }
  94. // SubVADBlockInfo 字幕分块信息
  95. type SubVADBlockInfo struct {
  96. Index int
  97. StartIndex int
  98. EndIndex int
  99. }
  100. /*
  101. OverParts 总长度 D = XLen + YLen
  102. */
  103. type OverParts struct {
  104. XLen float64 // 分段处长度
  105. YLen float64 // 分段处长度
  106. XMean float64 // X 段的 Mean 值
  107. YMean float64 // Y 段的 Mean 值
  108. }