nimroddolev / chime_tts

A custom Home Assistant integration to play combined audio files before and/or after text-to-speech (TTS) messages
https://nimroddolev.github.io/chime_tts/
MIT License
184 stars 13 forks source link

The cache parameter is a bit confusing #32

Closed leafney closed 11 months ago

leafney commented 1 year ago

Checklist

Is your feature request related to a problem? Please describe.

This is a fantastic project. Thanks.

In the first scenario, when calling the service through HomeAssistant:

If cache=True is set, when playing a TTS audio, it will first play the previously played audio or only start playing a part of it before playing the current audio content.

If cache=False is set, the first time a TTS audio is played after that, it will also play the previously played audio once again before playing the latest TTS content. After that, the TTS audio will play normally without playing the previous audio content.

In the second scenario, when calling it in Node-Red:

The data passed is as follows:

msg.payload={
   "chime_path":"custom_components/chime_tts/mp3s/ding_dong.mp3",
   "end_chime_path":"custom_components/chime_tts/mp3s/tada.mp3",
   "delay":450,
   "final_delay":20,
   "message":"昨日之日不可留",
   "tts_platform":"edge_tts",
   "cache":"True",
   "announce":"True"
}

return msg;

The output log is as follows:

2023-10-14 15:06:32.189 DEBUG (MainThread) [custom_components.chime_tts] Service call #0 was added to the queue.
2023-10-14 15:06:32.190 DEBUG (MainThread) [custom_components.chime_tts] Executing queued job #0
2023-10-14 15:06:32.190 DEBUG (MainThread) [custom_components.chime_tts] ----- Chime TTS Say Called -----
2023-10-14 15:06:32.191 DEBUG (MainThread) [custom_components.chime_tts] Chime path supplied = custom_components/chime_tts/mp3s/ding_dong.mp3
2023-10-14 15:06:32.191 DEBUG (MainThread) [custom_components.chime_tts] Chime path supplied = custom_components/chime_tts/mp3s/tada.mp3
2023-10-14 15:06:32.192 DEBUG (MainThread) [custom_components.chime_tts]  * entity_ids = ['media_player.mopidy']
2023-10-14 15:06:32.193 DEBUG (MainThread) [custom_components.chime_tts]  * hass = <HomeAssistant RUNNING>
2023-10-14 15:06:32.193 DEBUG (MainThread) [custom_components.chime_tts]  * chime_path = custom_components/chime_tts/mp3s/ding_dong.mp3
2023-10-14 15:06:32.195 DEBUG (MainThread) [custom_components.chime_tts]  * end_chime_path = custom_components/chime_tts/mp3s/tada.mp3
2023-10-14 15:06:32.196 DEBUG (MainThread) [custom_components.chime_tts]  * delay = 450.0
2023-10-14 15:06:32.196 DEBUG (MainThread) [custom_components.chime_tts]  * message = 昨日之日不可留
2023-10-14 15:06:32.196 DEBUG (MainThread) [custom_components.chime_tts]  * tts_platform = edge_tts
2023-10-14 15:06:32.196 DEBUG (MainThread) [custom_components.chime_tts]  * tts_playback_speed = 100.0
2023-10-14 15:06:32.197 DEBUG (MainThread) [custom_components.chime_tts]  * volume_level = -1.0
2023-10-14 15:06:32.197 DEBUG (MainThread) [custom_components.chime_tts]  * join_players = False
2023-10-14 15:06:32.197 DEBUG (MainThread) [custom_components.chime_tts]  * unjoin_players = False
2023-10-14 15:06:32.197 DEBUG (MainThread) [custom_components.chime_tts]  * cache = True
2023-10-14 15:06:32.197 DEBUG (MainThread) [custom_components.chime_tts]  * announce = True
2023-10-14 15:06:32.197 DEBUG (MainThread) [custom_components.chime_tts] ------
2023-10-14 15:06:32.198 INFO (MainThread) [custom_components.chime_tts] Media player entity "media_player.mopidy" is turned off. Turning on...
2023-10-14 15:06:32.307 DEBUG (MainThread) [custom_components.chime_tts] async_get_playback_audio_path
2023-10-14 15:06:32.307 DEBUG (MainThread) [custom_components.chime_tts] get_audio_from_path("custom_components/chime_tts/mp3s/ding_dong.mp3", 0, audio)
2023-10-14 15:06:32.309 DEBUG (MainThread) [custom_components.chime_tts]  - Retrieving audio from path: "custom_components/chime_tts/mp3s/ding_dong.mp3"...
2023-10-14 15:06:32.819 DEBUG (MainThread) [custom_components.chime_tts]    - ...audio with duration 1.533s retrieved successfully
2023-10-14 15:06:32.819 DEBUG (MainThread) [custom_components.chime_tts] async_request_tts_audio(hass, tts_platform="edge_tts", message="昨日之日不可留", cache="True", language="None", tld="None", gender="None", tts_playback_speed="100.0"))
2023-10-14 15:06:32.819 DEBUG (MainThread) [custom_components.chime_tts]  - Generating TTS audio...
2023-10-14 15:06:33.339 DEBUG (MainThread) [custom_components.chime_tts]  - ...TTS audio completed in 519.817 ms
2023-10-14 15:06:33.371 DEBUG (MainThread) [custom_components.chime_tts] get_audio_from_path("custom_components/chime_tts/mp3s/tada.mp3", 450.0, audio)
2023-10-14 15:06:33.371 DEBUG (MainThread) [custom_components.chime_tts]  - Retrieving audio from path: "custom_components/chime_tts/mp3s/tada.mp3"...
2023-10-14 15:06:33.922 DEBUG (MainThread) [custom_components.chime_tts]    - ...audio with duration 2.323s retrieved successfully
2023-10-14 15:06:33.960 DEBUG (MainThread) [custom_components.chime_tts]  - Final audio created:
2023-10-14 15:06:33.960 DEBUG (MainThread) [custom_components.chime_tts]    - Duration = 6.916s
2023-10-14 15:06:33.960 DEBUG (MainThread) [custom_components.chime_tts] Creating final audio file
2023-10-14 15:06:33.961 DEBUG (MainThread) [custom_components.chime_tts]    - Temp folder exists: /media/sounds/temp/chime_tts/
2023-10-14 15:06:33.961 DEBUG (MainThread) [custom_components.chime_tts]  - Creating temporary mp3 file...
2023-10-14 15:06:33.961 DEBUG (MainThread) [custom_components.chime_tts]    - temp_obj = <tempfile._TemporaryFileWrapper object at 0x5de1bd10>
2023-10-14 15:06:33.963 DEBUG (MainThread) [custom_components.chime_tts]    - Filepath = '/media/sounds/temp/chime_tts/l11vjl9d.mp3'
2023-10-14 15:06:35.281 DEBUG (MainThread) [custom_components.chime_tts]    - File saved successfully
2023-10-14 15:06:35.282 DEBUG (MainThread) [custom_components.chime_tts]  - audio_dict = {'audio_path': '/media/sounds/temp/chime_tts/l11vjl9d.mp3', 'audio_duration': 6.916}
2023-10-14 15:06:35.282 DEBUG (MainThread) [custom_components.chime_tts] Calling media_player.play_media service with data:
2023-10-14 15:06:35.282 DEBUG (MainThread) [custom_components.chime_tts]  - media_content_type: music
2023-10-14 15:06:35.282 DEBUG (MainThread) [custom_components.chime_tts]  - media_content_id: media-source://media_source/local/sounds/temp/chime_tts/l11vjl9d.mp3
2023-10-14 15:06:35.283 DEBUG (MainThread) [custom_components.chime_tts]  - entity_id: ['media_player.mopidy']
2023-10-14 15:06:35.448 DEBUG (MainThread) [custom_components.chime_tts] ...media_player.play_media completed.
2023-10-14 15:06:35.448 DEBUG (MainThread) [custom_components.chime_tts] Waiting 6.916s for audio playback to complete...
2023-10-14 15:06:42.367 DEBUG (MainThread) [custom_components.chime_tts] Waiting 0.02s for final_delay to complete...
2023-10-14 15:06:42.391 DEBUG (MainThread) [custom_components.chime_tts] ----- Chime TTS Say Completed in 10200.245 ms -----
2023-10-14 15:06:42.391 DEBUG (MainThread) [custom_components.chime_tts] Removing current queued service call.
2023-10-14 15:06:42.391 DEBUG (MainThread) [custom_components.chime_tts] Queue emptied. Reinitializing values.

