annotate 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
rev   line source
punk@44 1 // The MIT License
punk@44 2
punk@44 3 // Copyright (c) 2009 Massachusetts Institute of Technology
punk@44 4
punk@44 5 // Permission is hereby granted, free of charge, to any person obtaining a copy
punk@44 6 // of this software and associated documentation files (the "Software"), to deal
punk@44 7 // in the Software without restriction, including without limitation the rights
punk@44 8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
punk@44 9 // copies of the Software, and to permit persons to whom the Software is
punk@44 10 // furnished to do so, subject to the following conditions:
punk@44 11
punk@44 12 // The above copyright notice and this permission notice shall be included in
punk@44 13 // all copies or substantial portions of the Software.
punk@44 14
punk@44 15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
punk@44 16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
punk@44 17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
punk@44 18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
punk@44 19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
punk@44 20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
punk@44 21 // THE SOFTWARE.
punk@44 22
punk@44 23 import Connectable::*;
punk@44 24 import GetPut::*;
punk@44 25 import ClientServer::*;
punk@44 26 import Vector::*;
punk@44 27 import FIFO::*;
punk@44 28 import FixedPoint::*;
punk@44 29
punk@44 30 `define MAX_VOICES 2
punk@44 31
punk@44 32 //AWB includes
punk@44 33 `include "asim/provides/low_level_platform_interface.bsh"
punk@44 34 `include "asim/provides/soft_connections.bsh"
punk@44 35 `include "asim/provides/common_services.bsh"
punk@44 36
punk@44 37 // Local includes
punk@44 38 `include "asim/provides/audio_pipe_types.bsh"
punk@44 39 `include "asim/provides/path_types.bsh"
punk@44 40
punk@44 41 interface PutMixerIn;
punk@44 42 method Action sendVoiceIn(AudioStream voiceInput);
punk@44 43 endinterface
punk@44 44
punk@44 45 interface Mixer;
punk@44 46 interface PutMixerIn toMixer;
punk@44 47 interface Get#(AudioProcessorUnit) mixerOut;
punk@44 48 endinterface
punk@44 49
punk@44 50 function Bool isAudioFini(Vector#(`MAX_VOICES, Reg#(Bool)) voiceStat);
punk@44 51 Bool result = True;
punk@44 52 for (Integer i = 0; i < `MAX_VOICES; i = i+1)
punk@44 53 begin
punk@44 54 result = voiceStat[i] && result;
punk@44 55 end
punk@44 56 return result;
punk@44 57 endfunction
punk@44 58
punk@44 59 module [CONNECTED_MODULE] mkMixer#(Vector#(`MAX_VOICES, Volume) scalars) (Mixer);
punk@44 60
punk@44 61
punk@44 62 // Instantiate the modules
punk@44 63 Vector#(`MAX_VOICES, FIFO#(AudioPipeUnit)) voicesIn <- replicateM(mkFIFO());
punk@44 64 // <- newVector();
punk@44 65 Vector#(`MAX_VOICES, Reg#(Bool)) voiceFini <- replicateM(mkReg(False));
punk@44 66
punk@44 67 FIFO#(AudioProcessorUnit) masterFifo <- mkFIFO();
punk@44 68
punk@44 69 rule sendEnd(isAudioFini(voiceFini));
punk@44 70 masterFifo.enq(tagged EndOfFile);
punk@44 71 //prep for reset
punk@44 72 for (Integer i = 0; i < `MAX_VOICES; i = i+1)
punk@44 73 voiceFini[i] <= False;
punk@44 74 endrule
punk@44 75
punk@44 76 rule processSample(!isAudioFini(voiceFini)); //implicit on all voiceFifos having data
punk@44 77 FixedPoint#(32,32) sum = 0; //this should allow 16 voices at 16 bits without messing up
punk@44 78 for (Integer i = 0; i < `MAX_VOICES; i = i+1)
punk@44 79 begin
punk@44 80 if (voicesIn[i].first() matches tagged Valid .data)
punk@44 81 begin
punk@44 82 case (data) matches
punk@44 83 tagged Sample .sample :
punk@44 84 sum = sum + ((fromInt(sample) * fromInt(scalars[i])) >> 8);
punk@44 85 //shifting right 8 to divide by 256
punk@44 86 tagged EndOfFile :
punk@44 87 voiceFini[i] <= True;
punk@44 88 endcase
punk@44 89 end
punk@44 90 else voiceFini[i] <= True;
punk@44 91 voicesIn[i].deq();
punk@44 92 end
punk@44 93
punk@44 94 masterFifo.enq(tagged Sample truncate(fxptGetInt(sum >> `MAX_VOICES)));
punk@44 95 endrule
punk@44 96
punk@44 97 // Internal connections
punk@44 98
punk@44 99 interface PutMixerIn toMixer;
punk@44 100 method Action sendVoiceIn(AudioStream voiceInput);
punk@44 101 voicesIn[voiceInput.voice].enq(voiceInput.data);
punk@44 102 endmethod
punk@44 103 endinterface
punk@44 104
punk@44 105 interface Get mixerOut = fifoToGet(masterFifo);
punk@44 106
punk@44 107 endmodule