• Post Reply Bookmark Topic Watch Topic
  • New Topic

confusion about instance fields  RSS feed

 
Ranch Hand
Posts: 128
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a class called CueWrapper that plays a sound (wav format) using javax.sound.sampled, moving data from an AudioInputStream to a SourceDataLine.

I have class called LoopPlayer which will play two CueWrappers in succession, end-to-end, or with a gap or slight overlap.

In LoopPlayer, I make two separate instances of CueWrapper, called cue1 & cue2 which both share a filename (they are reading the same data). When LoopPlayer plays one or the other, it executes each play command on its own thread, taken from a thread pool.

Here is the question:

CueWrapper makes use of variables of type InputStream and AudioInputStream. If I make these two variables "method variables" of the method "play" in CueWrapper -- i.e., not created until cue1.play() or cue2.play() is called, then LoopPlayer plays the cues perfectly, even when the cues overlap in time. (Note: cue1 might overlap with cue2, but never with itself.)

Alternatively, if I make these two variables "instance variables" -- in other words, declare them as private at the outset, outside of the constructor, but NOT static, then when cue1 & and cue2 overlap via the same LoopPlayer calls as above, there is some interference heard in the audio output, as if CueWrapper is trying to use a single copy of InputStream or AudioInputStream for both instances.

I thought a variable declared as an instance variable reserves memory area that is solely for the use of the particular object made from a class, the only exception being "static" instance variables. Is there something special about the circumstances here (due to the use of File IO objects, perhaps? or the multithreading?) or is there something very fundamental about "instance variables" that I am not getting?

Many thanks!
 
Marshal
Posts: 56608
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Difficult to tell. Maybe you have two extant instances simultaneously which are interfering with each other. If you use local variables, they go out of scope at the end of the method, so the two instances don't have concurrent availability.
 
Phil Freihofner
Ranch Hand
Posts: 128
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the reply!

I added some code to "name" the instance and then report the instance name during playback.

And it turns out that I was in fact playing cue1 twice when I thought I was playing cue1 with cue2. The code written to control which of the two is to be played next is not switching properly.

Problem solved.

 
Campbell Ritchie
Marshal
Posts: 56608
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done sorting it out
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!