[go: nahoru, domu]

Skip to content
This repository has been archived by the owner on Oct 19, 2023. It is now read-only.

google-assistant-sdk/pushtotalk: fix conversation_stream handling #188

Merged
merged 7 commits into from
Mar 28, 2018

Conversation

proppy
Copy link
Contributor
@proppy proppy commented Feb 22, 2018

Fixes #186 #185 #155

  • close stream on stop_recording
  • restart stream on start_playback
  • flush stream on stop_playback

The previous behaviour (keeping the stream open the whole time)
exposed a race condition where we tried to flush (write to) a stream
that was still recording (read-only mode).

- close stream on stop_recording
- restart stream on start_playback
- flush stream on stop_playback

The previous behaviour (keeping the stream open the whole time)
exposed a race condition where we tried to flush (write to) a stream
that was still recording (read-only mode).

Change-Id: I629bee9267744d5080bcdaff61c6da241ba8fe7f
@@ -212,13 +215,12 @@ def flush(self):

def start(self):
"""Start the underlying stream."""
if not self._audio_stream.active:
if self._audio_stream.active and not self._audio_stream.active:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The condition will never be met so audio stream will never start

Copy link
Contributor Author
@proppy proppy Feb 22, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh yep, this is a typo, this should go in flush :)

@PizlaTheDeveloper
Copy link
Contributor
PizlaTheDeveloper commented Feb 22, 2018

When returning back the original condition mentioned in my review

if not self._audio_stream.active:

the assistant will run but will fail when trying to play assistant response

pushtotalk.py:
self.conversation_stream.write(resp.audio_out.audio_data)

audio_helpers.py:
self._start_playback.wait()
will wait forever

Maybe this is not the right way how to report these issues, but I am beginner to GitHub :)

@Fleker Fleker self-requested a review February 22, 2018 18:35
Change-Id: Iaff6e3f25c57e82f10073812fcbea7b165ef044a
@proppy
Copy link
Contributor Author
proppy commented Feb 22, 2018

@PizlaTheDeveloper PTAL I fixed the typo :)

@PizlaTheDeveloper
Copy link
Contributor

Hi, thank you for your fix, but as I mentioned already, with this fix pushtotalk.py gets stuck when trying to play assistant's response for any query

When returning back the original condition mentioned in my review

if not self._audio_stream.active:

the assistant will run but will fail when trying to play assistant response

pushtotalk.py:
self.conversation_stream.write(resp.audio_out.audio_data)

audio_helpers.py:
self._start_playback.wait()
will wait forever

Copy link
Contributor
@PizlaTheDeveloper PizlaTheDeveloper left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving audio_helpers changes...
Problems located in pushtotalk.py

@PizlaTheDeveloper PizlaTheDeveloper mentioned this pull request Mar 18, 2018
- remove conversation stream threading.Event based locks
- call stop_recording from the thread reading from the audio stream
- fix tests

According to:
https://app.assembla.com/spaces/portaudio/wiki/Tips_Threading
"""
In general, calls to ReadStream and WriteStream on different
streams in different threads are probably safe. That means that if you
are doing Blocking I/O, one thread may read and write to one stream,
and another thread may read and write to another.
"""

Change-Id: I1355986600453e9d1d932ebfc3e094359407b60d
@googlebot
Copy link

So there's good news and bad news.

👍 The good news is that everyone that needs to sign a CLA (the pull request submitter and all commit authors) have done so. Everything is all good there.

😕 The bad news is that it appears that one or more commits were authored by someone other than the pull request submitter. We need to confirm that all authors are ok with their commits being contributed to this project. Please have them confirm that here in the pull request.

Note to project maintainer: This is a terminal state, meaning the cla/google commit status will not change from this state. It's up to you to confirm consent of the commit author(s) and merge this pull request when appropriate.

@proppy
Copy link
Contributor Author
proppy commented Mar 19, 2018

@PizlaTheDeveloper I merged your change from #206 in this PR, and added more fixes and top: PTAL!