The problem is: although cache=True is set, it doesn't seem to work. The generated file /media/sounds/temp/chime_tts/l11vjl9d.mp3 cannot be found in the file system, but the TTS audio output is normal.

Describe the solution you'd like

no.

Describe alternatives you've considered

I am also puzzled by the situation where it did not take effect in Node-Red. I will conduct further tests to find the reason.

Additional context

chime_tts version: v0.7.4-beta1 home-assistant version: 2023.10.3 home-assistant platform: docker node-red version: v3.0.2 node-red installed node-red-contrib-home-assistant-websocket version: v0.57.4

leafney commented 1 year ago

Upon further testing, I found that there is no issue with the situation in Node-Red. I apologize for the confusion, as it was my mistake.

The reason for loading the default configuration values was due to a type error in the parameters, as I directly used the provided sample code. The problem was resolved by changing "cache":"True", to "cache":true,.

msg.payload={
   "chime_path":"custom_components/chime_tts/mp3s/ding_dong.mp3",
   "delay":450,
   "final_delay":50,
   "message":"Hello World",
   "tts_platform":"edge_tts",
   "cache":false,
   "announce":true
}

return msg;
nimroddolev commented 1 year ago

Hi @leafney, I am glad you got it working!

leafney commented 1 year ago

If cache=True is set, when playing a TTS audio, it will first play the previously played audio or only start playing a part of it before playing the current audio content.

