diff modules/bluespec/Pygar/core/Mixer.bsv @ 44:9b0dfce52c29 pygar svn.45

[svn r45] adding mixer
author punk
date Wed, 05 May 2010 12:30:18 -0400
parents
children 2991344775f8
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/modules/bluespec/Pygar/core/Mixer.bsv	Wed May 05 12:30:18 2010 -0400
     1.3 @@ -0,0 +1,107 @@
     1.4 +// The MIT License
     1.5 +
     1.6 +// Copyright (c) 2009 Massachusetts Institute of Technology
     1.7 +
     1.8 +// Permission is hereby granted, free of charge, to any person obtaining a copy
     1.9 +// of this software and associated documentation files (the "Software"), to deal
    1.10 +// in the Software without restriction, including without limitation the rights
    1.11 +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    1.12 +// copies of the Software, and to permit persons to whom the Software is
    1.13 +// furnished to do so, subject to the following conditions:
    1.14 +
    1.15 +// The above copyright notice and this permission notice shall be included in
    1.16 +// all copies or substantial portions of the Software.
    1.17 +
    1.18 +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    1.19 +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    1.20 +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    1.21 +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    1.22 +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    1.23 +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    1.24 +// THE SOFTWARE.
    1.25 +
    1.26 +import Connectable::*;
    1.27 +import GetPut::*;
    1.28 +import ClientServer::*;
    1.29 +import Vector::*;
    1.30 +import FIFO::*;
    1.31 +import FixedPoint::*;
    1.32 +
    1.33 +`define MAX_VOICES 2
    1.34 +
    1.35 +//AWB includes
    1.36 +`include "asim/provides/low_level_platform_interface.bsh"
    1.37 +`include "asim/provides/soft_connections.bsh"
    1.38 +`include "asim/provides/common_services.bsh"
    1.39 +
    1.40 +// Local includes
    1.41 +`include "asim/provides/audio_pipe_types.bsh"
    1.42 +`include "asim/provides/path_types.bsh"
    1.43 +
    1.44 +interface PutMixerIn;
    1.45 +   method Action sendVoiceIn(AudioStream voiceInput);
    1.46 +endinterface 
    1.47 +
    1.48 +interface Mixer;
    1.49 +   interface PutMixerIn toMixer;
    1.50 +   interface Get#(AudioProcessorUnit) mixerOut;
    1.51 +endinterface
    1.52 +
    1.53 +function Bool isAudioFini(Vector#(`MAX_VOICES, Reg#(Bool)) voiceStat);
    1.54 +   Bool result = True;
    1.55 +   for (Integer i = 0; i < `MAX_VOICES; i = i+1)
    1.56 +      begin
    1.57 +         result = voiceStat[i] && result;
    1.58 +      end
    1.59 +   return result; 
    1.60 +endfunction
    1.61 +
    1.62 +module [CONNECTED_MODULE]  mkMixer#(Vector#(`MAX_VOICES, Volume) scalars) (Mixer);
    1.63 +
    1.64 +	 
    1.65 +  // Instantiate the modules
    1.66 +   Vector#(`MAX_VOICES, FIFO#(AudioPipeUnit)) voicesIn <- replicateM(mkFIFO());
    1.67 +// <- newVector();
    1.68 +   Vector#(`MAX_VOICES, Reg#(Bool)) voiceFini <- replicateM(mkReg(False));
    1.69 +
    1.70 +   FIFO#(AudioProcessorUnit) masterFifo <- mkFIFO();
    1.71 +
    1.72 +   rule sendEnd(isAudioFini(voiceFini));
    1.73 +      masterFifo.enq(tagged EndOfFile);
    1.74 +      //prep for reset
    1.75 +      for (Integer i = 0; i < `MAX_VOICES; i = i+1)
    1.76 +	 voiceFini[i] <= False;
    1.77 +   endrule
    1.78 +   
    1.79 +   rule processSample(!isAudioFini(voiceFini));  //implicit on all voiceFifos having data
    1.80 +      FixedPoint#(32,32) sum = 0;  //this should allow 16 voices at 16 bits without messing up
    1.81 +      for (Integer i = 0; i < `MAX_VOICES; i = i+1)
    1.82 +	 begin
    1.83 +	    if (voicesIn[i].first() matches tagged Valid .data)
    1.84 +	      begin
    1.85 +		 case (data) matches
    1.86 +		    tagged Sample .sample :
    1.87 +	               sum = sum + ((fromInt(sample) * fromInt(scalars[i])) >> 8); 
    1.88 +	            //shifting right 8 to divide by 256
    1.89 +		    tagged EndOfFile :
    1.90 +		       voiceFini[i] <= True;
    1.91 +		 endcase
    1.92 +	      end
    1.93 +	    else voiceFini[i] <= True;
    1.94 +	    voicesIn[i].deq();
    1.95 +	 end
    1.96 +      
    1.97 +       masterFifo.enq(tagged Sample truncate(fxptGetInt(sum >> `MAX_VOICES)));
    1.98 +   endrule
    1.99 + 
   1.100 +  // Internal connections
   1.101 +
   1.102 +   interface PutMixerIn toMixer;
   1.103 +      method Action sendVoiceIn(AudioStream voiceInput);
   1.104 +	 voicesIn[voiceInput.voice].enq(voiceInput.data);
   1.105 +      endmethod
   1.106 +   endinterface
   1.107 +      
   1.108 +   interface Get mixerOut = fifoToGet(masterFifo);
   1.109 +
   1.110 +endmodule