if resp.speech_results:
logging.info('Transcript of user request: "%s".',
' '.join(r.transcript
for r in resp.speech_results))
self.conversation_stream.start_playback()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should playback start when we get text speech results? I think it makes sense to do it when we get audio data.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think to start playback in 'if len(resp.audio_out.audio_data) > 0:' ?
Yes, it might be better way...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but we also don't want to call start_playback more than one time.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should be able to check if start_playback has started already. Right now it feels like it is in the wrong place.

Copy link
Contributor Author
@proppy proppy Mar 23, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Fleker do you want me to change this in this PR?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should probably change it in this PR, but I'll leave it up to you.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@proppy
Copy link
Contributor Author
proppy commented Mar 20, 2018

@PizlaTheDeveloper can you confirm it fixes the issue for you?

Change-Id: I14440b02745aad6ac67494bd4f7c7833d9dce1d6
- remove end_of_utterance()
- stop recording from main thread with explicit locking

Change-Id: I07b24b8193560783945acde9f2b55e5533578ebd
…io data

Change-Id: Icfd2e3a01ddbb35e17e54b53f26091580bf31f0f
@proppy
Copy link
Contributor Author
proppy commented Mar 27, 2018

@Fleker PTAL

Copy link
Contributor
@Fleker Fleker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@proppy proppy merged commit 8302799 into master Mar 28, 2018
@moham96
Copy link
moham96 commented Mar 28, 2018

@proppy I still have issues with this fix.
If I use your fix I get the following error:

(env3dev) ➜  ~ googlesamples-assistant-pushtotalk
INFO:root:Connecting to embeddedassistant.googleapis.com
Traceback (most recent call last):
  File "/home/pi/env3dev/bin/googlesamples-assistant-pushtotalk", line 11, in <module>
    sys.exit(main())
  File "/home/pi/env3dev/lib/python3.5/site-packages/click/core.py", line 722, in __call__
    return self.main(*args, **kwargs)
  File "/home/pi/env3dev/lib/python3.5/site-packages/click/core.py", line 697, in main
    rv = self.invoke(ctx)
  File "/home/pi/env3dev/lib/python3.5/site-packages/click/core.py", line 895, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/home/pi/env3dev/lib/python3.5/site-packages/click/core.py", line 535, in invoke
    return callback(*args, **kwargs)
  File "/home/pi/env3dev/lib/python3.5/site-packages/googlesamples/assistant/grpc/pushtotalk.py", line 347, in main
    flush_size=audio_flush_size
  File "/home/pi/env3dev/lib/python3.5/site-packages/googlesamples/assistant/grpc/audio_helpers.py", line 190, in __init__
    blocksize=int(block_size/2),  # blocksize is in number of frames.
  File "/home/pi/env3dev/lib/python3.5/site-packages/sounddevice.py", line 1256, in __init__
    **_remove_self(locals()))
  File "/home/pi/env3dev/lib/python3.5/site-packages/sounddevice.py", line 772, in __init__
    'Error opening {0}'.format(self.__class__.__name__))
  File "/home/pi/env3dev/lib/python3.5/site-packages/sounddevice.py", line 2563, in _check
    raise PortAudioError(errormsg, err)
sounddevice.PortAudioError: Error opening RawStream: Device unavailable [PaErrorCode -9985]

While if I don't use these fixes I can run the sample with no errors and i can interact with the assistant but sometimes I get the usual audio errors:

(env) ➜  ~ googlesamples-assistant-pushtotalk
INFO:root:Connecting to embeddedassistant.googleapis.com
Press Enter to send a new request...
INFO:root:Recording audio request.
WARNING:root:SoundDeviceStream read overflow (3200, 6400)
INFO:root:Transcript of user request: "tell".
INFO:root:Playing assistant response.
INFO:root:Transcript of user request: "tell me".
INFO:root:Playing assistant response.
INFO:root:Transcript of user request: "tell me a".
INFO:root:Playing assistant response.
INFO:root:Transcript of user request: "tell me a joke".
INFO:root:Playing assistant response.
INFO:root:Transcript of user request: "tell  me a joke".
INFO:root:Playing assistant response.
INFO:root:Transcript of user request: "tell me  a joke".
INFO:root:Playing assistant response.
INFO:root:Transcript of user request: "tell me a  joke".
INFO:root:Playing assistant response.
INFO:root:Transcript of user request: "tell me a joke".
INFO:root:Playing assistant response.
INFO:root:End of audio request detected
INFO:root:Transcript of user request: "tell me a joke".
INFO:root:Playing assistant response.
WARNING:root:SoundDeviceStream write underflow (size: 1600)
WARNING:root:SoundDeviceStream write underflow (size: 1600)
WARNING:root:SoundDeviceStream write underflow (size: 1600)
WARNING:root:SoundDeviceStream write underflow (size: 1600)
WARNING:root:SoundDeviceStream write underflow (size: 1600)
WARNING:root:SoundDeviceStream write underflow (size: 1600)
WARNING:root:SoundDeviceStream write underflow (size: 1600)
WARNING:root:SoundDeviceStream write underflow (size: 1600)
WARNING:root:SoundDeviceStream write underflow (size: 1600)
INFO:root:Finished playing assistant response.
Press Enter to send a new request...
INFO:root:Recording audio request.
INFO:root:End of audio request detected
INFO:root:Finished playing assistant response.
Press Enter to send a new request...
INFO:root:Recording audio request.
INFO:root:End of audio request detected
INFO:root:Finished playing assistant response.
Press Enter to send a new request...
INFO:root:Recording audio request.
INFO:root:End of audio request detected
INFO:root:Finished playing assistant response.
Traceback (most recent call last):
  File "/home/pi/env/bin/googlesamples-assistant-pushtotalk", line 11, in <module>
    sys.exit(main())
  File "/home/pi/env/lib/python3.5/site-packages/click/core.py", line 722, in __call__
    return self.main(*args, **kwargs)
  File "/home/pi/env/lib/python3.5/site-packages/click/core.py", line 697, in main
    rv = self.invoke(ctx)
  File "/home/pi/env/lib/python3.5/site-packages/click/core.py", line 895, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/home/pi/env/lib/python3.5/site-packages/click/core.py", line 535, in invoke
    return callback(*args, **kwargs)
  File "/home/pi/env/lib/python3.5/site-packages/googlesamples/assistant/grpc/pushtotalk.py", line 424, in main
    continue_conversation = assistant.assist()
  File "/home/pi/env/lib/python3.5/site-packages/tenacity/__init__.py", line 214, in wrapped_f
    return self.call(f, *args, **kw)
  File "/home/pi/env/lib/python3.5/site-packages/tenacity/__init__.py", line 295, in call
    start_time=start_time)
  File "/home/pi/env/lib/python3.5/site-packages/tenacity/__init__.py", line 252, in iter
    return fut.result()
  File "/usr/lib/python3.5/concurrent/futures/_base.py", line 398, in result
    return self.__get_result()
  File "/usr/lib/python3.5/concurrent/futures/_base.py", line 357, in __get_result
    raise self._exception
  File "/home/pi/env/lib/python3.5/site-packages/tenacity/__init__.py", line 298, in call
    result = fn(*args, **kwargs)
  File "/home/pi/env/lib/python3.5/site-packages/googlesamples/assistant/grpc/pushtotalk.py", line 169, in assist
    self.conversation_stream.stop_playback()
  File "/home/pi/env/lib/python3.5/site-packages/googlesamples/assistant/grpc/audio_helpers.py", line 288, in stop_playback
    self._source.stop()
  File "/home/pi/env/lib/python3.5/site-packages/googlesamples/assistant/grpc/audio_helpers.py", line 221, in stop
    self.flush()
  File "/home/pi/env/lib/python3.5/site-packages/googlesamples/assistant/grpc/audio_helpers.py", line 211, in flush
    self._audio_stream.write(b'\x00' * self._flush_size)
  File "/home/pi/env/lib/python3.5/site-packages/sounddevice.py", line 1202, in write
    _check(err)
  File "/home/pi/env/lib/python3.5/site-packages/sounddevice.py", line 2563, in _check
    raise PortAudioError(errormsg, err)
sounddevice.PortAudioError: Can't write to an input only stream [PaErrorCode -9974]

This error I get only on the raspberry pi, if I run the sample on my laptop I have no problem using your fixes

Thanks.

@Fleker
Copy link
Contributor
Fleker commented Mar 28, 2018

