[Py4j-users] Fwd: Question about Python bytearray and Java byte[]
Status: Beta
Brought to you by:
barthe
From: <kur...@co...> - 2012-10-14 20:50:52
|
Hi Barthelemy, OK I got your email and d/l and installed the new .jar and python package and it appears to be working! Specifically the output of the .wav files I generate are identical now. Two questions/issues though: 1. I notice 'old' py4j0.7 python package will not work with the new py4j0.7.jar. I didn't debug just an fyi and not an issue, just mentioning. Is there a way I can pip install the version you mentioned? Right now when our developers do pip install py4j it picks up the 'old' 0.7. And they'll have to download it and build/install it themselves. 2. Notice a new exception I need to handle on Python side while waiting for Gateway to start: except (socket.error, Py4JNetworkError): Is that expected? Thanks, Kurt ----- Original Message ----- From: "kurt vogel" <kur...@co...> To: py4...@li... Sent: Sunday, October 14, 2012 12:25:51 PM Subject: Question about Python bytearray and Java byte[] Hello all, First off I want to say thanks to the developers of this project. We are a python shop using py4j to work with a vendor that provides us .class and .so library interfaces. For our Mac and Windows developers we use the py4j package to talk to the .class driver otherwise we use Python's ctypes to interact with their native unix .so library. So my question relates to bytearrays in Python. I have a straightforward Gateway java class attached below that interacts with our vendor's code. In red I've highlighted where my issues are. On the python side I'm doing this: code = gateway.ttsRequestBuffer(self.server_ip, self.server_port, text, self.voice, fmt, bFirst, bAll) buf = self.gw.ttsGetBufferData() if (code == driver_h.TTS_RESULT_SUCCESS) else None buf is a Python bytearray. The Java Gateway is returning byte[] which in this case is the binary of a short .wav clip. Attached are two files, each one is the hexdump of the short .wav file. The unix one is generated by using Python's ctype library and the Java uses py4j to talk to our vendor's library. Java using py4j: 00000000 52 49 46 46 00 1c 02 00 57 41 56 45 66 6d 74 20 |RIFF....WAVEfmt | 00000010 12 00 00 00 01 00 01 00 40 1f 00 00 80 3e 00 00 |........@....>..| Unix using ctypes: 00000000 52 49 46 46 da 1c 02 00 57 41 56 45 66 6d 74 20 |RIFF....WAVEfmt | 00000010 12 00 00 00 01 00 01 00 40 1f 00 00 80 3e 00 00 |........@....>..| These are the first 32 bytes of the files. This occurs throughout and doesn't seem to be random, take a look at the file differences between the two attachments, it's very strange. And I did test writeToTempfile() to verify it wasn't on the Java side. The problem definitely is between returning the byte[] buffer on the Java side and the reading of that bytearray on Python side. We would love to get this working so our Mac and Windows developers can work with our vendor's library. Any ideas would be helpful, Thanks! Kurt ******************************* import java.lang.String; import java.io.IOException; import py4j.GatewayServer; import voiceware.libttsapi; // This is our vendors library class Gateway { private libttsapi m_ttsapi; /* Default c'tor create libttsapi instance used by our gateway to * communicate with our python app */ public Gateway() { m_ttsapi = new libttsapi(); } /* python client will call this and get socket error if gateway not up */ public boolean get_gw_status() { return true ; } /* server status port is default to 7777 */ public int ttsRequestStatus(String ipaddr, int status_port) { int rc; try { rc = m_ttsapi.ttsRequestStatus(ipaddr, status_port); } catch (IOException e) { rc = -9; } return rc; } /* request to generate buffer from tts server */ public int ttsRequestBuffer(String ipaddr, int port, String text, int speakerId, int bufformat, int reqfirst, int oneframe) { int rc; try { rc = m_ttsapi.ttsRequestBuffer(ipaddr, port, text, speakerId, bufformat, reqfirst, oneframe); } catch (IOException e) { rc = -9; } return rc; } /* Typically after if ttsRequestBuffer() successfull to get buffer contents */ public byte [] ttsGetBufferData() { return m_ttsapi.szVoiceData; } /* main entry point for Gateway */ public static void main(String[] args) { GatewayServer gatewayServer = new GatewayServer( new Gateway()); gatewayServer.start(); } } |