If cache=False is set, the first time a TTS audio is played after that, it will also play the previously played audio once again before playing the latest TTS content. After that, the TTS audio will play normally without playing the previous audio content.

Thanks for your quick response. But this problem still exists.

nimroddolev commented 1 year ago

I apologise. Can you please help me better understand the nature of the problem?

nimroddolev commented 1 year ago

Does this happen with other TTS platforms, or only Microsoft Edge TTS?

leafney commented 1 year ago

Does this happen with other TTS platforms, or only Microsoft Edge TTS?

I'll try other platforms.

leafney commented 1 year ago

Does this happen with other TTS platforms, or only Microsoft Edge TTS?

I'll try other platforms.

I have tested other TTS platforms such as picotts, when to set cache = true, the previous audio will still be broadcast first. This issue should not be related to the TTS platform.

nimroddolev commented 12 months ago

I am still not sure I understand the problem: Based on your messages I understand that you are hearing a partial duplication of the TTS audio...which is very odd given that the integration connects MP3 files together and does not change their content (unless you change thetts_playback_speed value from 100%). Please correct me if I have misunderstood.

Are you able to download the generated temporary MP3 files onto another computer and play them? I wonder if the issue is with the MP3 files themselves, or if perhaps there is an issue with the hardware running your Home Assistant instance, or the speaker. Could you please share with me what hardware you are running your Home Assistant instance on? It might also help to have the generated temporary MP3 files as well.

leafney commented 12 months ago

I am still not sure I understand the problem: Based on your messages I understand that you are hearing a partial duplication of the TTS audio...which is very odd given that the integration connects MP3 files together and does not change their content (unless you change thetts_playback_speed value from 100%). Please correct me if I have misunderstood.

Are you able to download the generated temporary MP3 files onto another computer and play them? I wonder if the issue is with the MP3 files themselves, or if perhaps there is an issue with the hardware running your Home Assistant instance, or the speaker. Could you please share with me what hardware you are running your Home Assistant instance on? It might also help to have the generated temporary MP3 files as well.