@moham96 we have not done a new release of code. In order to test, you will need to git pull on this repository and run the pushtotalk.py file directly

@proppy
Copy link
Contributor Author
proppy commented Mar 29, 2018

@moham96 can you confirm that you installed the package with:

pip install  git+https://github.com/googlesamples/assistant-sdk-python.git@master

?

@moham96
Copy link
moham96 commented Mar 29, 2018

@Fleker I built the sdk from source (didn't use a release version)

@moham96
Copy link
moham96 commented Mar 29, 2018

@proppy I cloned the repo and built a wheel for the google-assistant-sdk and installed it in a virtual environment.
building from source without wheels fails for me. but I can assure you that I have the code installed correctly using wheels.

@moham96
Copy link
moham96 commented Mar 29, 2018

@proppy this is the error i get when running
pip install git+https://github.com/googlesamples/assistant-sdk-python.git@master


(env3devbranch) ➜  ~ pip install  git+https://github.com/googlesamples/assistant-sdk-python.git@master
Collecting git+https://github.com/googlesamples/assistant-sdk-python.git@master
  Cloning https://github.com/googlesamples/assistant-sdk-python.git (to master) to /tmp/pip-3in6ey18-build
    Complete output from command python setup.py egg_info:
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/usr/lib/python3.5/tokenize.py", line 454, in open
        buffer = _builtin_open(filename, 'rb')
    FileNotFoundError: [Errno 2] No such file or directory: '/tmp/pip-3in6ey18-build/setup.py'
    
    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-3in6ey18-build/

this issue happens beacuse it tries to run setup.py at the root of repository but there is no setup.py in the root, only in the google-assistant-sdk and google-assistant-grpc directories, maybe this needs a fix ?!

@proppy
Copy link
Contributor Author
proppy commented Mar 29, 2018

@moham96 can you try to pip install --upgrade pip first?

@proppy
Copy link
Contributor Author
proppy commented Mar 29, 2018

@moham96 sorry the right command is:

pip install git+https://github.com/googlesamples/assistant-sdk-python.git/@master#subdirectory=google-assistant-sdk

@moham96
Copy link
moham96 commented Mar 29, 2018

@proppy when using this command it states that it installed correctly but when I try to run the sample a got a lot of pkg_resources.DistributionNotFound: errors due to requirements not installed, anyway I installed all the missing requirements and now I can run the sample but the same error appears
sounddevice.PortAudioError: Error opening RawStream: Device unavailable [PaErrorCode -9985]

@proppy
Copy link
Contributor Author
proppy commented Mar 29, 2018

@moham96 thanks for testing

@proppy when using this command it states that it installed correctly but when I try to run the sample a got a lot of pkg_resources.DistributionNotFound: errors due to requirements not installed,

I see, that must be because [samples] extras were not included.

now I can run the sample but the same error appears
sounddevice.PortAudioError: Error opening RawStream: Device unavailable [PaErrorCode -9985]

This seems like a different error like the one you linked in #188 (comment)

Can you open a separate issue with the full logs, there is also something wrong with Playing assistant response. being logged multiple time. It should log something lik that

grpc 🍡  /tmp/env/bin/python pushtotalk.py 
INFO:root:Connecting to embeddedassistant.googleapis.com
Press Enter to send a new request...

INFO:root:Recording audio request.
INFO:root:Transcript of user request: "what".
INFO:root:Transcript of user request: "what time".
INFO:root:Transcript of user request: "what time is".
INFO:root:Transcript of user request: "what time is it".
INFO:root:Transcript of user request: "what  time is it".
INFO:root:Transcript of user request: "what time  is it".
INFO:root:Transcript of user request: "what time is  it".
INFO:root:Transcript of user request: "what time is it".
INFO:root:End of audio request detected.
INFO:root:Stopping recording.
INFO:root:Transcript of user request: "what time is it".
INFO:root:Playing assistant response.
INFO:root:Finished playing assistant response.
Press Enter to send a new request...  C-c C-c
INFO:root:Recording audio request.

@moham96
Copy link
moham96 commented Mar 29, 2018

@proppy sure, I will open a new issue.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Error in audio_helpers while setting volume
5 participants