view modules/bluespec/Pygar/core/Mixer.bsv @ 74:31fef269ae58 pygar svn.75

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