Sorry, the problem I described earlier was not very clear. Let me explain with a simple example.

First, without checking the cache option, my configuration is as follows:

service: chime_tts.say
data:
  chime_path: mp3_path_placeholder-ding_dong
  delay: 450
  final_delay: 0
  tts_playback_speed: 100
  volume_level: 1
  tts_platform: picotts
  message: Nice to meet you.
target:
  entity_id: media_player.mopidy

This TTS output is normal. I hear the result as: ding dong Nice to meet you.

Then, I check the cache option: cache: true

service: chime_tts.say
data:
  chime_path: mp3_path_placeholder-ding_dong
  delay: 450
  final_delay: 0
  tts_playback_speed: 100
  volume_level: 1
  tts_platform: picotts
  message: I never consider ease and joyfulness the purpose of life itself.
  cache: true
target:
  entity_id: media_player.mopidy

Now, I hear: ding dong I never consider ease and joyfulness the purpose of life itself.

So far, everything is normal.

After that, I change the message:

service: chime_tts.say
data:
  chime_path: mp3_path_placeholder-ding_dong
  delay: 450
  final_delay: 0
  tts_playback_speed: 100
  volume_level: 1
  tts_platform: picotts
  cache: true
  message: Hello World.
target:
  entity_id: media_player.mopidy

I will hear: ding dong I never consider ease... ding dong hello world.

I change the message again:

service: chime_tts.say
data:
  chime_path: mp3_path_placeholder-ding_dong
  delay: 450
  final_delay: 0
  tts_playback_speed: 100
  volume_level: 1
  tts_platform: picotts
  cache: true
  message: Courage is the ladder on which all the other virtues mount.
target:
  entity_id: media_player.mopidy

I will hear: ding dong hello world ding dong Courage is the ladder on which all the other virtues mount.

Now, I uncheck the cache option:

service: chime_tts.say
data:
  chime_path: mp3_path_placeholder-ding_dong
  delay: 450
  final_delay: 0
  tts_playback_speed: 100
  volume_level: 1
  tts_platform: picotts
  message: Can I help you solve this problem?
target:
  entity_id: media_player.mopidy

I will hear: ding dong ... ding dong Can I help you solve this problem?

I change the message again:

service: chime_tts.say
data:
  chime_path: mp3_path_placeholder-ding_dong
  delay: 450
  final_delay: 0
  tts_playback_speed: 100
  volume_level: 1
  tts_platform: picotts
  message: The weather is very nice today.
target:
  entity_id: media_player.mopidy

I will hear: ding dong The weather is very nice today.

The subsequent outputs are normal.

nimroddolev commented 12 months ago

Thank you for providing the full YAML and the explanations.

I ran an automation sequence on my instance where I first clear the Chime TTS cache: service: chime_tts.clear_cache and then I ran the six service calls to chime_tts.say you included in your last message. I was unable to reproduce the issue.

For the instances where the playback you heard had issues, can you please copy those temporary MP3 files to your computer and play them there to see if the issue is within the MP3 itself? You can find the full paths to the temporary MP3s in the full debug logs.

leafney commented 12 months ago

Yes, I am currently running it in debug mode.

The generated MP3 files are fine, they are all single content.

I suspect that when cache: true is enabled, it will play the last audio file generated before playing the current newly generated audio file.

However, since you cannot reproduce the issue on your end, I am starting to doubt if there is a problem with my environment. Give me some time, I will reconfigure the execution environment and try again.

leafney commented 12 months ago

Sorry for the late reply.

Unfortunately, I uninstalled the chime_tts integration version v0.7.4-beta1 and reinstalled version v0.7.3. After testing, the issue still persists. Following the testing steps I mentioned earlier still results in the problem.

I am also quite puzzled.

For now, I have set it to cache: false, and it works without any issues.

You can close this issue for now, and if anyone else encounters a similar situation, it can be reopened.

Thank you.

nimroddolev commented 11 months ago

Sorry to hear the issue persists for you. If anything changes please reopen this issue.